Creating and Using NumPy Arrays - A Complete Guide

In this article, we will discuss how to create and use NumPy arrays. We will also go through various array operations, indexing, slicing, and reshaping with practical Python examples for data science.

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 array
arr = np.array([1, 2, 3, 4, 5])
# Squaring each element in the array simultaneously using vectorization
res = arr ** 2
print(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 array
arr = 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 dimensions
res = arr + 3
print(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 columns
arr = 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 columns
arr = 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 array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Using array attributes
print(arr.shape)
print(arr.size)
print(arr.dtype)
print(arr.ndim)

Here is the output:

(2, 3)
6
int64
2

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 arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Adding the arrays
add = arr1 + arr2
print(add)
# Subtracting ‘arr2’ from ‘arr1’
sub = arr1 - arr2
print(sub)
# Multiplying the arrays
mul = arr1 * arr2
print(mul)
# Dividing ‘arr1’ by ‘arr2’
div = arr1 / arr2
print(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 array
arr = 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 array
arr = 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 array
arr = 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 array
arr = 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 4in 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 array
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshaping the array
res = 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 array
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshaping the array
arr.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 array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Flattening the array into a 1D array
rav = 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 array
arr = np.array([1, 2, 3, 4, 5])
# Iterating through the array
for i in arr:
print(i)

Here is the output:

1
2
3
4
5

The following example performs array iterating using nditer():

import numpy as np
# Creating an array
arr = np.array([1, 2, 3, 4, 5])
# Iterating through the array
for 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:

1
2
3
4
5

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

Codecademy Team

'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 team