Note
Numpy (35) 본문
728x90
How to drop rows that contain a missing value from a numpy array?
# 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.
# 1:
any_nan_in_row = np.array([~np.any(np.isnan(row)) for row in iris_2d])
iris_2d[any_nan_in_row][:5]
# 2: (By Rong)
iris_2d[np.sum(np.isnan(iris_2d), axis = 1) == 0][:5]
# output
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]])
'Numpy' 카테고리의 다른 글
Numpy (37) (0) | 2022.08.29 |
---|---|
Numpy (36) (0) | 2022.08.28 |
Numpy (34) (0) | 2022.08.26 |
Numpy (33) (0) | 2022.08.25 |
Numpy (32) (0) | 2022.08.24 |
Comments