我已经使用Sklearllogistic回归模型为文本创建了二进制分类模型。现在,我想选择用于模型的功能。我的代码看起来像这样 -
train, val, y_train, y_test = train_test_split(np.arange(data.shape[0]), lab, test_size=0.2, random_state=0)
X_train = data[train]
X_test = data[val]
#X_train, X_test, y_train, y_test = train_test_split(data, lab, test_size=0.2)
tfidf_vect = TfidfVectorizer(analyzer='word', ngram_range=(1,3), min_df = 0, stop_words = 'english')
X_tfidf_train = tfidf_vect.fit_transform(X_train)
X_tfidf_test = tfidf_vect.transform(X_test)
clf_lr = LogisticRegression(penalty='l1')
clf_lr.fit(X_tfidf_train, y_train)
feature_names = tfidf_vect.get_feature_names()
print len(feature_names)
y_pred_lr = clf_lr.predict_proba(X_tfidf_test)[:, 1]
最好的方法是什么。
您可以使用sklearn.feature_selection
这是您如何使用它的链接http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.rfe.html#sklearn.feature_selection.rfe