Python: reshaping a numpy array
numpy arrays can be re-shaped this means that we can assign the elements of an array into a new array with a different number of rows and…
numpy arrays can be re-shaped this means that we can assign the elements of an array into a new array with a different number of rows and collumns as long as the number of elements fit into the dimensions of the new array
Reshaping an array
Lets create an array with the following properties
- has one row
- has sixteen collunsarray1 = np.arange(16)
print(array1)
print(array1.shape)
The second print verifies that there are 16 columns and one row.[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
(16,)
We want to create a new array which has the same elements as array1, but we want the new array to have two rows and eight columns.
To do this we utilize the reshape function of array1, the first parameter is the number of rows, the second is the number of columns.array2 = array1.reshape(2,8)
print(array2)
print(array2.shape)
Notice that the output of the array looks like two nested one dimension arrays with the same number of elements[[ 0 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14 15]]
(2, 8)
Be careful!
If we try to reshape an array with dimensions that do not fit the number of elements Python will throw an errorarray2 = array1.reshape(3,8)
ValueError: cannot reshape array of size 16 into shape (3,8)
Of course we can convert an array from a two dimension array to a single dimension arrayarray3 = array2.reshape(16)
Or to a different shape, the only thing that we need to take care is the number of elements that must fit exactly, this will runarray4 = array2.reshape(4,4)
But this will notarray5 = array4.reshape(2,2)
ValueError: cannot reshape array of size 16 into shape (2,2)
But what if we have to work with many arrays and we need to reshape them to a single array? numpy offers two functions for this job, hstack and vstack
vstack takes arrays as arguments, the arrays must have the same dimensionsa1 = np.array([1,2,3])
a2 = np.array([4,5,6])
a3 = np.vstack((a1,a2))
Array a3 will have the contents of a1 and a2 as a 2x3 array[[1 2 3]
[4 5 6]]
hstack on the other hand will create an one dimension arraya1 = np.array([1,2,3])
a2 = np.array([4,5,6,7])
c3 = np.hstack((a1,a2))
print(c3)
output[1 2 3 4 5 6 7]
both hstack and vstack functions need the arrays to have the same dimensions, to create a new array from arrays that do not have the same dimensions you can use the concatenate function.
concatenate arraysa1 = np.array([[1,2,3],[4,5,6]])
a2 = np.array([7,8])
b1 = np.concatenate((a1,a2), axis = None)
print(b1)
The axis parameter allows us to define what operation will performed
if axis = None then will flatten the arrays into one[1 2 3 4 5 6 7 8]
if axis = 0 it will do a horizontal stack which in our case will not work, because arrays do not have the same number of columnsValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
Changing a2 array to match the number of columns of a1 and setting the axis to 0 will worka1 = np.array([[1,2,3],[4,5,6]])
a2 = np.array([[7,8,9]])
b1 = np.concatenate((a1,a2), axis = 0)
print(b1)
Output[[1 2 3]
[4 5 6]
[7 8 9]]
if axis = 1 will do a vertical stacka1 = np.array([[1,2,3],[4,5,6]])
a2 = np.array([[7,8,9],[10,11,12]])
b1 = np.concatenate((a1,a2), axis = 1)
print(b1)
Output[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
I hope you find this article interesting :)