December 2017
Intermediate to advanced
386 pages
10h 42m
English
To create arrays with repeated elements, we use the repeat() function, as shown in the following example:
np.repeat(1.2, 3)
This repeats the 1.2 value three times, resulting in the following array:
array([ 1.2, 1.2, 1.2])
The argument to be repeated can be itself an array, as shown in the following example:
x = np.array([[1,2,3],[4,5,6]])y = np.repeat(x, 2)
This will store, in the y variable, the following output array:
array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
Notice that this version of repeat() flattens the array, that is, it returns a one-dimensional ndarray. If we want to prevent flattening, we can use the axis option, demonstrated as follows:
y = np.repeat(x, 2, axis=0)
This produces the array: ...
Read now
Unlock full access