【Python】How to drop rows that contain a missing value from a numpy array? - 101 Numpy Exercises
Q:
iris_2dからnanを含まない行だけ取り出しなさい
# Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
A:
# Input url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan # Solution # No direct numpy function for this. any_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d]) iris_2d[any_nan_in_row][:5] #> array([[ 4.9, 3. , 1.4, 0.2], #> [ 4.7, 3.2, 1.3, 0.2], #> [ 4.6, 3.1, 1.5, 0.2], #> [ 5. , 3.6, 1.4, 0.2], #> [ 5.4, 3.9, 1.7, 0.4]])