How to randomly replace n number of columns and m number of rows with zero value from a 2d numpy array using Python

Aggy

I want to replace n randomly selected column value to zeros in m randomly selected rows for the purpose of adding noise to the dataset. So which means if my n = 3 and m = 5, it will replace zero to 3 randomly selected columns and 5 randomly selected rows.

For example if my n = 3(columns), m = 5(rows)

array([[10, 6, 1, 4, 8, 11, 12],
       [3, 2, 6, 7, 6, 2, 3],
       [1, 3, 2, 1, 10, 4, 9],
       [8, 1, 2, 4, 11, 12, 13],
       [3, 9, 5, 3, 4, 14, 4]])

one of the possible output will be

array([[10, 6, **0**, **0**, **0**, 11, 12],
       [**0**, 2, **0**, 7, **0**, 2, 3],
       [1, 3, 2, **0**, 10, **0**, **0**],
       [8, 1, 2, 4, **0**, **0**, **0**],
       [3, 9, **0**, 3, **0**, 14, 0]])

And if my n = 1(columns), m = 2(rows)

array([[10, 6, 1, 4, 8, 11, 12],
       [3, 2, 6, 7, 6, 2, 3],
       [1, 3, 2, 1, 10, 4, 9],
       [8, 1, 2, 4, 11, 12, 13],
       [3, 9, 5, 3, 4, 14, 4]])

one of the possible output will be

array([[10, **0**, 1, 4, 8, 11, 12],
       [3, 2, 6, 7, 6, 2, 3],
       [1, 3, 2, 1, **0**, 4, 9],
       [8, 1, 2, 4, 11, 12, 13],
       [3, 9, 5, 3, 4, 14, 4]])

Thanks in advance if anyone can help

cbo

For a general answer about adding noise in your data please refer to this SO answer : adding-noise-to-a-signal-in-python.

First create a reproducible example :

import numpy as np

n, m, high = 5, 7, 5
a = np.random.randint(low=0, high=high, size=n*m)
b = a.reshape(n, m).copy()
b

# array([[3, 0, 3, 1, 0, 3, 0],
#        [2, 3, 3, 3, 2, 0, 3],
#        [0, 2, 1, 4, 1, 4, 3],
#        [0, 4, 2, 3, 0, 1, 4],
#        [4, 4, 0, 2, 3, 4, 0]])

Then to modify values based on row or column number use :

n_rand = np.random.randint(n)
m_rand = np.random.randint(m)

b[n_rand,:] = -1
b[:,m_rand] = -1
b

# array([[ 3,  0,  3, -1,  0,  3,  0],
#        [ 2,  3,  3, -1,  2,  0,  3],
#        [ 0,  2,  1, -1,  1,  4,  3],
#        [ 0,  4,  2, -1,  0,  1,  4],
#        [-1, -1, -1, -1, -1, -1, -1]])

More generally to add noise to a signal, assuming rounding a normal distribution makes sense in your context, you could do :

noise = np.random.randn(n*m).round().reshape(n, m)
c = a.reshape(n, m)
print("noise :\n", noise)
print("\nstart matrix:\n", c ,"\n") 
np.add(c, noise)

# noise :
#  [[ 0.  1.  0.  0. -0.  0.  0.]
#  [-1.  0. -0.  1.  1.  0.  0.]
#  [-1. -2. -0.  0.  2. -1.  1.]
#  [-0. -0.  0.  0.  0.  2.  1.]
#  [-1. -1. -1.  0. -0.  0. -1.]]

# start matrix:
#  [[3 0 3 1 0 3 0]
#  [2 3 3 3 2 0 3]
#  [0 2 1 4 1 4 3]
#  [0 4 2 3 0 1 4]
#  [4 4 0 2 3 4 0]] 

# array([[ 3.,  1.,  3.,  1.,  0.,  3.,  0.],
#        [ 1.,  3.,  3.,  4.,  3.,  0.,  3.],
#        [-1.,  0.,  1.,  4.,  3.,  3.,  4.],
#        [ 0.,  4.,  2.,  3.,  0.,  3.,  5.],
#        [ 3.,  3., -1.,  2.,  3.,  4., -1.]])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How replace number with "-" randomly from a range of number using php

How to print all the elements of a 2d array in python with a different number of rows than columns?

Python: How to find the value of a number in a numpy array?

how to find number of rows and columns of 2D array to store prime numbers between 0 to 1000

How to assign all non-zero elements in each numpy column to a value in an array whose size is the same as the number of columns?

Replace rows in a numpy 2d array with rows from another 2d array

Is there a numpy (or Python) function to correlate each columns of 2D numpy array (n,m)

I'm using a mask to slice a numpy array, but the output is flattened. How do I retain the number of columns?

How can I get the number of columns and rows from 2D list

C++ How to replace rows and columns in 2D array?

Replace numpy array value on condition with random number

How best to randomly select a number of non zero elements from an array with many duplicate integers

How do I remove the first and last rows and columns from a 2D numpy array?

randomly generate a matrix with fixed number of non zero values using python

Replace columns in a 2D numpy array by columns from another2D array

How to make 2d complex number array in numpy?

Counting the number of rows based on the value of N number of columns

How do I randomly get a certain number of elements of a numpy array with at least one element from each class?

Replace "zero-columns" with values from a numpy array

Find number of non-zero elements adjacent to zeros in numpy 2D array

Determining the number of rows in a 2D array

how can I merge columns from 2 tables with different number of rows using SQLite?

Zero a row in a 2D array if the number zero exists in it

How to randomly sample a set number of rows from a dataframe with a preset condition?

How to count the number of a specific value within a 2D array

How to replace zero with a small number?

Create a 2D array with first number being the number of rows

How can I delete rows and columns that only contains zero's in an 2d array?

Replace a string numpy array with a number