试图适应网格估计器,但收到TypeError:无法克隆对象

2024-05-11 03:34:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试适应网格搜索K近邻分类器,但收到以下TpyeError消息

TypeError: Cannot clone object. You should provide an instance of scikit-learn estimator instead of a 
class.

原始数据如下:

火车

compactness sa      area    roofM3  h   o   glaz    glazing_area_distribution
0   0.66    759.5   318.5   220.50  3.5 2   0.40    3
1   0.76    661.5   416.5   122.50  7.0 3   0.10    1
2   0.66    759.5   318.5   220.50  3.5 3   0.10    1
3   0.74    686.0   245.0   220.50  3.5 5   0.10    4
4   0.64    784.0   343.0   220.50  3.5 2   0.40    4
... ... ... ... ... ... ... ... ...
609 0.98    514.5   294.0   110.25  7.0 4   0.40    2

X_train.description()

count 614.000000614.000000  614.000000  614.000000  614.000000  614.000000  614.000000  614.000000
mean0.762606    673.271173  319.617264  176.826954  5.227199    3.495114    0.236645    2.802932
std 0.106725    88.757699   43.705256   45.499990   1.751278    1.124751    0.133044    1.571128
min 0.620000    514.500000  245.000000  110.250000  3.500000    2.000000    0.000000    0.000000
25% 0.660000    612.500000  294.000000  122.500000  3.500000    2.000000    0.100000    1.000000
75% 0.820000    759.500000  343.000000  220.500000  7.000000    4.000000    0.400000    4.000000
max 0.980000    808.500000  416.500000  220.500000  7.000000    5.000000    0.400000    5.000000

尝试创建并拟合模型

 from sklearn.model_selection import StratifiedKFold

 model = StratifiedKFold()
 cv_object = StratifiedKFold(n_splits=5, shuffle=True, random_state=50)


 grid_values = {'n_neighbors': ['1','2','3','4','5'],
          'weights': ['uniform', 'distance']
          }



from sklearn.model_selection import GridSearchCV

from sklearn.neighbors import KNeighborsClassifier as KNN

model = KNN()

gridsearch = GridSearchCV(KNN, cv=cv_object, param_grid=grid_values, 
scoring='neg_mean_absolute_error')



grid_estimator.fit(X_train, y_train)

Tags: offromimportmodelobjecttrainareasklearn