Creating and Using NumPy Arrays - A Complete Guide
What is NumPy in Python?
NumPy, short for Numerical Python, is a powerful Python library for scientific computing. It enables efficient operations on large, multi-dimensional arrays and offers a wide range of mathematical functions. Widely used in data science and machine learning, NumPy significantly outperforms Python lists by using vectorization, broadcasting, and optimized memory handling.
In this guide, we’ll explore the benefits of using NumPy over Python lists, creating 1D, 2D, and 3D arrays, performing arithmetic operations, and applying indexing, slicing, reshaping, and iteration techniques in NumPy.
Learn Statistics with NumPy
Learn about NumPy, a Python library used to store arrays of numbers, organize large amounts of data, and perform statistical calculations. Try it for freeWhy is NumPy preferred over Python lists?
Faster computation with vectorization
NumPy’s primary strength lies in its speed and efficiency, which are achieved through vectorization. Vectorization allows operations 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)
The output of this code will be:
[ 1 4 9 16 25]
In this example, vectorization allows NumPy to square all the elements simultaneously instead of individually.
Memory efficiency
NumPy uses contiguous memory blocks, storing arrays in adjacent memory locations, allowing for quicker access and modifications. This efficient memory usage for large datasets reduces the risk of blockages, as the data is handled more efficiently than in Python lists.
Built-in functions and broadcasting
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)
The output of this code is:
[[4 5 6][7 8 9]]
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.
Types of NumPy arrays
0D arrays in NumPy
Let’s start with 0D arrays. A 0D array, also known as a scalar, is an array that contains a single element. Here is an example:
Example of a 0D array:
import numpy as nparr = np.array(5)
1D arrays in NumPy
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 as a row or column.
Example of a 1D array:
import numpy as nparr = np.array([1, 2, 3])
2D arrays in NumPy
A 2D array, also known as a matrix, is an array that contains a table of elements with rows and columns.
Example of a 2D array:
import numpy as nparr = np.array([[1, 2], [3, 4]])
3D arrays in NumPy
A 3D array, also known as a tensor, is an array that contains multiple tables of elements in it.
Example of a 3D array:
import numpy as nparr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
A NumPy array can have more than three dimensions. However, for this article, we’ll focus on 3D arrays.
But how do we create these arrays? Are there any built-in functions for them? Let’s check.
How to create NumPy arrays?
NumPy offers several built-in functions to generate arrays with different characteristics. Here’s a comparison of the most commonly used functions:
Function | Purpose | Example |
---|---|---|
np.array() |
Convert existing data | np.array([1, 2, 3]) |
np.zeros() |
Create array filled with zeros | np.zeros((2, 3)) |
np.ones() |
Create array filled with ones | np.ones((2, 3)) |
np.arange() |
Create sequence of numbers | np.arange(0, 10, 2) |
Let’s explore each function with practical examples:
Creating arrays with np.array()
in NumPy
The np.array()
function is the most common way to create a NumPy array. It converts a Python list, tuple, or sequence into a NumPy array object.
The following example creates arrays of different dimensions using the np.array()
function:
import numpy as np# Creating a 1D arrayarr_1d = np.array([1, 2, 3])print(arr_1d)# Creating a 2D arrayarr_2d = np.array([[1, 2], [3, 4]])print(arr_2d)# Creating a 3D arrayarr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])print(arr_3d)
The output of this code will be:
[1 2 3][[1 2][3 4]][[[1 2][3 4]][[5 6][7 8]]]
The np.array()
function also allows you to specify the data type using the dtype
argument.
Example:
import numpy as np# Create a float arrayarr = np.array([1, 2, 3], dtype=float)print(arr)
The output of this code will be:
[1. 2. 3.]
The np.array()
function is ideal when you need complete control over the data structure and type, especially at the beginning of any numerical or data science task.
Using np.zeros
to create an array
The np.zeros()
function creates an array of the specified shape, filled with 0’s.
Example:
import numpy as np# Creating an array having 2 rows and 2 columnsarr = np.zeros((2, 2))print(arr)
The output is as follows:
[[0. 0.][0. 0.]]
Using np.ones
to create an array
The np.ones()
function creates an array of the specified shape, filled with 1’s.
Example:
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.]]
Using np.arange
to create an array
The np.arange()
function creates an array containing evenly spaced values in a specified range.
Example:
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 this code, the np.arrange()
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 have an equal difference of 1.
In the next section, we’ll learn about a few array attributes we can use in NumPy.
Understanding 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 element).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 arithmetic operations with NumPy arrays
NumPy supports fast, element-wise arithmetic operations on arrays using vectorization. This allows you to perform calculations without explicit loops, making your code both cleaner and faster. The most common operations include:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Exponentiation (
**
) - Modulus (
%
)
Since these operations are applied across entire arrays simultaneously, they are highly efficient for numerical computing tasks.
Here is an example that applies some of these arithmetic 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)
This 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.
Array manipulation techniques in NumPy
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 this code, we first used indexing (arr[0]
) to access the array’s element at index 0
. Then, we used slicing (arr[1:3]
) to access the elements from index 1
to index 3
in the array (excluding index 3
).
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 this 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 as follows:
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. It is useful for organizing and manipulating data more efficiently.
Several functions can be used for array reshaping, such as reshape()
, resize()
, and ravel()
.
Using reshape()
in NumPy arrays
The reshape()
function reshapes an array without altering the original array.
Example:
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)
The output of this code is:
[[1 2 3][4 5 6]]
In this code, the array initially has one row and six columns. Then, the reshape()
function reshapes the array to have two rows and three columns instead. However, the original array stays unmodified, as a new array is created to store the result.
Using resize()
in NumPy arrays
The resize()
function changes the shape and size of an array.
Example:
import numpy as np# Creating an arrayarr = np.array([1, 2, 3, 4, 5, 6])# Reshaping the arrayarr.resize(3, 2)print(arr)
This code will result in the following output:
[[1 2][3 4][5 6]]
Here, the function reshapes the array from one row and six columns to 3 rows and 2 columns and modifies the original array.
Using ravel()
in NumPy arrays
The ravel()
function flattens a 2D or higher-dimensional array into a 1D array without modifying the original array.
Example:
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 as follows:
[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 this code, the function takes the array to be iterated as its only argument.
The output is as follows:
12345
Conclusion
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 build on what you’ve learned about NumPy arrays and explore how they’re used in statistics and data analysis, check out the Learn with NumPy course on Codecademy.
Frequently asked questions
1. What should I use if I want to generate random arrays in NumPy?
You can use functions from numpy.random
, such as np.random.rand()
, np.random.randn()
, or np.random.randint()
to create arrays with random values for simulations or testing models.
2. Can NumPy arrays hold mixed data types like Python lists?
No, NumPy arrays are homogeneous, meaning all elements must be of the same data type. If you try to include mixed types, NumPy will upcast them to a common compatible type, often converting everything to strings or floats.
3. How does broadcasting work in NumPy?
Broadcasting allows NumPy to perform arithmetic operations on arrays of different shapes by automatically expanding one array to match the shape of the other without copying data. It’s a powerful feature for writing efficient vectorized code.
4. When should I use reshape()
vs. resize()
in NumPy?
Use reshape()
when you want to return a new reshaped array without altering the original. Use resize()
when you need to change the shape of the original array in place. Both must match the total number of elements.
5. Is NumPy suitable for large-scale data processing?
Yes, NumPy is highly optimized for numerical computations and works well with large datasets. However, for extremely large-scale tasks, it’s often used alongside libraries like pandas or Dask for additional scalability and functionality.
'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
How to Split Arrays in NumPy?
Learn how to split NumPy arrays using functions like `np.split()`, `np.array_split()`, `np.hsplit()`, `np.vsplit()`, and `np.dsplit()`. A beginner-friendly guide with practical examples. - Article
Sorting and Unary Operations in NumPy
Explore sorting and unary operations in NumPy arrays with examples for single and multi-dimensional data. - Article
How to Use np.concatenate() in NumPy (With Examples and Comparisons)
Learn how to use `np.concatenate()` in Python to join NumPy arrays efficiently. Explore syntax, real-world examples, and key differences with `np.stack()`, `np.hstack()`, `np.vstack()`, and `np.dstack()`.
Learn more on Codecademy
- Course
Learn Statistics with NumPy
Learn about NumPy, a Python library used to store arrays of numbers, organize large amounts of data, and perform statistical calculations.With CertificateIntermediate4 hours - Free course
Learn JavaScript: Arrays and Loops
Create and manipulate arrays and execute efficient repetitions using loops to develop meaningful programs.Beginner Friendly3 hours - Free course
Learn C: Arrays and Strings
Sharpen your programming skills by learning how to create, assess, and modify arrays and strings in C.Beginner Friendly2 hours
- What is NumPy in Python?
- Why is NumPy preferred over Python lists?
- Types of NumPy arrays
- Creating an array containing values from 0 - 2 (3 excluded)
- Understanding NumPy array attributes
- Performing basic arithmetic operations with NumPy arrays
- Array manipulation techniques in NumPy
- Conclusion
- Frequently asked questions