I have a little problem working with the same big data. But for now, let's assume that I have a NumPy array filled with zeros
>>> x = np.zeros((3,3)) >>> x array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
Now I want to change some of these zeros with specific values. I gave an index of the cells I want to change.
>>> y = np.array([[0,0],[1,1],[2,2]]) >>> y array([[0, 0], [1, 1], [2, 2]])
And I have an array with the desired (for random) numbers, as follows
>>> z = np.array(np.random.rand(3)) >>> z array([ 0.04988558, 0.87512891, 0.4288157 ])
So now I thought I could do the following:
>>> x[y] = z
But how does it fill the entire array like this
>>> x array([[ 0.04988558, 0.87512891, 0.4288157 ], [ 0.04988558, 0.87512891, 0.4288157 ], [ 0.04988558, 0.87512891, 0.4288157 ]])
But I was hoping to get
>>> x array([[ 0.04988558, 0, 0 ], [ 0, 0.87512891, 0 ], [ 0, 0, 0.4288157 ]])
EDIT
Now I used the diagonal index, but in this case my index is not just diagonal. I was hoping for the following work:
>>> y = np.array([[0,1],[1,2],[2,0]]) >>> x[y] = z >>> x >>> x array([[ 0, 0.04988558, 0 ], [ 0, 0, 0.87512891 ], 0.4288157, 0, 0 ]])
But it fills the whole array as above