Numpy reshape() and arange()

Just learning about these numpy functions that are useful in array manipulation (and creation).

np.arrange

The arange() function from numpy creates numeric sequences. It’s syntax is as follows:

np.arrange (start, stop, step, dtype)

start: the start of the interval (optional)
stop: the end of the interval
step: the step between values (optional)
dtype: the data type (optional)

import numpy as np

myArray1 = np.arange (0,14)
myArray2 = np.arange (5,14)
myArray3 = np.arange (8)
myArray4 = np.arange (0,14,1.5)
myArray5 = np.arange (0,14,dtype='float')
print (myArray1)
print (myArray2)
print (myArray3)
print (myArray4)
print (myArray5)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13] 
[ 5 6 7 8 9 10 11 12 13]
[0 1 2 3 4 5 6 7]
[ 0. 1.5 3. 4.5 6. 7.5 9. 10.5 12. 13.5]
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.]

np.reshape

Numpy reshape() function will reshape an existing array into a different dimensioned array.

import numpy as np

# create a 1 dimensional array
myArray1 = np.arange (0,9)
print (myArray1)

# convert the 1D array to a 2D array
myArray2 = myArray1.reshape(3,3) # (rows, columns)
print (myArray2)

print ("--------------------------")
print (myArray1.shape)
print (myArray2.shape)
[0 1 2 3 4 5 6 7 8] 
[[0 1 2]
[3 4 5]
[6 7 8]]

--------------------------
(9,)
(3, 3)

For the reshape to work and not throw an error, the elements in the first array must be able to fit into the array’s dimenstions that we’re creating.

In the above you can see we created a 1D array of 9 elements (0 through 8). This will fit into a 3D array of dimensionals 3 x 3. If we do not take notice of this we will get an ValueError: something like:

We can do more with the reshape function than just the above. The following is an example of converting a 1D array into a 3D array. Take a look at the following code example.

import numpy as np

myArray1 = np.arange (1,26)

print (myArray1)

myArray2 = myArray1.reshape (5,5,1) # 5 sections, 5 rows, 1 column

print (myArray2)

print ("--------------------------")
print (myArray1.shape)
print (myArray2.shape)
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25]
[[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]]
[[ 6]
[ 7]
[ 8]
[ 9]
[10]]
[[11]
[12]
[13]
[14]
[15]]
[[16]
[17]
[18]
[19]
[20]]
[[21]
[22]
[23]
[24]
[25]]]

--------------------------
(25,)
(5, 5, 1)

Here we have created a 3D array. We can see it’s a 3D array when we call the shape() function in the code (there are 3 values inbetween the braces), and we can also see it’s a 3D array because we see 3 opening square brackets [[[ in the output.

Taking a look at the code:

myArray2 = myArray1.reshape (5,5,1) # 5 sections, 5 rows, 1 column

A good way to think about whether this reshape will ‘work’ or throw an error is we can see we have 5 rows and 1 column (5*1)=5, so we have 5 values in each section and we have 5 sections (5*5=25).

Because we have 25 values in our inital array, the reshape should work just fine.

Splitting numpy arrays

import numpy as np

myArray1 = np.arange (1,13)

print (myArray1)
print ("--------------------------")
myArray2 = myArray1.reshape (2,3,2) # 5 sections, 5 rows, 1 column

print (myArray2)
print ("--------------------------")

myArray3, myArray4 = myArray1.reshape (2,3,2)

print (myArray3)
print ("--------------------------")
print (myArray4)
[ 1  2  3  4  5  6  7  8  9 10 11 12]
----------------------------
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
----------------------------
[[1 2]
[3 4]
[5 6]]
----------------------------
[[ 7 8]
[ 9 10]
[11 12]]

You can see in line 12 of the code, it’s very similar to before but now we have two variables (myArray3 and myArray4) before the = symbol.

Leave a Reply