NumPy (short for numerical Python) is a useful library for mathematics and data science, specifically for working with arrays of data.
In this post I want to detail, with examples, some common ways to create NumPy arrays. These range from creating arrays with Python lists to using built-in NumPy commands for standard arrays (like ones and zeros).
This post does not cover matrix operations like multiplication, splicing, etc. Those are reserved for other posts. I want to solely focus on methods for creating NumPy arrays.
Since I want this post to be comprehensive, if you have a specific way of creating NumPy arrays feel free to reach out using this contact form. You will receive credit and a backlink for contributing.
To begin, install NumPy using ‘pip install numpy’.
pip install numpy
Then, in your Notebook or Python file, import NumPy as the variable np.
import numpy as np
If you’re using Conda, use this installation guide from the NumPy documentation.
Creating Arrays in NumPy
There are so many ways to create arrays in NumPy, your eyes will cross. Let’s go over them one at a time. This won’t cover all the ways, but these are at least the most useful.
Creating 1-D Arrays
To create a 1-dimensional (1-D) NumPy array, use the np.array() function.
a = np.array([1, 2, 3])
# Output:
[1 2 3]
Note that the argument passed into np.array is a simple Python list. We could just as easily write the following:
a_list = [i for i in range(1,4)]
a = np.array(a_list)
# Output:
[1 2 3]
Of course, NumPy has an easier way to do the same thing. Using the np.arange() function you can create an array of numbers from a start value up to, but not including, an end value.
a = np.arange(start=1, stop=4, step=1)
# Or, more succinctly:
a = np.arange(1, 4)
# Output:
[1 2 3]
If you have a range of values in mind, but need an array that cuts the range in an even n number of times, then you can use the np.linspace() function.
a = np.linspace(start=0, stop=50, num=6)
# Or, more succintly:
a = np.linspace(0, 50, 6)
# Output:
[0. 10. 20. 30. 40. 50.]
Note how the output is expressed as a floating point number. You can have NumPy output the datatype you prefer using the dtype parameter.
a = np.linspace(0, 50, 6, dtype=int) # prints a list of ints
a = np.linspace(0, 50, 6, dtype=str) # prints a list of strings
Prepackaged NumPy Arrays
There are some NumPy functions that create arrays with specific characteristics. For example:
- np.ones()
- np.zeros()
- np.random.rand()
The first two are pretty straightforward. Pass in an integer value and you’ll receive a 1-dimensional array of ones or zeros.
print(np.ones(5))
# Output: [1. 1. 1. 1. 1.]
print(np.zeros(5))
# Output: [0. 0. 0. 0. 0.]
If you’d like to fill a 1-dimenstional array of size n with n randomly generated numbers between 0 and 1, then use the np.random.rand() function. This function produces uniformly distributed random samples from 0 to n (not including n).
print(np.random.rand(5))
# Example Output: [0.59836903 0.2973207 0.26984867 0.30717907 0.63486949]
If you’d like to produce random samples between 0 and a different number (say, 100) then you can perform an element-wise multiplication on the resulting array.
In NumPy, an element-wise multiplication is done by simply multiplying the array by a number.
a = np.random.rand(5)
a = 100 * a
print(a)
# Example Output: [55.28802359 44.4521426 48.20100507 29.64656067 26.23718413]
Creating 2-D Arrays
There are a few ways to define a 2-dimensional array in NumPy using Python. The first is by explicitly defining the 2D array as a list of lists and then passing that into the np.array() function.
a_list = [[1,2,3],[4,5,6]]
a = np.array(a_list)
print(a)
# Output:
# [[1 2 3]
# [4 5 6]]
You can also “reshape” a 1D array into a 2D array using the np.reshape() function. The first argument is the 1D array you want to convert. The second argument is a tuple representing the number of rows and columns in the 2D array, respectively.
a_1D = np.array([1, 2, 3, 4, 5, 6])
a_2D = np.reshape(a_list, (3, 2))
print(a_2D)
# Output:
# [[1 2]
# [3 4]
# [5 6]]
Article Corrections and Contributions
Did you know you can contribute to Quantastic Research articles? For contributions, fill out the Article Contribution Form. For corrections, see the Article Correction Form. Use the standard Contact Form for all other inquiries.