我想将分类变量的字符串数组转换为分类变量的整数数组。numpy将分类字符串数组转换为整数数组
Ex。
import numpy as np
a = np.array(['a', 'b', 'c', 'a', 'b', 'c'])
print a.dtype
>>> |S1
b = np.unique(a)
print b
>>> ['a' 'b' 'c']
c = a.desired_function(b)
print c, c.dtype
>>> [1,2,3,1,2,3] int32
我意识到这可以用循环完成,但我想有一个更简单的方法。谢谢。
你认真想添加的警告,这种方式只适用于长度为1的字符串。 – smci 2013-07-23 12:23:39