NumPy Array Shape

NumPy Array Shape

Numpy is a popular library for data science in Python. Numpy provides a powerful N-dimensional array object, which is the core of the library. In this tutorial, we will learn about the shape of numpy arrays.

What is the Shape of an Array?

The shape of a numpy array is a tuple of integers that describe the number of elements in each dimension of the array. For example, the shape of a 2-dimensional array, with 3 rows and 4 columns, is (3, 4).

The shape of an array can be obtained using the shape attribute of the numpy array. The shape attribute returns a tuple of integers that describe the number of elements in each dimension of the array.

import numpy as np

# create a 2-dimensional array
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# get the shape of the array
print(arr.shape)

Output:

(2, 3)

The above code creates a 2-dimensional array with 2 rows and 3 columns, and then prints the shape of the array, which is (2, 3).

Changing the Shape of an Array

The shape of a numpy array can be changed using the reshape() function. The reshape() function returns a new array with a different shape than the original array, but with the same data.

import numpy as np

# create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

# reshape the array to a 2-dimensional array with 3 rows and 4 columns
new_arr = arr.reshape(3, 4)

# print the new array and its shape
print(new_arr)
print(new_arr.shape)

Output:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
(3, 4)

The above code creates a 1-dimensional array with 12 elements, and then reshapes the array to a 2-dimensional array with 3 rows and 4 columns using the reshape() function. The new array and its shape are then printed.

Flattening an Array

Flattening an array means converting a multi-dimensional array into a one-dimensional array. This can be done using the flatten() function.

import numpy as np

# create a 2-dimensional array
arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# flatten the array
flat_arr = arr.flatten()

# print the flattened array and its shape
print(flat_arr)
print(flat_arr.shape)

Output:

[1 2 3 4 5 6]
(6,)

The above code creates a 2-dimensional array, and then flattens the array into a 1-dimensional array using the flatten() function. The flattened array and its shape are then printed.

Resizing an Array

Resizing an array means changing the shape of the array, and adding or removing elements as necessary. This can be done using the resize() function.

import numpy as np

# create a 1-dimensional array with 6 elements
arr = np.array([1, 2, 3, 4, 5, 6])

# resize the array to a 2-dimensional array with 2 rows and 3 columns
arr.resize(2, 3)

# print the resized array and its shape
print(arr)
print(arr.shape)

Output:

[[1 2 3]
 [4 5 6]]
(2, 3)

The above code creates a 1-dimensional array with 6 elements, and then resizes the array to a 2-dimensional array with 2 rows and 3 columns using the resize() function. The resized array and its shape are then printed.

Broadcasting Arrays

When performing operations on numpy arrays of different shapes, numpy uses a set of rules known as “broadcasting” to ensure that the arrays have compatible shapes. In broadcasting, the smaller array is “broadcast” across the larger array to match its shape.

The following example demonstrates broadcasting:

import numpy as np

# create two arrays
x = np.array([1, 2, 3])
y = np.array([[4],
              [5],
              [6]])

# add the arrays together
z = x + y

# print the arrays and the result
print(x)
print(y)
print(z)

Output:

[1 2 3]
[[4]
 [5]
 [6]]
[[ 5  6  7]
 [ 6  7  8]
 [ 7  8  9]]

The above code creates two arrays, one 1-dimensional and one 2-dimensional, and then adds them together. The result is a 2-dimensional array with the same shape as the larger input array.

Conclusion

In this tutorial, we learned about the shape of numpy arrays. We learned how to obtain the shape of an array using the shape attribute, and how to change the shape of an array using the reshape(), flatten(), and resize() functions. We also learned about broadcasting, which is used to perform operations on arrays with different shapes.

Leave a Reply

Your email address will not be published. Required fields are marked *