【发布时间】:2017-01-19 13:56:08
【问题描述】:
我正在为网页编写一个分类器,所以我有数字特征的混合,我也想对文本进行分类。我正在使用词袋方法将文本转换为(大)数字向量。代码最终是这样的:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import numpy as np
numerical_features = [
[1, 0],
[1, 1],
[0, 0],
[0, 1]
]
corpus = [
'This is the first document.',
'This is the second second document.',
'And the third one',
'Is this the first document?',
]
bag_of_words_vectorizer = CountVectorizer(min_df=1)
X = bag_of_words_vectorizer.fit_transform(corpus)
words_counts = X.toarray()
tfidf_transformer = TfidfTransformer()
tfidf = tfidf_transformer.fit_transform(words_counts)
bag_of_words_vectorizer.get_feature_names()
combinedFeatures = np.hstack([numerical_features, tfidf.toarray()])
这可行,但我担心准确性。请注意,有 4 个对象,只有两个数字特征。即使是最简单的文本也会产生一个具有九个特征的向量(因为语料库中有九个不同的单词)。显然,对于真实文本,会有数百或数千个不同的单词,因此最终的特征向量将是 1000 个基于单词的特征。
正因为如此,分类器 (SVM) 不会以 100 比 1 的系数对数字特征上的单词进行大量加权吗?如果是这样,我该如何补偿以确保词袋与数字特征的权重相等?
【问题讨论】:
-
您可以使用 scikit learn 中的 TruncatedSVD 来降低单词特征的维度。 scikit-learn.org/stable/modules/generated/…
-
您找到处理方法了吗?我正在用 Spark 做类似的事情。
-
我对此主题了解不多,但我一直在寻找相同的东西,看来您正在寻找的是 FeatureUnion - stackoverflow.com/questions/39445051/…
标签: python scikit-learn classification text-classification