How to randomly insert NaN in a matrix with NumPy in Python ? Last Updated : 21 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Import moduleCreate dataChoose random indices to Nan value to.Pass these indices to ravel() functionPrint data Example 1: Python3 import numpy as np import pandas as pd # number of nan we want to add It will insert 3 nan values to the data..... n = 3 # creating dataset data = np.random.randn(5, 5) # choosing random indexes to put NaN index_nan = np.random.choice(data.size, n, replace=False) # adding nan to the data. 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 # number of nan we want to add It will insert 3 nan values to the data..... n_b = 5 # creating dataset data_b = np.random.randint(10, 100, size=(5, 5)) # converting the data to float as nan is also of type float data_b = data_b*0.1 # choosing random indexes to put NaN index_b = np.random.choice(data_b.size, n_b, replace=False) # adding nan to the data. 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: Import moduleCreate dataCreate maskShuffle the mask to randomly apply Nan valuesApply the mask to the dataPrint data Example : Python3 import numpy as np # creating dataset X = 10 Y = 5 N = 15 data = np.random.randn(X, Y) # making a array randomly of same size as data of bool type mask = np.zeros(X*Y, dtype=bool) # marking first n indexes as true mask[:N] = True # shuffling the mask np.random.shuffle(mask) mask = mask.reshape(X, Y) # applying mask to the data 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: Import moduleCreate dataUse insert Nan valuesPrint data 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)]) # adding nan values to the row np.insert(a, 2, np.nan, axis=0) # adding nan values to the row np.insert(a, 2, np.nan, axis=1) Output: Comment More infoAdvertise with us Next Article How to randomly insert NaN in a matrix with NumPy in Python ? P prachibindal2925 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-numpy Python numpy-Matrix Function +1 More Practice Tags : python Similar Reads How to create a matrix of random integers in Python ? Prerequisites: numpy To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand. Syntax :  numpy.random.randint(low, high=None, size=None, 2 min read How to create an empty matrix with NumPy in Python? In Python, an empty matrix is a matrix that has no rows and no columns. NumPy, a powerful library for numerical computing, provides various methods to create matrices with specific properties, such as uninitialized values, zeros, NaNs, or ones. Below are different ways to create an empty or predefin 3 min read How to create a constant matrix in Python with NumPy? A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value th 4 min read How to randomly select rows of an array in Python with NumPy ? In this article, we will see two different methods on how to randomly select rows of an array in Python with NumPy. Let's see different methods by which we can select random rows of an array: Method 1: We will be using the function shuffle(). The shuffle() function shuffles the rows of an array rand 2 min read How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read Like