【问题标题】:AttributeError: 'GridSearchCV' object has no attribute 'cv_results_' on scikit-learn 0.19.2AttributeError:“GridSearchCV”对象在 scikit-learn 0.19.2 上没有属性“cv_results_”
【发布时间】:2018-08-15 13:42:43
【问题描述】:

我目前使用的是 Scikit-Learn 版本 0.19.2 和 Python 3.6.3

由于某种原因,我无法从我的GridSearchCV 访问cv_results_ 属性。

这是我正在使用的代码:

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 可能没有安装有关,但我完全可以用它来预测新实例等。

请指教一下?

【问题讨论】:

  • 那么在你grid_search_ABC.fit(X,y) 之后,如果你做grid_search_ABC.cv_results_,属性就没有了吗?你有return_train_score=True 或return_train_score='warn' 所以这应该不是问题。
  • 你正在这样做pred = grid_search_ABC.fit(X,y)。所以 pred 实际上是一个 GridSearchCV 对象,而不是预测。你确定你在下一行没有收到任何错误:print(metrics.confusion_matrix(y, pred))?发布错误的完整堆栈跟踪。

标签: python pandas scikit-learn


【解决方案1】:

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

我刚刚尝试在 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'])

而且效果很好。

【讨论】:

    猜你喜欢
    • 2017-05-22
    • 2019-07-03
    • 1970-01-01
    • 2016-03-15
    • 2020-07-02
    • 2013-05-20
    • 2019-03-29
    • 1970-01-01
    • 2019-06-18
    相关资源
    最近更新 更多