我有一个评论数据集,其中有正面/负面的类标签。我正在将逻辑回归应用于该评论数据集。首先,我将其转换为词袋模型。这里sorted_data ['Text']是评论,而final_counts是一个稀疏矩阵。
count_vect = CountVectorizer()
final_counts = count_vect.fit_transform(sorted_data['Text'].values)
standardized_data = StandardScaler(with_mean=False).fit_transform(final_counts)
将数据集拆分为训练集和测试集。
X_1, X_test, y_1, y_test = cross_validation.train_test_split(final_counts, labels, test_size=0.3, random_state=0)
X_tr, X_cv, y_tr, y_cv = cross_validation.train_test_split(X_1, y_1, test_size=0.3)
我将按以下方式应用逻辑回归算法。
optimal_lambda = 0.001000
log_reg_optimal = LogisticRegression(C=optimal_lambda)
# fitting the model
log_reg_optimal.fit(X_tr, y_tr)
# predict the response
pred = log_reg_optimal.predict(X_test)
# evaluate accuracy
acc = accuracy_score(y_test, pred) * 100
print('\nThe accuracy of the Logistic Regression for C = %f is %f%%' % (optimal_lambda, acc))
我的权重是
weights = log_reg_optimal.coef_ . #<class 'numpy.ndarray'>
array([[-0.23729528, -0.16050616, -0.1382504 , ..., 0.27291847,
0.35857267, 0.41756443]])
(1, 38178) #shape of weights
我希望获取特征重要性,即具有高权重的前100个特征。有人可以告诉我如何获取它们吗?