朴素贝叶斯和K最近邻居上的“ coefs_”或“ features_importance_”是什么意思?

时间:2019-07-27 04:56:42

标签: python-3.x machine-learning scikit-learn

首先,我正在做有关情绪分析分类器比较的项目。然后我想知道每个分类器功能的重要性

1 个答案:

答案 0 :(得分:0)

对于K最近的Neigbour,您可以同时使用一项功能进行拟合和预测,然后打印结果以查看哪个功能最重要。

使用虹膜数据集的示例:

import numpy as np
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score

iris = datasets.load_iris() # the data
clf = KNeighborsClassifier() # the model

y =  iris.target # the target vector
n_features = iris.data.shape[1]

print('Feature Index , Accuracy obtained')
for i in range(n_features):
    X = iris.data[:, i].reshape(-1, 1)
    scores = cross_val_score(clf, X, y, cv = 5, scoring='accuracy') # cross-validated accuracy
    print('{}   {}'.format(i, scores.mean()))

上面的照片:

Feature Index , Accuracy obtained
0   0.646666666667
1   0.553333333333
2   0.946666666667
3   0.96