GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — NumPy Memory Layout Exercises, Practice, Solution

  • Home
  • Python Home
  • Python Exercises
  • ▼Python NumPy
  • NumPy Home
  • ▼Basic Operations and Arrays
  • NumPy Basic
  • NumPy Array
  • ▼Mathematics, Linear Algebra, and Statistics
  • NumPy Mathematics
  • NumPy Linear Algebra
  • NumPy Statistics
  • ▼Random Numbers
  • NumPy Random
  • ▼Sorting, Searching, and Indexing
  • Sorting and Searching
  • NumPy Advanced Indexing
  • ▼Datetime and String Operations
  • NumPy DateTime
  • NumPy String
  • ▼Broadcasting and Memory Layout
  • NumPy Broadcasting
  • NumPy Memory Layout
  • ▼Performance Optimization and Interoperability
  • NumPy Performance Optimization
  • NumPy Interoperability
  • ▼Input/Output (I/O) Operations
  • NumPy I/O Operations
  • ▼Functions and Masked Arrays
  • NumPy Universal Functions
  • NumPy Masked Arrays
  • ▼Structured Arrays and SciPy Integration
  • NumPy Structured Arrays
  • NumPy Integration with SciPy
  • ▼Advanced Topics and Mastery
  • Advanced NumPy
  • Mastering Numpy
  • ..More to come..

NumPy Memory Layout Exercises, Practice, Explanation, Solution

Last update on April 28 2025 13:26:39 (UTC/GMT +8 hours)


This resource offers a total of 95 NumPy Memory Layout problems for practice. It includes 19 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

These exercises explore NumPy's memory layout and data manipulation techniques, covering array strides, reshaping, flattening, transposing, and contiguity in memory. You'll practice advanced indexing, shared data behavior, Fortran vs. C-order flattening, and multi-dimensional transformations. By working through these, you'll gain a deeper understanding of NumPy's memory efficiency and array operations.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. 3D Array Strides Examination

Write NumPy program to create a 3D array of shape (2, 3, 4) and print its strides.

Click me to see the sample solution


2. 1D to 2D Reshape Strides

Write a NumPy program that reshapes a 1D array of 12 elements into a 2D array of shape (3, 4) and print its strides.

Click me to see the sample solution


3. 2D Array Flatten with Ravel

Write a NumPy program to create a 2D array of shape (4, 4), then use ravel() to flatten it and print the result.

Click me to see the sample solution


4. 3D Array Ravel and Reshape

Write a NumPy program to create a 3D array of shape (3, 3, 3), then reshape it into a 1D array using ravel() and finally print the result.

Click me to see the sample solution


5. 2D Array Stride Modification

Write a NumPy program to create a 2D array of shape (5, 5) and change its strides to view every other element in the first dimension.

Click me to see the sample solution


6. 1D to 2D to Different Shape Transformation

Write a NumPy program that creates a 1D array of 16 elements, reshape it to (4, 4), then change its shape to (2, 8) without changing the underlying data.

Click me to see the sample solution


7. 2D to 3D Array Reshape

Write a NumPy program that creates a 2D array of shape (6, 2) and use reshape() to change it into a 3D array of shape (2, 3, 2). Print the new array.

Click me to see the sample solution


8. 3D Array Fortran Order Flattening

Write a NumPy program that creates a 3D array of shape (2, 4, 4) and print the strides. Then, flatten it with ravel(order='F') and print the flattened array.

Click me to see the sample solution


9. 1D to 2D Shared Data

Write a NumPy program that creates a 1D array of 9 elements and reshape it into a 3x3 matrix. Modify the matrix and observe the changes in the original array.

Click me to see the sample solution


10. 2D Array C-Order Contiguity

Write a NumPy program that creates a 2D array of shape (3, 3), change its memory layout to 'C' order using ascontiguousarray(), and print the array and its strides.

Click me to see the sample solution


11. 2D Array F-Order Contiguity

Write a NumPy program that creates a 2D array of shape (3, 3), change its memory layout to 'F' order using asfortranarray(), and print the array and its strides.

Click me to see the sample solution


12. 2D Array Ravel and Reshape Back

Write a NumPy program that creates a 2D array of shape (5, 5) and use ravel() to get a flattened array. Then use reshape() to convert it back to its original shape and print both arrays.

Click me to see the sample solution


13. 3D Array Swapaxes Operation

Write a NumPy program that creates a 3D array of shape (2, 3, 3) and use swapaxes() to swap the first and third axes. Print the original and modified arrays.

Click me to see the sample solution


14. 2D Array Flatten Order Comparison

Write a NumPy program that creates a 2D array of shape (4, 5) and use flatten(order='C') and flatten(order='F'). Print both flattened arrays.

Click me to see the sample solution


15. 2D Array Transpose Strides

Write a NumPy program that creates a 2D array of shape (3, 3), make a transposed view using .T, and print the strides of both the original and transposed arrays.

Click me to see the sample solution


16. Sub-array Strides Inspection

Write a NumPy program that creates a 1D array of 15 elements, reshape it into a (3, 5) matrix, then slice a sub-array from it and print the sub-array and its strides.

Click me to see the sample solution


17. 4D Array Multi-Reshape

Write a NumPy program that creates a 4D array of shape (2, 2, 3, 3), reshape it into a 2D array, and then reshape it back to the original shape. Print all the intermediate arrays.

Click me to see the sample solution


18. 2D to 3D Reshape Variation

Write a NumPy program that creates a 2D array of shape (4, 6), then reshape it into a 3D array of shape (2, 2, 6). Print the original and reshaped arrays.

Click me to see the sample solution


19. Sub-matrix Strides in Reshaped Array

Write a NumPy program that creates a 1D array of 20 elements and use reshape() to create a (4, 5) matrix. Slice a (2, 3) sub-matrix and print its strides.

Click me to see the sample solution


Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.