获取numpy数组中的nan值的索引

时间:2018-11-09 08:30:53

标签: python-3.x numpy

从条件中获取nan值的索引并将其传递到原始数组,但它返回空值

#Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter = ',', dtype = 'float')# random number from 0 to 150 as high is none
iris_2d[np.random.randint(150, size = 20), np.random.randint(4, size = 20)] = np.nan


( iris_2d[np.where(iris_2d[a,b]==np.nan)])

1 个答案:

答案 0 :(得分:0)

我从您提到的url下载了数据,并将其保存为“ iris_data.txt”。请尝试以下代码。它为您提供nan数组numpyiris_2d的索引。

import numpy as np

iris_2d = np.genfromtxt('iris_data.txt', delimiter = ',', dtype = 'float')

nan_indx = np.argwhere(np.isnan(iris_2d))

print nan_indx.shape
print " " 
print nan_indx[0:5,:]

输出:

(150, 2)

[[0 4]
 [1 4]
 [2 4]
 [3 4]
 [4 4]]