How to randomly insert NaN in a matrix with NumPy in Python ? (original) (raw)

Last Updated : 21 May, 2021

Prerequisites: Numpy

In this article, let’s see how to generate a Python Script that randomly inserts Nan into a matrix using Numpy. Given below are 3 methods to do the same:

Method 1: Using ravel() function

ravel() function returns contiguous flattened array(1D array with all the input-array elements and with the same type as it). A copy is made only if needed.
Syntax :

numpy.ravel(array, order = 'C')

Approach:

Example 1:

Python3

import numpy as np

import pandas as pd

n = 3

data = np.random.randn( 5 , 5 )

index_nan = np.random.choice(data.size, n, replace = False )

data.ravel()[index_nan] = np.nan

print (data)

Output:

Example 2: Adding nan to but using randint function to create data. For using np.nan in randint function we must first convert the data into float as np.nan is of float type.

Python3

import numpy as np

n_b = 5

data_b = np.random.randint( 10 , 100 , size = ( 5 , 5 ))

data_b = data_b * 0.1

index_b = np.random.choice(data_b.size, n_b, replace = False )

data_b.ravel()[index_b] = np.nan

print (data_b)

Output:

Method 2: Creating mask

Creating a mask of boolean and applying that mask to the dataset can be one approach to produce the required result.

Approach:

Example :

Python3

import numpy as np

X = 10

Y = 5

N = 15

data = np.random.randn(X, Y)

mask = np.zeros(X * Y, dtype = bool )

mask[:N] = True

np.random.shuffle(mask)

mask = mask.reshape(X, Y)

data[mask] = np.nan

print (data)

Output:

Method 3: Using insert()

Using insert() function will convert a whole row or a whole column to NaN. This function inserts values along the mentioned axis before the given indices.
Syntax :

numpy.insert(array, object, values, axis = None)

Approach:

Example:

Python3

import numpy as np

a = np.array([( 13.0 , 1.0 , - 47.0 ), ( 12.0 , 3.0 , - 47.0 ), ( 15.0 , 2.0 , - 44.0 )])

np.insert(a, 2 , np.nan, axis = 0 )

np.insert(a, 2 , np.nan, axis = 1 )

Output: