NumPy Memory Layout Exercises, Practice, Explanation, Solution
NumPy Memory Layout Exercises [ 19 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. 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. 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. 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. 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. 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. 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. 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.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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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:
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics