w3resource logo
Python Exercises

Python NumPy: Collapse a 3-D array into one dimension array

Python NumPy: Array Object Exercise-49 with Solution

Write a Python program to collapse a 3-D array into one dimension array.

Sample Solution:-

Python Code:

import numpy as np
x = np.eye(3)
print("3-D array:")
print(x)
f = np.ravel(x, order='F')
print("One dimension array:")
print(f)

Sample Output:

3-D array:                                                             
[[ 1.  0.  0.]                                                         
 [ 0.  1.  0.]                                                         
 [ 0.  0.  1.]]                                                        
One dimension array:                                                   
[ 1.  0.  0.  0.  1.  0.  0.  0.  1.]

Python Code Editor:

import numpy as np
x = np.eye(3)
print("3-D array:")
print(x)
f = np.ravel(x, order='F')
print("One dimension array:")
print(f)

Improve this sample solution and post your code through Disqus