确定哪些功能下降/选择使用GridSearch scikit学习

问题描述 投票:0回答:2

一个人如何确定哪些功能/列/属性中使用GridSearch成绩下降?

换句话说,如果GridSearch返回max_features应该是3,我们能确定它的确切3,特点应在使用?

让我们以经典的虹膜数据集有4个特点。

import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import StratifiedKFold 
from sklearn.model_selection import GridSearchCV
from sklearn import datasets

iris = datasets.load_iris()
all_inputs = iris.data
all_labels = iris.target

decision_tree_classifier = DecisionTreeClassifier()

parameter_grid = {'max_depth': [1, 2, 3, 4, 5],
              'max_features': [1, 2, 3, 4]}

cross_validation = StratifiedKFold(n_splits=10)

grid_search = GridSearchCV(decision_tree_classifier,
                       param_grid=parameter_grid,
                       cv=cross_validation)

grid_search.fit(all_inputs, all_labels)
print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))

比方说,我们得到了max_features是3.如何找出3特点是最合适的吗?

在max_features = 3把将用于装配工作,但我想知道哪个属性是正确的。

我必须生成所有特征组合自己养活GridSearch或有更简单的方式可能的名单?

python machine-learning scikit-learn feature-selection
2个回答
1
投票

如果您使用具有feature_importances_你可以简单地做属性的估计:

feature_importances = grid_search.best_estimator_.feature_importances_

这将返回的每个功能如何重要的是与网格搜索找到的最佳估计列表(n_features)。此外,如果你想使用假设线性分类(回归),不具有属性feature_importances_你可以做的是:

# Get the best estimator's coefficients
estimator_coeff = grid_search.best_estimator_.coef_
# Multiply the model coefficients by the standard deviation of the data
coeff_magnitude = np.std(all_inputs, 0) * estimator_coeff)

这也是该功能重要性的指示。如果模型的系数是>> 0<< 0,这意味着,在通俗地说,该模型正努力捕捉存在于该功能的信号。


2
投票

max_features是决策树的一个超参数。它不训练前降大任于你的特点,也没有找到好的还是坏的特征。

你decisiontree着眼于所有功能找到最好的功能,根据您的标签拆分您的数据。如果设置但MaxFeature 3在你的榜样,你的决策树只是着眼于三个随机的特点和需要的最好的功能,使分裂。这使得你的训练速度更快,并增加了一些随机性的分类器(也可能有助于防止过度拟合)。

您的分类器确定其是由一个标准(像基尼系数的信息增益(1-熵))的功能。所以,你可以采取功能重要性或这种测量

使用具有属性的估计feature_importances_

作为@gorjan提及。

© www.soinside.com 2019 - 2024. All rights reserved.