【发布时间】:2017-12-21 06:59:42
【问题描述】:
我有 204567 个单词,其中 21010 个是唯一的。每个单词都与一个唯一的标签相关联。总共有 46 个唯一标签。
我已经使用特征散列来映射使用HashingVectorizer() 的 204567 个单词。我对标签进行了 one-hot 编码,并使用Perceptron() 模型来解决这个多类分类问题。
from keras.utils import np_utils
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.linear_model import Perceptron
from sklearn.preprocessing import LabelEncoder
vect = HashingVectorizer(decode_error='ignore', n_features=2**15,
preprocessor=None)
X = vect.transform(X_train)
encoder = LabelEncoder()
y = encoder.transform(y_train)
target = np_utils.to_categorical(y)
ppn = Perceptron(n_iter=40, eta0=0.1, random_state=0)
ppn.fit(X, target)
但是,我收到以下错误:ValueError: bad input shape (204567, 46)
有没有更好的方法来编码标签?
附:请解释错误和可能的解决方案
【问题讨论】:
-
在这里发布
X.shape和target.shape的输出。 -
X.shape = (204567, 32768) 和 y.shape = (204567, 46)
-
*y.shape 这里指的是target.shape
-
是否有特殊需要将 one-hot 编码标签转换回分类标签。您不能像在
Perceptron.fit()中一样使用它们吗? -
@VivekKumar 不,没有必要将它们改回来。我遇到的问题恰好在 perceptron.fit() 部分
标签: python-3.x machine-learning scikit-learn nlp keras