Categories: NumpyPython

Numpy array shape manipulation, using reshape(),ravel() and transpose() in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

Python Numpy module provides a series functions that can reshape Numpy arrays. When an array is created via Numpy, it has a shape and dimension. reshape() is used to rearrange all elements of the array with another specified shape. Comparatively, ravel() will reshape the object to an one-dimensional array, If the original array has shape (x1,x2), transpose() will transform its shape to (x2,x1). Next, we will show several examples of array shape manipulation in Python.

#Import Numpy module
import numpy as np
#create an one-dimensional array of 20 elements
B = np.random.random(20)
B
#output
array([0.87049167, 0.98208694, 0.76247996, 0.53349063, 0.10419605,
       0.75908853, 0.20081232, 0.93892132, 0.74249042, 0.22420541,
       0.18680545, 0.79214439, 0.12376144, 0.76080517, 0.33618041,
       0.60158737, 0.5252644 , 0.61641478, 0.40580038, 0.30622666])
#reshape array into shape (5,4) and store to a new array
B2 = B.reshape(5,4)
B2
#output
array([[0.87049167, 0.98208694, 0.76247996, 0.53349063],
       [0.10419605, 0.75908853, 0.20081232, 0.93892132],
       [0.74249042, 0.22420541, 0.18680545, 0.79214439],
       [0.12376144, 0.76080517, 0.33618041, 0.60158737],
       [0.5252644 , 0.61641478, 0.40580038, 0.30622666]])
#reshape array again to shape (4,5)
B2.shape = (4, 5)
B2
#output
array([[0.87049167, 0.98208694, 0.76247996, 0.53349063, 0.10419605],
       [0.75908853, 0.20081232, 0.93892132, 0.74249042, 0.22420541],
       [0.18680545, 0.79214439, 0.12376144, 0.76080517, 0.33618041],
       [0.60158737, 0.5252644 , 0.61641478, 0.40580038, 0.30622666]])
#using ravel() to reshape array into one-dimension, and store to a new object
B1 = B2.ravel()
B1
#output
array([0.87049167, 0.98208694, 0.76247996, 0.53349063, 0.10419605,
       0.75908853, 0.20081232, 0.93892132, 0.74249042, 0.22420541,
       0.18680545, 0.79214439, 0.12376144, 0.76080517, 0.33618041,
       0.60158737, 0.5252644 , 0.61641478, 0.40580038, 0.30622666])
# using transpose() to reshape original array to shape 
#row and column is original column and row
B2.transpose()
array([[0.87049167, 0.98208694, 0.76247996, 0.53349063],
       [0.10419605, 0.75908853, 0.20081232, 0.93892132],
       [0.74249042, 0.22420541, 0.18680545, 0.79214439],
       [0.12376144, 0.76080517, 0.33618041, 0.60158737],
       [0.5252644 , 0.61641478, 0.40580038, 0.30622666]])

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

1 week ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

3 weeks ago

Topic Modeling using Latent Dirichlet Allocation with Python

Topic modeling is a subcategory of unsupervised machine learning method, and a clustering task in…

1 month ago

Document sentiment classification using bag-of-words in Python

For online Python training registration, click here ! Sentiment classification is a type of machine…

2 months ago

Download R Course source files

Click here to download R Course source files !

11 months ago

Download Python Course source files

Click here to download Python Course Source Files !

11 months ago