Note
Numpy (52) 본문
728x90
How to create row numbers grouped by a categorical variable?
# Input:
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)
np.random.seed(100)
species_small = np.sort(np.random.choice(species, size=20))
species_small
array(['Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa',
'Iris-setosa', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-versicolor', 'Iris-versicolor',
'Iris-versicolor', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica', 'Iris-virginica', 'Iris-virginica',
'Iris-virginica'],
dtype='<U15')
# solution
print([i for val in np.unique(species_small) for i, grp in enumerate(species_small[species_small==val])])
# output
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5]
'Numpy' 카테고리의 다른 글
Numpy (54) (0) | 2022.10.13 |
---|---|
Numpy (53) (0) | 2022.09.21 |
Numpy (51) (0) | 2022.09.17 |
Numpy (50) (0) | 2022.09.16 |
Numpy (49) (0) | 2022.09.15 |