May 30, 2021 Article blog
First of all, it is emphasized that the where() function returns different values for different inputs.
1, when the array is a one-dimensional array, the returned value is a one-dimensional index, so there is only one set of index arrays;
2. When an array is a two-dimensional array, the array value that meets the criteria returns a location index of the value, so there are two sets of index arrays to represent the location of the value, as illustrated below:
import numpy as np
a=np.reshape(np.arange(20),(4,5))
a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
b = np.where(a>10)
b
(array([2, 2, 2, 2, 3, 3, 3, 3, 3], dtype=int64), array([1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=int64))
b[0][:]
array([2, 2, 2, 2, 3, 3, 3, 3, 3], dtype=int64)
b[0]
array([2, 2, 2, 2, 3, 3, 3, 3, 3], dtype=int64)
b[1]
array([1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=int64)
a is a two-dimensional array, b is the index returned, the index is divided into row index and column index two parts, b is the row index, b is the column index.
His function is to repeat an array. F or example, tile (A, n), the function is to repeat array A n times to form a new array. example:
import numpy as np
a = [1,2,3]
b = np.tile(a, (1, 4))
b
array([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]])
b = np.tile(a, 4)
b
array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
d = np.tile(a, (2, 4))
d
array([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]])
As we can see from the example above, where b s np.tile (a, (1, 4)) generates a two-dimensional array, while b s np.tile (a, 4) produces a one-dimensional array that repeats a four times.