Scikit SGDClassifier 使用字母而不是单词作为特征

Scikit SGDClassifier using letters as features instead of words

我正在尝试按照 this 教程

使用 SGDClassifier 对短语进行分类

我的管道是这样的:

p_clf = Pipeline([('vect', CountVectorizer(analyzer='word', ngram_range=(1, 2),
                                  token_pattern=r'\b\w+\b', min_df=1)),
                  ('tfidf', TfidfTransformer()),
                  ('clf', SGDClassifier(loss='log', penalty='l2',
                                        alpha=1e-3, n_iter=5, random_state=42)), ])

尽管我得到了正确的分类,但我不明白为什么它将字母视为特征而不是整个单词。在这个例子中,当我调用 predict_proba('Hello') 时,我得到:

[[ 0.15889614  0.23752053  0.4353584   0.16822494]
 [ 0.15889614  0.23752053  0.4353584   0.16822494]
 [ 0.15889614  0.23752053  0.4353584   0.16822494]
 [ 0.15889614  0.23752053  0.4353584   0.16822494]
 [ 0.11579265  0.19786962  0.36811551  0.31822223]]

每行是一个字母,列是我的 类。 不应该只有一行吗?

在您的例子中,'Hello' 被解释为字符数组,如 ['H','e','l','l','o']。 (请记住,predict_proba 需要数组或稀疏矩阵作为输入。)这可以通过将字符串放入列表中来解决:

predict_proba(['Hello'])