我正在爲網頁編寫分類器,所以我有數字特徵的混合,並且我還想分類文本。我正在使用詞袋方法將文本轉換爲(大)數值向量。代碼結果是這樣的:如何在scikit-learn中將數字特徵與文字(字袋)正確結合?
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個對象,只有兩個數字特徵。即使是最簡單的文本也能得到具有9個特徵的向量(因爲在語料庫中有9個不同的單詞)。顯然,在真實文本中,將會有數百或數千個不同的單詞,所以最終的特徵向量將是數字特徵,但是基於1000個以上的單詞。由於這個原因,分類器(SVM)不會將數字特徵上的單詞以100到1的比例嚴重加權嗎?如果是這樣,我該如何補償,以確保單詞包的權重等於數字特徵?
您可以使用Scikit學習中的TruncatedSVD來降低單詞特徵的維度。 http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html – aberger
你找到了解決方法嗎?我正在與Spark做類似的事情。 – schoon