Array Attributes and Dtype

NumPyNumPyBeginner
Practice Now

Introduction

This tutorial will explore NumPy array attributes, focusing on the dtype attribute. NumPy is a powerful library for numerical computing in Python, and the NumPy array is a core data structure for this library.

NumPy arrays are multidimensional, homogeneous arrays, which means they can store elements of the same data type in multiple dimensions. They are efficient and convenient for numerical operations, providing many functions and capabilities.

Creating NumPy Arrays

Before exploring NumPy array attributes, let's first create a NumPy array. You can create NumPy arrays from lists, tuples, or other arrays using the numpy.array() function.

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Now you can use the numpy.array() function to create NumPy arrays

import numpy as np

## Creating a 1D array from a list
one_d_array = np.array([1, 2, 3, 4, 5])

## Creating a 2D array from a list of lists
two_d_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

NumPy Array Attributes

NumPy arrays have several attributes that provide information about the array's properties, such as:

  • shape: A tuple representing the dimensions of the array.
  • size: The total number of elements in the array.
  • ndim: The array's dimensions (axes).
  • dtype: The data type of the array elements.
  • itemsize: The size in bytes of each element in the array.

Using Array Attributes

Now, We can use these attributes in practice:

## Create a 2D array
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

## Get the shape of the array
print("Shape:", array.shape)  ## Output: (3, 3)

## Get the size of the array
print("Size:", array.size)  ## Output: 9

## Get the number of dimensions of the array
print("Number of dimensions:", array.ndim)  ## Output: 2

## Get the data type of the array elements
print("Data type:", array.dtype)  ## Output: int64 (or int32, depending on your system)

## Get the size in bytes of each element in the array
print("Item size:", array.itemsize)  ## Output: 8 (or 4, depending on your system)

Understanding Dtype

The dtype attribute is particularly important because it determines the type of data stored in the array. NumPy supports various data types, such as integers (int8, int16, int32, int64), unsigned integers (uint8, uint16, uint32, uint64), floating-point numbers (float16, float32, float64), and complex numbers (complex64, complex128).

When creating a NumPy array, you can specify the dtype using the dtype parameter. If not specified, NumPy will try to infer the data type from the input data.

Dtype Usage

Let's explore using the dtype attribute

## Create a float array from a list
float_array = np.array([1.2, 2.3, 3.4, 4.5], dtype=np.float32)
print("Float array dtype:", float_array.dtype)  ## Output: float32

## Create an integer array from a list
int_array = np.array([1, 2, 3, 4, 5], dtype=np.int16)
print("Integer array dtype:", int_array.dtype)  ## Output: int16

## Create a complex array from a list
complex_array = np.array([1 + 2j, 2 + 3j, 3 + 4j], dtype=np.complex64)
print("Complex array dtype:", complex_array.dtype)  ## Output: complex64

## Create an array and let Numpy infer the data type
mixed_array = np.array([1, 2, 3.5, 4.5])
print("Mixed array dtype:", mixed_array.dtype)  ## Output: float64

## Changing the data type of an existing array
new_dtype_array = mixed_array.astype(np.int32)
print("New dtype array:", new_dtype_array)  ## Output: [1 2 3 4]
print("New dtype:", new_dtype_array.dtype)  ## Output: int32

## Creating an array of zeros with specified dtype
zeros_array = np.zeros((3, 3), dtype=np.uint8)
print("Zeros array with dtype uint8:\n", zeros_array) ## Output:[[0 0 0] [0 0 0] [0 0 0]]

Summary

In summary, this tutorial focused on NumPy array attributes, particularly the dtype attribute. We covered the creation of NumPy arrays, explored important attributes, and delved into the significance of dtype. Understanding and effectively using the dtype attribute is crucial for efficient and accurate numerical computations in Python using NumPy arrays. Keep practicing to improve your proficiency with NumPy arrays and their attributes.

Other NumPy Tutorials you may like