Inserting NaN values into a matrix is a relatively straightforward task when using numpy in python. Examples:
Table of contents
Create a matrix with numpy
First, we must create the array that will hold our matrix (or alternatively load the matrix from a file):
import numpy as npA = np.random.uniform(10,80, size=(4,6))print(A)
Ouput
[[52.34830542 43.7300926 65.65912419 74.47707968 47.7363097 31.78605372][49.41123686 19.82268971 59.91408598 40.86920833 53.23834812 37.93559161][16.18419498 42.49772722 76.53306408 30.90572765 38.15287236 72.44956349][46.15969878 52.39722864 71.97596547 70.40800518 44.63824773 35.43923044]]
Randomly insert NaN
Then, we can use np.random.choice() to randomly select elements in the array and replace them with NaN values. For example:
n = 6index = np.random.choice(A.size, n, replace=False)A.ravel()[index] = np.nanprint(A)
Ouput
[[52.34830542 43.7300926 65.65912419 nan 47.7363097 nan][ nan 19.82268971 nan 40.86920833 53.23834812 37.93559161][16.18419498 42.49772722 nan 30.90572765 38.15287236 72.44956349][46.15969878 52.39722864 71.97596547 nan 44.63824773 35.43923044]]
Another example
Note:
print(type(np.nan))
gives
<class 'float'>
An example
import numpy as npA = np.random.randint(10,80, size=(5,2))A = A * 1.0print(A)
returns
[[52. 55.][14. 33.][19. 50.][67. 37.][16. 72.]]
and
n = 3index = np.random.choice(A.size, n, replace=False)A.ravel()[index] = np.nanprint(A)
returns for example
[[52. 55.][14. 33.][19. nan][67. nan][16. nan]]
References
| Links | Site |
|---|---|
| numpy.random.choice | numpy.org |
| ravel() | numpy.org |
