我想学习gridsearchCV的工作原理,通过在Knearistneighbors上进行测试。当我将n_neighbors = 9时,我的分类器给出了0.9122807017543859的得分。
但是,当我在列表中使用gridsearchCV并给它n_neighbors = 9时,我得到了0.8947368421052632的得分。
可能的原因是什么?感谢您的任何努力。
以下是我的代码:
from sklearn import datasets
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split as splitter
import pickle
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
# Data pre-processing <-----------------------
data = datasets.load_breast_cancer()
p=data
add=data.target.reshape(569,1)
columns = np.append(data.feature_names,
data.target_names[0],
axis=None)
data = np.append(data.data,
add,
axis=1)
df = pd.DataFrame(data=data,columns=columns)
X_train,X_test,y_train,y_test = splitter(p.data,
p.target,
test_size=0.3,
random_state=12)
gauss = KNeighborsClassifier(n_neighbors=9)
param_grid={'n_neighbors':[1,2,3,4,5,6,7,8,9,11,12,13,10]}
gausCV = GridSearchCV(KNeighborsClassifier(),param_grid,verbose=False)
gauss.fit(X_train,y_train)
gausCV.fit(X_train,y_train)
print(gauss.score(X_test,y_test))
print(gausCV.score(X_test,y_test))
我得到的内容如下
0.9122807017543859
0.8947368421052632