I have a 2D numpy array which contains my values (some of them can be NaN). I want to remove the 30% of the non-NaN values and replace them with the mean of the array. How can I do so? What I tried so far:
def spar_removal(array, mean_value, sparseness):
array1 = deepcopy(array)
array2 = array1
spar_size = int(round(array2.shape[0]*array2.shape[1]*sparseness))
for i in range (0, spar_size):
index = np.random.choice(np.where(array2 != mean_value)[1])
array2[0, index] = mean_value
return array2
But this is just picking the same row of my array. How can I remove from all over the array? It seems that choice works only for one dimension. I guess what I want is to calculate the (x, y) pairs that I will replace its value with mean_value.
removeandreplace. Remove implies, at least me, reducing the shape of the array, e.g. from a (100,100) to (90,90) or some such value. While it is easy to remove a whole row or column, removing individual elements is hard without making the array ragged.