10 Ways to Initialize a Numpy Array (How to create numpy arrays)
In Python, matrix-like data structures are most commonly represented with numpy
arrays. The numpy
Python package has been well-developed for efficient computation of matrices. The first step to using numpy
arrays is to initialize, or create, an array. In Python, there are many ways to create a numpy
array. In this article, I’ll demonstrate how to create numpy
arrays in ten different ways.
Ten common ways to initialize (or create) numpy
arrays are:
- From values (
numpy.array([value, value, value])
) - From a Python list or tuple (
numpy.asarray(list)
) - Empty array (
numpy.empty(shape)
) - Array of ones (
numpy.ones(shape)
) - Array of zeros (
numpy.zeros(shape)
) - Array of any value (
numpy.full(value)
) - Copy an array (
numpy.copy(array)
) - Sequential or evenly spaced values (
numpy.arange
,numpy.linspace
,numpy.geomspace
) - Array of random values (
numpy.random
) - Array of repeaded values (
numpy.repeat
)
At first, numpy
will have a steep learning curve, but stick with it. Learning numpy
is a skill that will greatly improve your Python programming.
How to Create Numpy Arrays in 10 Different Ways
The very first thing we must do is import numpy
.
import numpy as np
Array from Values¶
The most basic way to create a numpy
array is to specify the exact values you would like to include in the array. This is done with the numpy.array()
function. The desired values are passed to numpy.array()
as lists defined by square brackets ([]
).
Below, I’ve specified values to intialize a simple 1-dimension numpy
array.
np.array([8, 3, 13, 1])
array([ 8, 3, 13, 1])
More complicated, multidimensional arrays can also be created. Below, I’ve created a 2D array.
np.array([[4, 23, 98, 7],
[38, 54, 6, 18],
[5, 48, 4, 56]])
array([[ 4, 23, 98, 7], [38, 54, 6, 18], [ 5, 48, 4, 56]])
Just make sure you have the same number of values in each row. If you don’t you’ll get an error like this.
np.array([[1, 2, 3, 4], # first row as 4 values
[1, 2, 3]]) # second row only has 3 values
<ipython-input-4-3bc2f09689e3>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. np.array([[1, 2, 3, 4], # first row as 4 values
array([list([1, 2, 3, 4]), list([1, 2, 3])], dtype=object)
You will often find it useful to save the array as a variable. Here, a 2D array is saved to the variable a
.
a = np.array([[4, 23, 98, 7],
[38, 54, 6, 18],
[5, 48, 4, 56]])
a
array([[ 4, 23, 98, 7], [38, 54, 6, 18], [ 5, 48, 4, 56]])
numpy
Array from a Python List (or tuple)¶
Converting values stored in another Python data structure, like a list or tupe, to a numpy
array can be done with numpy.asarray()
. Pass the list (or other data structure) to numpy.asarray()
and a numpy
array is returned.
These two examples demonstrate how Python lists can be converted to 1D and 2D numpy
arrays.
First, a 1D array.
my_list = [2, 45, 3, 2, 56]
my_array = np.asarray(my_list)
print('The intial object type of', type(my_list), 'was converted to ', type(my_array))
my_array
The intial object type of <class 'list'> was converted to <class 'numpy.ndarray'>
array([ 2, 45, 3, 2, 56])
As you can see from the output above, the intial Python list was converted into a numpy
array with the same values as the original.
Now, let’s take a look at a 2D array.
my_2d_list = [[445, 2, 442, 56, 23], [64, 556, 29, 34, 23]]
my_2d_array = np.asarray(my_2d_list)
my_array
array([ 2, 45, 3, 2, 56])
Create an Empty Array¶
If you don’t have data to create a numpy
array then you can create an empty array. In truth, and empty array isn’t actually empty, it just contains very small, meaningless values.
To create and empty numpy
array, call the numpy.empty()
function and pass it a shape tuple. The code below demonstrates how this is done. Notice that the output array does contain values.
a_shape = (3, 4) # 3 rows and 4 columns
a = np.empty(a_shape)
a
array([[9.30800404e-312, 2.47032823e-322, 0.00000000e+000, 0.00000000e+000], [4.00766127e+175, 4.47032019e-038, 6.87008009e-091, 9.14796435e-071], [1.00554692e-047, 4.47042917e-033, 3.99910963e+252, 1.93300839e+184]])
More information and examples for numpy.empty
can be found in the numpy documentation.
Array of Ones¶
Use the numpy.ones()
function to create a numpy
array of a specified shape that is is filled with the value one (1). The numpy.ones
function is very similar to numpy.empty
. You just need to pass it the shape of the output array.
The code below creates an array with 3 rows and 4 columns. Each element in the array contains the value 1.
You can change the value type (integer, double, byte, etc) by passing the dtype
argument to the function.
a_shape = (3, 4) # 3 rows and 4 columns
a = np.ones(a_shape)
a
array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]])
More information and examples for numpy.ones
can be found in the numpy documentation.
Array of Zeros¶
Use the numpy.zeros()
function to create a numpy
array of a specified shape that is filled with the value zero (0). The numpy.zeros
function is nearly the same as numpy.ones
and numpy.empty
except that the resulting array is filled the value of zero. Once again, you just need to pass a shape tuple to the numpy.zeros
function.
a_shape = (3, 4) # 3 rows and 4 columns
a = np.zeros(a.shape)
a
array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
More information and examples for numpy.zeros
can be found in the numpy documentation
Array of Any Value¶
With numpy.full()
you can create an array where each element contains the same value. The numpy.full
function is very similar to the previous three functions (numpy.empty
, numpy.zeros
, and numpy.ones
) but it requires two arguments, the shape of the resulting array and the fill value.
The code below creates and array with 3 rows and 4 columns where each element contains the value 38.7.
a_shape = (3, 4) # 3 rows and 4 columns
fill_value = 38.7
a = np.full(a_shape, fill_value)
a
array([[38.7, 38.7, 38.7, 38.7], [38.7, 38.7, 38.7, 38.7], [38.7, 38.7, 38.7, 38.7]])
More information and examples for numpy.full
can be found in the numpy documentation.
Copy an Existing numpy
Array¶
a = np.array([8, 3, 13, 1])
a_copy = np.copy(a)
a_copy
array([ 8, 3, 13, 1])
Create an Array of Sequential (or evenly spaced) Values¶
There are several ways to create arrays of sequential or evenly spaced values with numpy
. In this exaple I’ll go over how to do this numpy.arange
for evenly spaced values over a range, numpy.linspace
for a certain number of evenly spaced values between two endpoints, and numpy.geomspace
for evenly spaces values on a log scale.
numpy.arange¶
Let’s start with numpy.arange
. numpy.arange
returns an array of values over a specified interval. There are a few ways to use it. In this first example, we’ll create a 1D array with 9 elements by passing only the number 9 to arange
np.arange(9)
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
As you can see, an array with 9 values (0-8) was returned. This array can be reshaped in place using numpy.reshape
after the call to arange
. Like this.
np.arange(9).reshape((3, 3))
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
Now we have a 2D array with the same number of elements and same values but in a different shape. When you do this be sure the product of the shape tuple equals the number of elements in the array, or you will get an error.
With numpy.arange
we can also specify the interval for the values to be generated from. For example, if I want to generate an array with values 12-19 I would use the following code, which specifies a start value of 12 and an end value (non-inclusive) of 20.
np.arange(12, 20)
array([12, 13, 14, 15, 16, 17, 18, 19])
Finally, a step value can be passed to arange
so that not all values in the range are included. For example, the following code will return an array with all even values in the range 20-40.
np.arange(20, 41, 2)
array([20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40])
More information and examples for numpy.arange
can be found in the numpy documentation
numpy.linspace
¶
Similar to numpy.arange
, numpy.linspace
generates an interval of values. The difference is that numpy.linspace
will generate a specified number of values over an interval. I’ll demonstrate this with an example. To generate 11 evenly spaced values on the interval 0-10 we would use the following code.
np.linspace(0, 10, 11)
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
Let’s consider one more example. This time we’ll generate 10 values on the interval 22-27.
np.linspace(22, 27, 10)
array([22. , 22.55555556, 23.11111111, 23.66666667, 24.22222222, 24.77777778, 25.33333333, 25.88888889, 26.44444444, 27. ])
We can also exclude the endpoint from the interval. This will give us values that are multiples of 0.5.
np.linspace(22, 27, 10, endpoint=False)
array([22. , 22.5, 23. , 23.5, 24. , 24.5, 25. , 25.5, 26. , 26.5])
More information and examples for numpy.linspace
can be found in the numpy documentation.
numpy.geomspace¶
numpy.geomspace
uses the same arguments as numpy.linspace
, but produces values evenly spaced on a log scale. Let’s take a look at a couple of examples.
np.geomspace(1, 1000, 4)
array([ 1., 10., 100., 1000.])
np.geomspace(1, 256, 9)
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
Notice that numpy.geomspace
automatically infers the the shortest log base. More information and examples for numpy.geomspace
can be found in the numpy documentation
Create an Array of Random Values¶
First lets create a random number generator, rng
.
rng = np.random.default_rng()
We can now use rng
to generate random values. The following code will generate a random float between 0.0 and 1.0. First we’ll generate a single random value. Notice that this line of code will generate a new number each time it is run.
rng.random()
0.5849737472063717
An array of random values can be generated by passing an integer to random
, like this.
rng.random(10)
array([0.49100274, 0.79170392, 0.12288195, 0.4785899 , 0.3978777 , 0.0933146 , 0.19365217, 0.73930889, 0.68300462, 0.18180961])
And the array can be reshaped like this.
rng.random(9).reshape((3,3))
array([[0.58577718, 0.62177914, 0.25939382], [0.30153055, 0.5661081 , 0.03098927], [0.34069052, 0.27714148, 0.68044703]])
To generate random integers we’ll take a slightly different approach. Random integers can be generated by passing an interval start, interval end, and number of values to the integers
method. The following code will generate 5 random integers between 0 and 100.
rng.integers(0, 100, 5)
array([ 5, 32, 72, 5, 77], dtype=int64)
These are some basic example of random number generation with numpy
. More information and examples for random number generation with numpy can be found in the numpy documentation.
Create an Array of Repeated Values¶
Finally, we’ll create an array of a repeated value or pattern with numpy.repeat
. There are many ways to apply numpy.repeat
, let’s walk through a few examples.
First, is a simple example where we’ll create an array be repeating the number 13 four times, like this.
np.repeat(13, 4)
array([13, 13, 13, 13])
We can also repeat each number in an array-like object a specified number of times.
np.repeat([7, 8, 9], 3)
array([7, 7, 7, 8, 8, 8, 9, 9, 9])
Alternatively, each element in the array can be repeated a different number of times.
np.repeat([7, 8, 9], [2, 3, 4])
array([7, 7, 8, 8, 8, 9, 9, 9, 9])
More information and examples for numpy.repeat
can be found in the numpy documentation.
Final Thoughts
There are many different ways to create numpy
arrays. This article has demonstrated what I think are the most common and useful ways to initialize numpy
arrays. If one of these methods doesn’t meet your needs, consult the numpy
documentation for more array creation options.
Whether you’re looking to take your GIS skills to the next level, or just getting started with GIS, we have a course for you! We’re constantly creating and curating more courses to help you improve your geospatial skills.
All of our courses are taught by industry professionals and include step-by-step video instruction so you don’t get lost in YouTube videos and blog posts, downloadable data so you can reproduce everything the instructor does, and code you can copy so you can avoid repetitive typing