Creating an array by repeating elements

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:

array([[1, 2, 3],
[1, 2, 3],
[4, 5, 6],
[4, 5, 6]])

To understand this output, it is important to understand the notion of axis in NumPy. This notion applies to general n-dimensional arrays, but, to keep the examples simple, let's consider the case of a 3 x 2 two-dimensional array. The array can be visualized as a table, as follows:

Column 0
Column 1
Column 3
Row 0
x[0,0]
x[0,1]
x[0,2]
Row 1
x[1,0]
x[1,1]
x[1,2]

 

Each element in the array is indexed by a pair of integers, [i,j]. We say that i is the index along axis 0, and j is the index along axis 1. Pictorially, axis 0 goes vertically in the downward direction, across the rows, and axis 1 goes horizontally to the right, across the columns.

If we move along axis 0, the array consists of two row vectors, as follows:

Row 0 x[0,0] x[0,1] x[0,2]
Row 1 x[0,0] x[0,1] x[0,2]

 

Accordingly, the np.repeat(x, 2, axis=0) function call takes each of the rows of the x array, copies it twice, and stacks all resulting rows vertically. Similarly, we could have called repeat with axis=1, as in the following example:

y = np.repeat(x, 2, axis=1)

In this example, the x array is interpreted as a sequence of column vectors, and each column is repeated twice, producing the following result:

array([[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6]])

One final option in repeat() is to use a list to specify the number of repetitions of each row/column, as in the following example:

y = np.repeat(x, [2, 1, 2], axis=1)

Since we are specifying axis=1, the array x is viewed as a sequence of columns. Then, column 0 is repeated twice, column 1 is repeated once, and column 2 is repeated twice, resulting the following array:

array([[1, 1, 2, 3, 3],
[4, 4, 5, 6, 6]])

Notice that by setting the number of repetitions to 0, it is possible to use repeat() to delete the rows/columns of an array, as shown in the following example:

x = np.array([[1,2,3,4],
[5,6,7,8]])
y = np.repeat(x, [1,0,1,0], axis=1)

Since the number of repetitions for columns 1 and 3 is equal to zero, these columns will be removed from the array, resulting in the following output:

array([[1, 3],
[5, 7]])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset