Note
Numpy (32) 본문
728x90
How to insert values at random positions in an array?
# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
# 1
i, j = np.where(iris_2d)
# i, j contain the row numbers and column numbers of 600 elements of iris_x
np.random.seed(100)
iris_2d[np.random.choice((i), 20), np.random.choice((j), 20)] = np.nan
# 2
np.random.seed(100)
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan
# Print first 10 rows
print(iris_2d[:10])
# output
[[b'5.1' b'3.5' b'1.4' b'0.2' b'Iris-setosa']
[b'4.9' b'3.0' b'1.4' b'0.2' b'Iris-setosa']
[b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
[b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']
[b'5.0' b'3.6' b'1.4' b'0.2' b'Iris-setosa']
[b'5.4' b'3.9' b'1.7' b'0.4' b'Iris-setosa']
[b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']
[b'5.0' b'3.4' b'1.5' b'0.2' b'Iris-setosa']
[b'4.4' nan b'1.4' b'0.2' b'Iris-setosa']
[b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]
'Numpy' 카테고리의 다른 글
Numpy (34) (0) | 2022.08.26 |
---|---|
Numpy (33) (0) | 2022.08.25 |
Numpy (31) (0) | 2022.08.23 |
Numpy (30) (0) | 2022.08.22 |
Numpy (29) (0) | 2022.08.21 |
Comments