What Are NumPy Array Attributes

Click to share! ⬇️

NumPy arrays have several attributes that provide important information about the array and its contents. These attributes are a powerful and useful feature of NumPy, and they can be very helpful when working with arrays of data. In this tutorial, we will introduce some of the most commonly used array attributes in NumPy and show you how to use them.

One of the most important array attributes is shape, which specifies the size of the array along each dimension. The shape attribute is represented as a tuple of integers, with each integer specifying the size of the corresponding dimension. For example, an array with shape (3, 4) has 3 rows and 4 columns, and contains a total of 3 * 4 = 12 elements.

Another important array attribute is size, which specifies the total number of elements in the array. The size attribute is simply the product of the elements in the shape attribute. For example, an array with shape (3, 4) has a size of 3 * 4 = 12.

The dtype attribute specifies the data type of the elements in the array. NumPy provides several built-in data types, such as int8, int16, int32, int64, float16, float32, float64, and complex64, complex128. You can also create custom data types using the dtype function, which allows you to specify the size and type of each field in the data type.

NumPy Array Shape

The shape of an array in NumPy is the number of elements along each dimension of the array. It is represented as a tuple of integers, with each integer specifying the size of the corresponding dimension.

The shape of an array is significant for several reasons. First, the shape of an array determines its size and the number of elements it contains. For example, a 2-D array with shape (3, 4) has 3 rows and 4 columns, and contains a total of 3 * 4 = 12 elements.

The shape of an array also determines how the array is indexed. For example, in a 2-D array with shape (3, 4), the element at row 0 and column 0 can be accessed using the index [0, 0], and the element at row 1 and column 2 can be accessed using the index [1, 2].

The shape of an array also determines how operations are performed on the array. For example, element-wise operations on an array are performed on each element of the array, and are broadcast to the appropriate shape if necessary. Aggregation functions, such as sum and mean, are also applied along a specified dimension of the array, as specified by the axis parameter.

Here is an example of using the shape attribute and the reshape function in NumPy:

import numpy as np

# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1  2  3  4]
#          [ 5  6  7  8]
#          [ 9 10 11 12]]

# Access the shape of the array
print(a.shape)
# Output: (3, 4)

# Reshape the array to (2, 6)
b = a.reshape((2, 6))
print(b)
# Output: [[ 1  2  3  4  5  6]
#          [ 7  8  9 10 11 12]]

NumPy Array Reshape

Array reshape in NumPy is important because it allows you to change the shape of an array without changing its data. This can be useful when you need to perform operations on an array that require a specific shape, or when you want to change the way the data is represented.

There are several ways to use the reshape function in NumPy. Here are a few examples:

You can use reshape to change the shape of an array to a new shape with the same number of elements. For example:

import numpy as np

# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1  2  3  4]
#          [ 5  6  7  8]
#          [ 9 10 11 12]]

# Reshape the array to (2, 6)
b = a.reshape((2, 6))
print(b)
# Output: [[ 1  2  3  4  5  6]
#          [ 7  8  9 10 11 12]]

Here is an example of using reshape to change the shape of an array to a new shape with a different number of elements:

import numpy as np

# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1  2  3  4]
#          [ 5  6  7  8]
#          [ 9 10 11 12]]

# Reshape the array to (4, 3)
b = a.reshape((4, 3))
print(b)
# Output: [[ 1  2  3]
#          [ 4  5  6]
#          [ 7  8  9]
#          [10 11 12]]

Note that the new shape must have the same number of elements as the original array. If the new shape has a different number of elements, a ValueError will be raised.

NumPy Array Size

The size attribute of a NumPy array is useful for checking the size of an array, or the number of elements it contains. This can be useful in a variety of scenarios, such as:

  1. Checking if an array is empty: You can use size to determine if an array is empty by checking if its size is 0.
  2. Computing the total number of elements in an array: You can use size to compute the total number of elements in an array, which can be useful for performing calculations on all elements of the array.
  3. Determining the size of an array along a specific dimension: You can use size in combination with the shape attribute to determine the size of an array along a specific dimension. For example, you can determine the number of rows or columns in a 2-D array by accessing the shape attribute with the appropriate index.
  4. Checking the compatibility of arrays for operations: You can use size to check that two arrays have the same size before performing operations on them. For example, you might want to ensure that two arrays have the same shape before adding them together element-wise.

There are many creative ways to use the size attribute of a NumPy array. Here are a few examples:

  1. You can use size to compute the total number of elements in an array. For example:
import numpy as np

# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1  2  3  4]
#          [ 5  6  7  8]
#          [ 9 10 11 12]]

# Calculate the total number of elements in the array
total_elements = a.size
print(total_elements)
# Output: 12
  1. You can use size to determine if an array is empty. An array is considered empty if its size is 0. For example:
import numpy as np

# Create an empty array
a = np.array([])
print(a)
# Output: []

# Check if the array is empty
if a.size == 0:
    print("Array is empty")
else:
    print("Array is not empty")
# Output: Array is empty
  1. You can use size to determine the size of an array along a specific dimension. For example:
import numpy as np

# Create an array with shape (3, 4)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
# Output: [[ 1  2  3  4]
#          [ 5  6  7  8]
#          [ 9 10 11 12]]

# Determine the size of the array along the rows (first dimension)
row_size = a.shape[0]
print(row_size)
# 3

NumPy Array Data Types

NumPy arrays have a data type, which specifies the type of data that the array holds. The data type of an array can be accessed using the dtype attribute.

NumPy provides several built-in data types, such as int8, int16, int32, int64, float16, float32, float64, and complex64, complex128. These data types correspond to different sizes and precisions of integer and floating point numbers, as well as complex numbers.

You can also create custom data types using the dtype function, which allows you to specify the size and type of each field in the data type.

Here is an example of using the dtype attribute and the dtype function in NumPy:

import numpy as np

# Create an array with data type float64
a = np.array([1, 2, 3], dtype='float64')
print(a)
# Output: [1. 2. 3.]
print(a.dtype)
# Output: float64

# Create an array with data type int32
b = np.array([1, 2, 3], dtype='int32')
print(b)
# Output: [1 2 3]
print(b.dtype)
# Output: int32
Click to share! ⬇️