python - 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
可能不适合有关,但我完全可以用它来预测新实例等。
请问有什么指点吗?
解决方案
问题可能出在您的数据集上。这就是为什么本网站鼓励您发布可验证的示例。
我刚刚尝试在 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'])
它工作得很好。
推荐阅读
- r - 如何根据用户在 R-Shiny 中的 tabItem 选择触发某些代码?
- angular7 - ngx-gallery 不显示图像
- database - 如何使用 OnCellClick 事件在 Delphi 中的 DBGrid 上获取单元格的内容
- haskell - 解决依赖于约束的类的约束
- sql - SQL 多连接查询-查询调优
- ios - UIStackView 高度,内容在中间而不是在顶部
- c# - Xamarin 表单如何从图库中选择图像
- python - 如何删除没有特定字符模式的字符串?
- angular7 - 如何以角度提交具有相同输入字段名称的多个动态表单?
- python-3.x - 从 2 个句子中找到缺失的单词