w3resource

NumPy Memory Layout Exercises, Practice, Explanation, Solution


This resource offers a total of 19 NumPy Memory Layout problems for practice.

[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.