python - how to replace an array containing indices with values from another array? -
python - how to replace an array containing indices with values from another array? -
i have array b containing indices of array a. want insert values of array c in array b same indices.
import numpy np a1=np.array([[1, 3, 5, 2, 3],[7, 6, 5, 2, 4],[2, 0, 5, 6, 4]]) a=a1.argsort()[:,:2]
## create array indices of 2 smallest values of a1
[[0 3] [3 4] [1 0]] b=np.array([[1],[2],[3],[4],[5],[6]])
now want replace value 0 in 1 in b ; 3 4 , on
i tried using:
[a[index]]=b[index]
but not right way array handles these indices values
please help
if understood correctly, can utilize flattened version of a
index b
:
result = b.ravel()[a.ravel()] [1, 4, 4, 5, 2, 1]
if need in same dimensions a
can reshape it:
result = result.reshape(a.shape) [[1, 4] [4, 5] [2, 1]]
python python-2.7 numpy
Comments
Post a Comment