📜  如何在Python中使用 NumPy 在矩阵中随机插入 NaN?

📅  最后修改于: 2022-05-13 01:55:27.091000             🧑  作者: Mango

如何在Python中使用 NumPy 在矩阵中随机插入 NaN?

先决条件: Numpy

在本文中,让我们看看如何使用 Numpy 生成一个将 Nan 随机插入矩阵的Python脚本。下面给出了执行相同操作的 3 种方法:

方法一:使用ravel()函数

ravel()函数返回连续的扁平数组(具有所有输入数组元素并具有与其相同类型的一维数组)。仅在需要时制作副本。
句法 :

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

方法:

  • 导入模块
  • 创建数据
  • 选择随机索引到 Nan 值。
  • 将这些索引传递给 ravel()函数
  • 打印数据

示例 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)


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)


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)


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)


输出:

示例 2:将 nan 添加到但使用randint函数创建数据。要在randint函数中使用np.nan ,我们必须首先将数据转换为 float,因为np.nan是 float 类型。

蟒蛇3

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)

输出:

方法 2:创建蒙版

创建布尔值掩码并将该掩码应用于数据集可能是产生所需结果的一种方法。

方法:

  • 导入模块
  • 创建数据
  • 创建蒙版
  • 洗牌掩码以随机应用 Nan 值
  • 将掩码应用于数据
  • 打印数据

例子 :

蟒蛇3

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)

输出:

方法 3:使用 insert()

使用 insert()函数会将整行或整列转换为 NaN。此函数在给定索引之前沿提到的轴插入值。
句法 :

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

方法:

  • 导入模块
  • 创建数据
  • 使用插入 Nan 值
  • 打印数据

例子:

蟒蛇3

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)

输出: