Creating and Using NumPy Arrays - A Complete Guide
Introduction to NumPy
NumPy, short for Numerical Python, is a Python library that supports scientific computing tasks. It provides support for large multi-dimensional arrays, along with a collection of high-level mathematical functions to operate on these arrays. NumPy is crucial for data science and machine learning because it enables efficient numerical computations, making it easier to work with large datasets.
NumPy offers a significant performance boost over Python lists. By leveraging vectorization, broadcasting and efficient memory layout, it succeeds at performing operations on large datasets much faster compared to Python lists.
In this guide, we’ll have a detailed discussion on the advantages of NumPy over Python lists, creating 1D, 2D and 3D arrays in NumPy, carrying out basic math operations on NumPy arrays and performing array indexing, slicing, reshaping and iterating in NumPy.
Advantages of NumPy over Python Lists
Speed and Efficiency
NumPy’s primary strength lies in its speed and efficiency, achieved through vectorization. Vectorization allows operations to be performed on entire arrays rather than iterating through individual elements, significantly enhancing performance.
Here is an example of vectorization in NumPy:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5])# Squaring each element in the array simultaneously using vectorizationres = arr ** 2print(res)
In the example, vectorization allows NumPy to square all the elements simultaneously instead of doing it individually.
Memory Efficiency
NumPy uses contiguous memory blocks, meaning it stores arrays in adjacent memory locations, allowing for quicker access and modifications. For large datasets, this efficient memory usage reduces the risk of blockages, as the data is handled more efficiently than in Python lists.
Rich Functionality
NumPy offers a rich collection of built-in mathematical functions, making complex computations simpler, such as np.sum()
, np.min()
, np.max()
, and more. Additionally, broadcasting allows NumPy to perform operations on arrays of different shapes by automatically increasing the size of the smaller array to match the bigger array, effectively extending functionality and flexibility.
Here is an example of broadcasting in NumPy:
import numpy as np# Creating a 2x3 arrayarr = np.array([[1, 2, 3], [4, 5, 6]])# Adding a scalar (3) to each element in the array# The dimensions of this scalar will be broadcast to 2x3 to match the array dimensionsres = arr + 3print(res)
Here, the scalar (3) is broadcast or expanded to a 2x3 array with all the elements equal to 3 to match the dimensions of the original array. Then, the expanded array is added to the original array to get the result.
Next, let’s discuss the different types of arrays in NumPy.
Different Types of NumPy Arrays
Understanding Array Dimensions
Let’s start with 0D arrays. A 0D array, also known as a scalar, is an array that contains a single element in it. Here is an example:
import numpy as np
arr = np.array(5)
A 1-D array, also known as a vector, is an array that contains a sequence of elements in it. This sequence of elements can be laid out in the form of a row or column:
import numpy as np
arr = np.array([1, 2, 3])
A 2D array, also known as a matrix, is an array that contains a table of elements with rows and columns:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
A 3D array, also known as a tensor, is an array that contains multiple tables of elements in it:
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
There can be more than three dimensions in a NumPy array. However, for this article, we’ll keep it to 3D arrays.
Next, let’s check out the different functions that can be used to create special arrays in NumPy, such as np.zeros()
, np.ones()
and np.arange()
.
How to Create NumPy Arrays?
The np.zeros()
function creates an array of the specified shape, filled with 0’s:
import numpy as np# Creating an array having 2 rows and 2 columnsarr = np.zeros((2, 2))print(arr)
The output is following:
[[0. 0.] [0. 0.]]
The np.ones()
function creates an array of the specified shape, filled with 1’s:
import numpy as np# Creating an array having 2 rows and 2 columnsarr = np.ones((2, 2))print(arr)
Here is the output:
[[1. 1.] [1. 1.]]
The np.arange()
function creates an array containing evenly spaced values in a specified range. For example, in the below code, the function creates an array containing elements from 0 to 2 (3 excluded). These elements are evenly spaced, i.e., each element and the element attached next to it has an equal difference of 1:
import numpy as np## Creating an array containing values from 0 - 2 (3 excluded)arr = np.arange(0, 3)print(arr)
Here is the output:
[0 1 2]
In the next section, we’ll learn about a few array attributes that we can use in NumPy.
Array Attributes
The different array attributes in NumPy are listed below:
shape
: Calculates the number of rows and columns in an array.size
: Calculates the number of elements in an array.dtype
: Finds out the data type of the elements in the array (since NumPy arrays only contain a single type of elements).ndim
: Calculates the number of dimensions in an array.
Now, let’s look at the following example:
import numpy as np# Creating an arrayarr = np.array([[1, 2, 3], [4, 5, 6]])# Using array attributesprint(arr.shape)print(arr.size)print(arr.dtype)print(arr.ndim)
Here is the output:
(2, 3)6int642
Now that we’ve discussed the different types of arrays and array attributes in NumPy, let’s learn how to perform some basic mathematical operations on NumPy arrays.
Performing Basic Mathematical Operations with NumPy Arrays
The basic mathematical operations in NumPy include addition, subtraction, multiplication, division, exponentiation, modulus, etc. Since NumPy uses vectorization, it is able to perform these operations at a high speed.
Here is an example that applies some of the above operations on two NumPy arrays:
import numpy as np# Creating two arraysarr1 = np.array([1, 2, 3])arr2 = np.array([4, 5, 6])# Adding the arraysadd = arr1 + arr2print(add)# Subtracting ‘arr2’ from ‘arr1’sub = arr1 - arr2print(sub)# Multiplying the arraysmul = arr1 * arr2print(mul)# Dividing ‘arr1’ by ‘arr2’div = arr1 / arr2print(div)
The above code produces the following output:
[5 7 9][-3 -3 -3][4 10 18][0.25 0.4 0.5]
Understanding these foundational operations will enable us to perform more challenging mathematical tasks in NumPy.
Performing Array Manipulation in NumPy: Indexing, Slicing, Reshaping and Iterating
There are several array manipulation techniques in NumPy, including indexing, slicing, reshaping and iterating. Let’s discuss them one-by-one.
Array Indexing and Slicing
Array indexing in NumPy refers to the process of accessing a particular element in an array using indices. It uses the syntax arr[n]
, where n
refers to a specific index in the arr
array.
Array slicing refers to the process of accessing a range of elements in an array using indices. It uses the syntax arr[m:n]
, where m
refers to the starting index (inclusive) and n
refers to the ending index (exclusive) in the arr
array.
Here is an example that performs indexing and slicing on a 1D array:
import numpy as np# Creating a 1D arrayarr = np.array([1, 2, 3, 4, 5])print(arr[0])print(arr[1:3])
In the above code, we first used indexing (arr[0]
) to access the element at index 0
in the array. Then, we used slicing (arr[1:4]
) to access the elements from index 1
to index 3
in the array (excluding index 4
).
This will produce the following output:
1[2 3 4]
The following example performs indexing and slicing on a 2D array:
import numpy as np# Creating a 2D arrayarr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])print(arr[1, 2])print(arr[1, 1:3])
In the above code, arr[1, 2]
selects the element at row index 1
and column index 2
in the array.
On the other hand, 1
in arr[1, 1:3]
selects the second element in the array ([5, 6, 7, 8]
) and 1:3
selects the elements from index 1
to index 2
in the selected element (or array).
The output is following:
7[6 7]
There are some advanced indexing techniques in NumPy as well, including Boolean indexing and integer indexing.
Boolean indexing allows us to select elements based on a specific condition. It uses the syntax arr[con]
, where con
is the condition that needs to be checked:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5])boolean = arr[arr > 2]print(boolean)
Here, the condition arr > 2
selects all the elements in the array that are greater than 2
. Notably, the array name needs to be the same on the inside and outside, i.e., arr
in this case:
[3 4 5]
Integer indexing enables us to select elements using specific indices:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5])integer = arr[[0, 2, 4]]print(integer)
In the above code, the elements at index 0
, 2
and 4
in the array are selected:
[1 3 5]
Reshaping Arrays
Array reshaping is the process of altering the structure of an array without changing its data, useful for organizing and manipulating data more efficiently.
There are several functions that we can use for array reshaping, such as reshape()
, resize()
and ravel()
.
The reshape()
function reshapes an array without altering the original array:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5, 6])# Reshaping the arrayres = np.array(arr).reshape(2, 3)print(res)
In the above code, the array initially has one row and six columns. Then, the reshape()
function reshapes the array to have 2 rows and 3 columns instead. However, the original array stays unmodified as a new array is created for storing the result:
[[1 2 3][4 5 6]]
The resize()
function reshapes the original array to a 3x2 array:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5, 6])# Reshaping the arrayarr.resize(3, 2)print(arr)
In the above code, the function reshapes the array from one row and six columns to 3 rows and 2 columns and modifies the original array with the result:
[[1 2][3 4][5 6]]
The ravel()
function flattens a 2D or higher dimensional array into a 1D array without modifying the original array:
import numpy as np# Creating a 2D arrayarr = np.array([[1, 2, 3], [4, 5, 6]])# Flattening the array into a 1D arrayrav = arr.ravel()print(arr)
The output is following:
[1 2 3 4 5 6]
Array Iterating
Array iterating is the process of going through the elements in an array individually.
We can use either a standard Python loop or the nditer()
function to perform array iterating. Though both are useful, nditer()
iterates through arrays significantly faster than standard Python loops since nditer()
is written in C.
Here is an example of array iterating using standard Python loops:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5])# Iterating through the arrayfor i in arr:print(i)
Here is the output:
12345
The following example performs array iterating using nditer()
:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5])# Iterating through the arrayfor i in np.nditer(arr):print(i)
As we can see in the above code, the function takes the array to be iterated as its only argument.
The output is following:
12345
Concept Review and Next Steps
In this article, we explored the essential concepts of NumPy arrays. We highlighted their advantages over Python lists and learned about the various types of arrays. Moreover, we also covered basic mathematical operations and array manipulation techniques like indexing, slicing, reshaping and iterating.
To learn about sorting and unary functions in NumPy, check out this article on Codecademy.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Joining Arrays in NumPy for Beginners
Learn how to join NumPy arrays using functions like np.concatenate(), np.stack(), np.hstack(), np.vstack(), and np.dstack(). A beginner-friendly guide with examples. - Article
Sorting and Unary Operations in NumPy
Explore sorting and unary operations in NumPy arrays with examples for single and multi-dimensional data.
Learn more on Codecademy
- Skill path
Code Foundations
Start your programming journey with an introduction to the world of code and basic concepts.Includes 5 CoursesWith CertificateBeginner Friendly4 hours - Career path
Full-Stack Engineer
A full-stack engineer can get a project done from start to finish, back-end to front-end.Includes 51 CoursesWith Professional CertificationBeginner Friendly150 hours