Note

Numpy (38) 본문

Numpy

Numpy (38)

알 수 없는 사용자 2022. 8. 30. 19:52
728x90

How to replace all missing values with 0 in 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
iris_2d[np.isnan(iris_2d)] = 0
iris_2d[:4]

# output 
 array([[ 5.1,  3.5,  1.4,  0. ],
        [ 4.9,  3. ,  1.4,  0.2],
        [ 4.7,  3.2,  1.3,  0.2],
        [ 4.6,  3.1,  1.5,  0.2]])

'Numpy' 카테고리의 다른 글

Numpy (40)  (2) 2022.09.03
Numpy (39)  (0) 2022.08.31
Numpy (37)  (0) 2022.08.29
Numpy (36)  (0) 2022.08.28
Numpy (35)  (0) 2022.08.27
Comments