Create a NumPy array of numbers between 10 and 20 using arange() function

Create a NumPy unidimensional (one dimension) array of sequential numbers using the NumPy arange statement between 10 and including 20 in increments (NumPy step) of 2. Then use the NumPy reshape and shape statement to transform this one-dimensional array to a multidimensional array of 2 elements by 3 elements. Then output the multidimensional array to the Python console using a print statement.

import numpy as np
a = np.arange(10, 21, 2)
print(a.reshape(2, 3))
a.shape=(2, 3)
print(a)

Output

array([[10, 12, 14],
       [16, 18, 20]])
array([[10, 12, 14],
       [16, 18, 20]])

As mentioned in the question that 20 is to be included; that is why 21 is used inside arange() function.

The question demands a multidimensional array of 2 rows and 3 columns, so (2,3) is passed to reshape() function. The same task is also performed using shape attribute.

Both reshape() function and shape attribute are used to change the shape of the NumPy array. Still, a subtle difference between reshape() function and shape attribute is that reshape() function creates a new array. In contrast, the shape attribute modifies the original array.

Recommended Posts