NumPy Arrays and Data Types

NumPyNumPyBeginner
Practice Now

Introduction

NumPy is a library for the Python programming language, used for performing numerical operations in Python. NumPy offers a convenient way to work with numerical data through the use of multidimensional arrays. In this tutorial, we will be discussing how to create, access, and modify NumPy arrays, as well as exploring the different data types available.


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/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/IndexingandSlicingGroup -.-> numpy/basic_idx("`Basic Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/with_statement -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/variables_data_types -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/lists -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/tuples -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/importing_modules -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/data_collections -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/numerical_computing -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} python/build_in_functions -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} numpy/1d_array -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} numpy/multi_array -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} numpy/data_array -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} numpy/basic_idx -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} numpy/bool_idx -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} numpy/fancy_idx -.-> lab-4996{{"`NumPy Arrays and Data Types`"}} end

Creating Arrays

Open up a new Python interpreter in Ternimal.

python3

Before we can start working with arrays, we need to create them. NumPy offers several methods for creating arrays, such as:

1. np.array()

This function creates an array from a Python list or tuple.

import numpy as np

## Creating an array from a Python list
my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array)  ## Output: [1 2 3 4 5]

## Creating an array from a Python tuple
my_tuple = (6, 7, 8, 9, 10)
my_array = np.array(my_tuple)
print(my_array)  ## Output: [ 6  7  8  9 10]

2. np.zeros()

This function creates an array of zeros with a given shape.

## Creating an array of zeros
my_array = np.zeros((3, 4))
print(my_array)
## Output:
## [[0. 0. 0. 0.]
##  [0. 0. 0. 0.]
##  [0. 0. 0. 0.]]

3. np.ones()

This function creates an array of ones with a given shape.

## Creating an array of ones
my_array = np.ones((2, 3))
print(my_array)
## Output:
## [[1. 1. 1.]
##  [1. 1. 1.]]

4. np.arange()

This function creates an array with evenly spaced values within a given range.

## Creating an array with evenly spaced values
my_array = np.arange(0, 10, 2)
print(my_array)  ## Output: [0 2 4 6 8]

5. np.linspace()

This function creates an array with evenly spaced values between two endpoints.

## Creating an array with evenly spaced values between two endpoints
my_array = np.linspace(0, 1, 5)
print(my_array)  ## Output: [0.   0.25 0.5  0.75 1.  ]

Accessing Elements

To access an element in a one-dimensional array, we can use its index.

my_array = np.array([1, 2, 3, 4, 5])
print(my_array[0])  ## Output: 1

To access an element in a multidimensional array, we need to specify its position in each dimension.

my_array = np.array([[1, 2], [3, 4], [5, 6]])
print(my_array[1, 0])  ## Output: 3

Modifying Elements

We can modify the value of an element in an array by assigning a new value to it.

my_array = np.array([1, 2, 3])
my_array[2] = 4
print(my_array)  ## Output: [1 2 4]

We can also modify a slice of an array.

my_array = np.array([1, 2, 3, 4, 5])
my_array[1:4] = [6, 7, 8]
print(my_array)  ## Output: [1 6 7 8 5]

Data Types

NumPy arrays can store elements of different data types, such as integers, floats, and booleans. NumPy offers a range of data types, including:

Data Type Description
int_ Integer
int8 8-bit integer
int16 16-bit integer
int32 32-bit integer
int64 64-bit integer
uint8 Unsigned 8-bit integer
uint16 Unsigned 16-bit integer
uint32 Unsigned 32-bit integer
uint64 Unsigned 64-bit integer
float_ Floating point number
float16 Half precision floating point number
float32 Single precision floating point number
float64 Double precision floating point number
complex_ Complex number
complex64 Complex number represented by two 32-bit floats
complex128 Complex number represented by two 64-bit floats
bool_ Boolean
object_ Object (can hold any Python object)

To specify a data type for an array, we can use the dtype parameter.

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

We can also convert an array to a different data type using the astype() method.

## Converting an array to a different data type
my_array = np.array([1, 2, 3], dtype=np.int32)
my_array = my_array.astype(np.float64)
print(my_array)  ## Output: [1. 2. 3.]

Summary

In this tutorial, we have learned how to create, access, and modify NumPy arrays, as well as explore the different data types available. NumPy is a powerful library for working with numerical data in Python and offers many useful functions and methods for manipulating arrays.

Other NumPy Tutorials you may like