I have the following numpy matrix:
M = [
['a', 5, 0.2, ''],
['a', 2, 1.3, 'as'],
['b', 1, 2.3, 'as'],
]
M = np.array(M)
我想要对类别值 ('a', 'b', '', 'as'
) 进行编码。尝试使用OneHotEncoder进行编码,但它无法处理字符串变量并且会导致错误。
enc = preprocessing.OneHotEncoder()
enc.fit(M)
enc.transform(M).toarray()
我知道我必须使用categorical_features
来显示我要编码的值,我认为通过提供dtype
,我将能够处理字符串值,但我不能。那么有没有一种方法可以对我的矩阵进行分类值编码?