lundi 13 juillet 2015

Numpy: Replace random elements in an array

I already googled a bit and didn't find any good answers.

The thing is, I have a 2d numpy array and I'd like to replace some of its values at random positions.

I found some answers using numpy.random.choice to create a mask for the array. Unfortunately this does not create a view on the original array so I can not replace its values.

So here is an example of what I'd like to do.

Imagine I have 2d array with float values.

[[ 1., 2., 3.],
 [ 4., 5., 6.],
 [ 7., 8., 9.]]

And then I'd like to replace an arbitrary amount of elements. It would be nice if I could tune with a parameter how many elements are going to be replaced. A possible result could look like this:

[[ 3.234, 2., 3.],
 [ 4., 5., 6.],
 [ 7., 8., 2.234]]

I couldn't think of nice way to accomplish this. Help is appreciated.




Aucun commentaire:

Enregistrer un commentaire