我有一个评论数据集,它有一个正面/负面的类标签。我正在将决策树应用于该审查数据集。首先,我正在转换成一袋文字。在这里,排序数据‘’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是一种决策树分类器。有人能告诉我们如何使用决策树分类器获得特性重要性吗?
发布于 2018-08-04 04:40:39
使用feature_importances_属性,该属性将在调用fit()后定义。例如:
import numpy as np
X = np.random.rand(1000,2)
y = np.random.randint(0, 5, 1000)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier().fit(X, y)
tree.feature_importances_
# array([ 0.51390759, 0.48609241])https://stackoverflow.com/questions/51682470
复制相似问题