首页 > 解决方案 > AttributeError:“GridSearchCV”对象在 scikit-learn 0.19.2 上没有属性“cv_results_”

问题描述

我目前正在使用 Scikit-Learn 版本 0.19.2 和 Python 3.6.3

出于某种原因,我无法cv_results_从我的GridSearchCV.

这是我正在使用的代码:

df = pd.read_csv(input_file, sep = ";", header=None)

numpy_array = df.as_matrix()
y=numpy_array[:,1]
y[y=='RR']=1
y[y=='AIRR']=0
print(y)
y=y.astype('int')

vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5, stop_words=stopwords)

X=numpy_array[:,0]
X=vectorizer.fit_transform(X)

param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
              "base_estimator__splitter" :   ["best", "random"],
              "n_estimators": [1, 2]
             }

DTC = DecisionTreeClassifier(random_state = 11, max_features = "auto", class_weight = "balanced",max_depth = None)


# Create and fit an AdaBoosted decision tree
bdt = AdaBoostClassifier(base_estimator = DTC)

grid_search_ABC = GridSearchCV(bdt, param_grid=param_grid, scoring = 'roc_auc', cv=5, refit=True)

pred = grid_search_ABC.fit(X,y)

print(metrics.confusion_matrix(y, pred))

mean=grid_search_ABC.cv_results_['mean_test_score']
std=grid_search_ABC.cv_results_['std_test_score']

我读到这主要与GridSearchCV可能不适合有关,但我完全可以用它来预测新实例等。

请问有什么指点吗?

标签: pythonpandasscikit-learn

解决方案


问题可能出在您的数据集上。这就是为什么本网站鼓励您发布可验证的示例。

我刚刚尝试在 iris 数据集上运行您的代码,它工作得很好:

from sklearn import datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier

param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
              "base_estimator__splitter" :   ["best", "random"],
              "n_estimators": [1, 2]
             }

DTC = DecisionTreeClassifier(random_state = 11, max_features = "auto", class_weight = "balanced",max_depth = None)
bdt = AdaBoostClassifier(base_estimator = DTC)
grid_search_ABC = GridSearchCV(bdt, param_grid=param_grid, scoring = 'roc_auc', cv=5, refit=True)

pred = grid_search_ABC.fit(iris.data, iris.target>0)
print(grid_search_ABC.cv_results_['mean_test_score'])

它工作得很好。


推荐阅读