Note
Numpy (40) 본문
728x90
How to convert a numeric to a categorical (text) array?
# Input
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
# Bin petallength
petal_length_bin = np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10])
# Map it to respective category
label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}
petal_length_cat = [label_map[x] for x in petal_length_bin]
# View
petal_length_cat[:4]
<#> ['small', 'small', 'small', 'small']
'Numpy' 카테고리의 다른 글
Numpy (42) (0) | 2022.09.06 |
---|---|
Numpy (41) (0) | 2022.09.04 |
Numpy (39) (0) | 2022.08.31 |
Numpy (38) (0) | 2022.08.30 |
Numpy (37) (0) | 2022.08.29 |
Comments