In this tutorial, we are going to discuss the shape manipulation of NumPy arrays. We will see how to convert a one-dimensional array into a matrix and vice-versa. We will also see how to find the transpose of a matrix.
What is shape Manipulation of arrays in Python?
Shape manipulation is a technique by which we can manipulate the shape of a NumPy array and then convert the initial array into an array or matrix of required shape and size. This may include converting a one-dimensional array into a matrix and vice-versa and finding transpose of the matrix by using different functions of the NumPy module.
Suppose we have an array an of length 15.
import numpy as np
a=np.random.random((15))
print(a)
Output:
[0.45571673, 0.60686929, 0.34301217, 0.68924335, 0.87628601,
0.81087616, 0.22130314, 0.08402535, 0.75766745, 0.97000277,
0.1947358 , 0.60662285, 0.87718557, 0.74830521, 0.92319394]
In the above code, we are using random.random()
function which generates random floating-point numbers between zero and one(including zero and excluding one) and then creates a NumPy array a of length 15.
How to convert a 1-D NumPy array into a matrix or 2-D NumPy array?
We can convert an array into the matrix or vice-versa with the help of reshape()
method which takes dimensions of the required output array as parameters.
import numpy as np
a=np.random.random((15))
print(a)
A=a.reshape(3,5)
print(A)
Output:
[0.51072442, 0.24339212, 0.07296422, 0.2389665 , 0.57840795,
0.1090571 , 0.55816292, 0.46774436, 0.37100759, 0.85158329,
0.20215432, 0.91473218, 0.22408144, 0.50179911, 0.51157215]
[[0.51072442, 0.24339212, 0.07296422, 0.2389665 , 0.57840795],
[0.1090571 , 0.55816292, 0.46774436, 0.37100759, 0.85158329],
[0.20215432, 0.91473218, 0.22408144, 0.50179911, 0.51157215]]
In the above code, we have elements of the initial array so we will reshape it into a matrix of size 3×5 or 5×3 such that elements of the initial array occupy all the positions in the output array. Otherwise, it will generate ValueError i.e. is if we will give a.reshape(3,6)
that will demand eighteen numbers and hence it will give ValueError
.
import numpy as np
a=np.random.random((15))
print(a)
A=a.reshape(3,6)
print(A)
Output:
ValueError Traceback (most recent call last)
<ipython-input-8-34f26575a730> in <module>
----> 1 A=a.reshape(3,6)
ValueError: cannot reshape array of size 15 into shape (3,6)
We should reshape the initial array into a matrix such that it will occupy the whole output array.
We can also directly modify the shape of an array using the shape attribute of NumPy arrays. We can directly assign the dimensions to shape attribute.
import numpy as np
a=np.random.random((15))
print(a)
A=a.reshape(3,5)
print(A)
a.shape = (3, 5)
print(a)
Output:
[0.51072442, 0.24339212, 0.07296422, 0.2389665 , 0.57840795,
0.1090571 , 0.55816292, 0.46774436, 0.37100759, 0.85158329,
0.20215432, 0.91473218, 0.22408144, 0.50179911, 0.51157215]
[[0.51072442, 0.24339212, 0.07296422, 0.2389665 , 0.57840795],
[0.1090571 , 0.55816292, 0.46774436, 0.37100759, 0.85158329],
[0.20215432, 0.91473218, 0.22408144, 0.50179911, 0.51157215]]
[[0.51072442, 0.24339212, 0.07296422, 0.2389665 , 0.57840795],
[0.1090571 , 0.55816292, 0.46774436, 0.37100759, 0.85158329],
[0.20215432, 0.91473218, 0.22408144, 0.50179911, 0.51157215]]
reshape()
method creates a new array object while changing the shape attribute of the array reshapes the initial array.How to convert 2-D arrays into one-dimensional arrays?
We can convert a matrix into a one-dimensional array by using the ravel()
method of NumPy. We can also convert a matrix into one-dimensional array by defining the shape attribute or using the reshape method by defining shape as (n,1) where n is the total number of elements in the matrix.
import numpy as np
a=np.random.random((12))
print(a)
A=a.reshape(3,4)
print(A)
A=A.ravel()
print(A)
Output:
[0.75600259, 0.13896508, 0.7448837 , 0.51311894, 0.89158404,
0.09216384, 0.16834103, 0.95998679, 0.23943659, 0.60906847,
0.98166569, 0.16903275]
[[0.75600259, 0.13896508, 0.7448837 , 0.51311894],
[0.89158404, 0.09216384, 0.16834103, 0.95998679],
[0.23943659, 0.60906847, 0.98166569, 0.16903275]]
[0.75600259, 0.13896508, 0.7448837 , 0.51311894, 0.89158404,
0.09216384, 0.16834103, 0.95998679, 0.23943659, 0.60906847,
0.98166569, 0.16903275]
How to find the transpose of a 2-D NumPy array?
Transposing a matrix means that we have to convert rows of the matrix into columns and columns of the matrix into rows i.e. rows and columns of the matrix are changed and hence shape as well as the order of elements also changes. We can use the transpose() method to find the transpose of a 2-D NumPy array.
import numpy as np
a=np.random.random((12))
A=a.reshape(3,4)
print(A)
print(A.transpose())
Output:
[[0.03540246, 0.4439432 , 0.13032118, 0.71739415],
[0.30935818, 0.66366247, 0.0526514 , 0.90658794],
[0.01321906, 0.30606385, 0.14793544, 0.04597834]]
[[0.03540246, 0.30935818, 0.01321906],
[0.4439432 , 0.66366247, 0.30606385],
[0.13032118, 0.0526514 , 0.14793544],
[0.71739415, 0.90658794, 0.04597834]]
In the above example, we can see that initially, array A has three rows and four columns and when we perform transpose operation by using the transpose() function of NumPy we see that now we have four rows and three columns.
Conclusion:
In this tutorial, we saw how to perform different operations to reshape NumPy arrays. We also saw how to find a transpose of a NumPy array.
References:
Happy Learning!