Numpy Array Dimensions

Numpy Array Dimensions

Numpy is a powerful library in Python that provides fast and efficient mathematical operations on large multi-dimensional arrays and matrices. One of the fundamental concepts in Numpy is the array dimension, which determines the shape and size of the array.

In this article, we will explore numpy array dimensions in detail and provide code examples to demonstrate the concepts.

Understanding Numpy Array Dimensions

Before diving into the code examples, let’s first understand what array dimensions mean in Numpy. A numpy array can have one or more dimensions. Each dimension represents a specific axis of the array. For example, a 1-dimensional array is a row or column, a 2-dimensional array is a matrix, and a 3-dimensional array is a cube-like structure.

The number of dimensions in a numpy array is known as its rank. Numpy arrays are referred to using the term “rank” instead of “dimensions.” For instance, a 1-dimensional array has a rank of 1, a 2-dimensional array has a rank of 2, and so on.

Code Examples

Now, let’s dive into some code examples that showcase numpy array dimensions and manipulation.

Example 1: Creating a 1-dimensional array

To create a 1-dimensional numpy array, we can use the numpy.array() function and pass it a list of values. Here’s an example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)

Output:

Numpy Array Dimensions

In this example, we create a 1-dimensional numpy array arr containing the values [1, 2, 3, 4, 5]. The output shows the array itself.

Example 2: Creating a 2-dimensional array

To create a 2-dimensional numpy array, we can pass a list of lists to the numpy.array() function. Each inner list represents a row in the array. Here’s an example:

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])
print(arr)

Output:

Numpy Array Dimensions

In this example, we create a 2-dimensional numpy array arr with two rows and three columns. The output shows the array structure.

Example 3: Creating a 3-dimensional array

To create a 3-dimensional numpy array, we can pass a list of matrices to the numpy.array() function. Each matrix represents a slice in the array. Here’s an example:

import numpy as np

arr = np.array([[[1, 2, 3],
                 [4, 5, 6]],

                [[7, 8, 9],
                 [10, 11, 12]]])
print(arr)

Output:

Numpy Array Dimensions

In this example, we create a 3-dimensional numpy array arr with two 2×3 matrices. The output shows the array structure.

Example 4: Getting the number of dimensions

To get the number of dimensions of a numpy array, we can use the ndim attribute. Here’s an example:

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])
print(arr.ndim)

Output:

Numpy Array Dimensions

In this example, we create a 2-dimensional numpy array arr and use the ndim attribute to get the number of dimensions, which is 2.

Example 5: Getting the shape of an array

The shape of an array represents the size of each dimension. To get the shape of an array, we can use the shape attribute. Here’s an example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr.shape)

Output:

Numpy Array Dimensions

In this example, we create a 1-dimensional numpy array arr and use the shape attribute to get its shape, which is (5,). The comma represents the absence of additional dimensions.

Example 6: Reshaping an array

We can reshape a numpy array to change its dimensional structure. To reshape an array, we can use the reshape() function. Here’s an example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape((2, 3))
print(reshaped_arr)

Output:

Numpy Array Dimensions

In this example, we create a 1-dimensional numpy array arr and reshape it to a 2-dimensional array with two rows and three columns using the reshape() function.

Example 7: Adding a new dimension

Sometimes, we may need to add a new dimension to an existing numpy array. We can achieve this using the np.newaxis keyword. Here’s an example:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
new_dimension_arr = arr[np.newaxis, :]
print(new_dimension_arr.shape)

Output:

Numpy Array Dimensions

In this example, we add a new dimension to the 1-dimensional numpy array arr using np.newaxis. The resulting array new_dimension_arr has a shape of (1, 5).

Example 8: Removing a dimension

To remove a dimension from an existing numpy array, we can use the np.squeeze() function. Here’s an example:

import numpy as np

arr = np.array([[[1, 2, 3],
                 [4, 5, 6]]])
squeezed_arr = np.squeeze(arr)
print(squeezed_arr.shape)

Output:

Numpy Array Dimensions

In this example, we create a 3-dimensional numpy array arr and remove the outermost dimension using the np.squeeze() function. The resulting array squeezed_arr has a shape of (2, 3).

Example 9: Transposing an array

To transpose a numpy array, we can use the T attribute or the np.transpose() function. Here’s an example:

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])
transposed_arr = arr.T
print(transposed_arr)

Output:

Numpy Array Dimensions

In this example, we create a 2-dimensional numpy array arr and transpose it using the T attribute. The resulting array transposed_arr swaps the rows and columns.

Example 10: Adding arrays along a specific axis

Numpy allows us to perform operations along specific axes of an array, such as adding arrays along a particular axis. Here’s an example:

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])
sum_axis_0 = np.sum(arr, axis=0)
print(sum_axis_0)

Output:

Numpy Array Dimensions

In this example, we create a 2-dimensional numpy array arr and add its elements along the first axis (axis 0) using the np.sum() function. The resulting array sum_axis_0 contains the sums of the columns.

Conclusion of Numpy Array Dimensions

Understanding numpy array dimensions is essential for working with multi-dimensional arrays using Numpy. Numpy’s array dimensions open up a world of possibilities for scientific and numerical computing.
In this article, we explored the concept of array dimensions, learned how to create arrays with different dimensions, manipulate arrays, and perform operations along specific axes. We also provided code examples to illustrate each concept.

Like(1)