【发布时间】:2019-01-11 22:16:45
【问题描述】:
我有一个评论数据集,其类别标签为正面/负面。我正在将决策树应用于该评论数据集。首先,我正在转换成一个词袋。这里 sorted_data['Text'] 是评论,final_counts 是稀疏矩阵。
我将数据拆分为训练和测试数据集。
X_tr, X_test, y_tr, y_test = cross_validation.train_test_split(sorted_data['Text'], labels, test_size=0.3, random_state=0)
# BOW
count_vect = CountVectorizer()
count_vect.fit(X_tr.values)
final_counts = count_vect.transfrom(X_tr.values)
如下应用决策树算法
# instantiate learning model k = optimal_k
# Applying the vectors of train data on the test data
optimal_lambda = 15
final_counts_x_test = count_vect.transform(X_test.values)
bow_reg_optimal = DecisionTreeClassifier(max_depth=optimal_lambda,random_state=0)
# fitting the model
bow_reg_optimal.fit(final_counts, y_tr)
# predict the response
pred = bow_reg_optimal.predict(final_counts_x_test)
# evaluate accuracy
acc = accuracy_score(y_test, pred) * 100
print('\nThe accuracy of the Decision Tree for depth = %f is %f%%' % (optimal_lambda, acc))
bow_reg_optimal 是一个决策树分类器。谁能告诉我如何使用决策树分类器获得特征重要性?
【问题讨论】:
标签: python machine-learning scikit-learn decision-tree sklearn-pandas