Creating and indexing arrays in numpy#
# initialization: make numpy available
import numpy as np
Creating ndarray#
Numpy provides a number of functions for creating regular ndarray, and we will show you a few important ones.
To create a linear sequence between a and b (b inclusive), for a length of n elements, use np.linspace(a, b, n):
np.linspace(0, 2, 10)
array([0. , 0.22222222, 0.44444444, 0.66666667, 0.88888889,
1.11111111, 1.33333333, 1.55555556, 1.77777778, 2. ])
To create a sequence between a and b (b exclusive), with a spacing d between consecutive elements, use np.arange(a, b, d):
np.arange(0, 2, 0.5)
array([0. , 0.5, 1. , 1.5])
To create an array of zeros, for a length of n, use np.zeros(n):
np.zeros(5)
array([0., 0., 0., 0., 0.])
For an array of ones, similarly use np.ones():
np.ones(4)
array([1., 1., 1., 1.])
More generally, to create a constant array, use np.full(n, a), where n gives the number of elements while a gives the constant value:
np.full(3, 2)
array([2, 2, 2])
If you want to join two numpy arrays, you can use np.concatenate():
x = np.array([1, 4, 9])
y = np.array([-2, 0, 2])
np.concatenate([x, y])
array([ 1, 4, 9, -2, 0, 2])
Basic indexing#
In short, an ndarray can be indexed and sliced in the same way a python list do. For example:
# define the array to be indexed
x = np.arange(1, 10.1, 0.5)
print(x)
[ 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5
8. 8.5 9. 9.5 10. ]
# extract the third (index = 2) element
x[2]
2.0
# extract the second (index 1) to the fifth (index 4) element
x[1:5]
array([1.5, 2. , 2.5, 3. ])
# extract every odd position (even index) elements
x[::2]
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
As with python list, indexing can be used to reassign values to an ndarray. And as in the case of python list, this modifies the numpy array in-place. For example, to change the 3rd element (index 2), we may do:
x[2] = -1
print(x)
[ 1. 1.5 -1. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5
8. 8.5 9. 9.5 10. ]
And just as in the case of python list, slices can be use for assignment. Furthermore (and unlike python list) both scalar and vector values are accepted on the right hand side (for vector values the shape of the RHS must match the shape of the LHS). Thus the following all works:
x[5:7] = -1
print(x)
[ 1. 1.5 -1. 2.5 3. -1. -1. 4.5 5. 5.5 6. 6.5 7. 7.5
8. 8.5 9. 9.5 10. ]
x[12:14] = [-4, -2]
print(x)
[ 1. 1.5 -1. 2.5 3. -1. -1. 4.5 5. 5.5 6. 6.5 -4. -2.
8. 8.5 9. 9.5 10. ]
x[-2:] = np.array([-0.5, -1.5])
print(x)
[ 1. 1.5 -1. 2.5 3. -1. -1. 4.5 5. 5.5 6. 6.5 -4. -2.
8. 8.5 9. -0.5 -1.5]
Advanced indexing#
In addition to indexing by an integer or by a slice object, an ndarray can also be indexed by a boolean ndarray. Say, for example, you want to extract the second and the fifth element of the array y below:
y = np.array([1, 7, -5, -2, 3])
We can create a Boolean array the same length as y, for which the entry is True only in the second and fifth positions:
selector = np.array([False, True, False, False, True])
We can now use the selector array to index the y array:
y[selector]
array([7, 3])
While the above example is a bit contrived, there are scenarios where indexing by boolean array is natural. Very commonly, you may want to select elements in an array whose values satisfy certain condition. Say you want to extract all the negative elements of y. You can then do:
y[y < 0]
array([-5, -2])
To break this into steps, in the above expression you first create an unnamed, temporary Boolean array y < 0, which you then use it to index y. The result is exactly the same as extracting negative elements from y
In addition to indexing via boolean array, you can also index an ndarray using an integer ndarray. The output then consists of values whose indices are as specified by the indexing array. As a concrete example, to extract the second and fifth element of y, we may also do:
y[np.array([1, 4])]
array([7, 3])