Python 属性错误:';GridSearchCV';对象没有属性';cv_结果';在scikit上学习0.19.2
我目前正在使用Scikit学习版0.19.2和Python 3.6.3 由于某些原因,我无法从我的Python 属性错误:';GridSearchCV';对象没有属性';cv_结果';在scikit上学习0.19.2,python,pandas,scikit-learn,Python,Pandas,Scikit Learn,我目前正在使用Scikit学习版0.19.2和Python 3.6.3 由于某些原因,我无法从我的GridSearchCV访问cv\u results\u属性 这是我正在使用的代码: 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')
GridSearchCV
访问cv\u results\u
属性
这是我正在使用的代码:
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'])
它工作得很好。那么在你
网格搜索\u ABC.fit(X,y)
之后,如果你做网格搜索\u ABC.cv\u结果
,属性就不存在了吗?您有return\u train\u score=True
或return\u train\u score='warn'
,所以这不应该是一个问题。您正在执行pred=grid\u search\u ABC.fit(X,y)
。所以pred实际上是一个GridSearchCV对象,而不是预测。您确定下一行中没有出现任何错误:打印(metrics.conflusion\u matrix(y,pred))
?发布错误的完整堆栈跟踪。