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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/ArrayBasicsGroup -.-> numpy/shape_dim("`Shapes and Dimensions`") numpy/ArrayBasicsGroup -.-> numpy/data_type("`Data Types`") numpy/ArrayBasicsGroup -.-> numpy/attr("`Basic Attributes`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/with_statement -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/variables_data_types -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/numeric_types -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/lists -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/tuples -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/importing_modules -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/data_collections -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/numerical_computing -.-> lab-8027{{"`Array Attributes and Dtype`"}} python/build_in_functions -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/1d_array -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/multi_array -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/data_array -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/shape_dim -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/data_type -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/attr -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/bool_idx -.-> lab-8027{{"`Array Attributes and Dtype`"}} numpy/fancy_idx -.-> lab-8027{{"`Array Attributes and Dtype`"}} end

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