我正在使用GridSearchCV来查找管道的最佳参数 .

我的管道似乎运作良好,因为我可以申请:

pipeline.fit(X_train, y_train)
preds = pipeline.predict(X_test)

而且我得到了不错的结果 .

但GridSearchCV显然不喜欢什么,我无法弄明白 .

我的管道:

feats = FeatureUnion([('age', age),
                      ('education_num', education_num),
                      ('is_education_favo', is_education_favo),
                      ('is_marital_status_favo', is_marital_status_favo),
                      ('hours_per_week', hours_per_week),
                      ('capital_diff', capital_diff),
                      ('sex', sex),
                      ('race', race),
                      ('native_country', native_country)
                     ])

pipeline = Pipeline([
        ('adhocFC',AdHocFeaturesCreation()),
        ('imputers', KnnImputer(target = 'native-country', n_neighbors = 5)),
        ('features',feats),('clf',LogisticRegression())])

我的网格搜索:

hyperparameters = {'imputers__n_neighbors' : [5,21,41], 'clf__C' : [1.0, 2.0]}

GSCV = GridSearchCV(pipeline, hyperparameters, cv=3, scoring = 'roc_auc' , refit = False) #change n_jobs = 2, refit = False

GSCV.fit(X_train, y_train)

我收到11个类似的警告:

/home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/ipykernel/main.py:11:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 . 尝试使用.loc [row_indexer,col_indexer] = value

这是错误信息:

/home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/ipykernel/main.py:11:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 . 尝试使用.loc [row_indexer,col_indexer] = value而不是查看文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy / home / jo /anaconda2/envs/py35/lib/python3.5/site-packages/ipykernel/main.py:12:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 . 尝试使用.loc [row_indexer,col_indexer] = value而不是查看文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy / home / jo /anaconda2/envs/py35/lib/python3.5/site-packages/ipykernel/main.py:14:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置一个值 . 尝试使用.loc [row_indexer,col_indexer] = value而不是查看文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy ---- -------------------------------------------------- --------------------- ValueError Traceback(最近一次调用last)in()3 GSCV = GridSearchCV(管道,超参数,cv = 3,得分='roc_auc' ,refit = False)#change n_jobs = 2,refit = False 4 ----> 5 GSCV.fit(X_train,y_train)/home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/ sklearn / model_selection / _search.py in fit(self,X,y,groups)943 train / test set . 944“”“ - > 945返回self._fit(X,y,groups,ParameterGrid(self.param_grid))946 947 /home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/sklearn _fit中的/model_selection/_search.py(self,X,y,groups,parameter_iterable)562 return_times = True,return_parameters = True,563 error_score = self.error_score) - > 564 for parameter_iterable 565 for train,test in cv_iter )566 /home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in call(self,iterable)756#被派遣 . 特别是这包括边缘757#Parallel与耗尽的迭代器一起使用 . - > 758而self.dispatch_one_batch(iterator):759 self._iterating = True 760 else:/home/jo/anaconda2/envs/py35/lib/python3.5/site dispatch_one_batch(self,iterator)中的-packages / sklearn / externals / joblib / parallel.py 606返回False 607否则: - > 608 self._dispatch(tasks)609返回True 610 / home / jo / anaconda2 / envs / py35 / LIB / python3.5 /站点包/ sklearn /外部组件/ JOBLIB / PAR _dispatch中的allel.py(self,batch)569 dispatch_timestamp = time.time()570 cb = BatchCompletionCallBack(dispatch_timestamp,len(batch),self) - > 571 job = self._backend.apply_async(batch,callback = cb) 572 self._jobs.append(job)573 /home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/sklearn/externals/joblib/_parallel_backends.py in apply_async(self,func,callback)107 def apply_async(self,func,callback = None):108“”“安排一个要运行的命令”“” - > 109结果= ImmediateResult(func)110如果回调:111回调(结果)/ home / jo / anaconda2 init中的/envs/py35/lib/python3.5/site-packages/sklearn/externals/joblib/_parallel_backends.py(self,batch)324#不要延迟应用程序,以避免在内存中保留输入325#参数 - > 326 self.results = batch()327 328 def get(self):/ home / joa / annaconda2 / envs / py35 / lib / python3.5 / site -packages / sklearn / externals / joblib / parallel.py in call(self)129 130 def call(self): - > 131返回[func(* args,** kwargs)for func,args,kwargs in s elf.items] 132 133 def len(self):/ home / join / anaconda2 / envs / py35 / lib / python3.5 / site -packages / sklearn / externals / joblib / parallel.py in(.0)129 130 def call(self): - > 131返回[func(* args,** kwargs)for func,args,kwargs in self.items] 132 133 def len(self):/ home / jo / anaconda2 / envs / py35 / _fit_and_score中的lib / python3.5 / site-packages / sklearn / model_selection / _validation.py(估算器,X,y,记分器,训练,测试,详细,参数,fit_params,return_train_score,return_parameters,return_n_test_samples,return_times,error_score)236估计器.fit(X_train,** fit_params)237 else: - > 238 estimator.fit(X_train,y_train,** fit_params)239 240除了Exception为e:/ home / jo / anaconda2 / envs / py35 / lib / python3 . 5 / site-packages / sklearn / pipeline.py in fit(self,X,y,** fit_params)266此估算器267“”“ - > 268 Xt,fit_params = self._fit(X,y,** fit_params )269 ifself._final_estimator不是无:270 self._final_estimator.fit(Xt,y,** fit_params)/home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/sklearn/pipeline.py in _fit (self,X,y,** fit_params)232 pass 233 elif hasattr(transform,“fit_transform”): - > 234 Xt = transform.fit_transform(Xt,y,** fit_params_steps [name])235 else:236 Xt = transform.fit(Xt,y,** fit_params_steps [name])\ /home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/sklearn/base.py in fit_transform(self,X, y,** fit_params)495 else:496 #fit方法2(监督转换) - > 497返回self.fit(X,y,** fit_params).transform(X)498 499 in fit(self,X ,y)16 self.ohe.fit(X_full)17#创建一个不包含任何空值的数据帧,categories变量是OHE,所有每一行---> 18 X_ohe_full = self.ohe.transform(X_full [~X [self.col] .isnull()] . drop(self.col,axis = 1))19 20#在col为null的行上放置分类器/home/jo/anaconda2/envs/py35/lib/python3.5 /站点包/熊猫/核心/ FRAM e.py in getitem(self,key)2057返回self._getitem_multilevel(key)2058 else: - > 2059 return self._getitem_column(key)2060 2061 def _getitem_column(self,key):/ home / jo / anaconda2 / envs / _getitem_column中的py35 / lib / python3.5 / site-packages / pandas / core / frame.py(self,key)2064#self列2065如果self.columns.is_unique: - > 2066返回self._get_item_cache(key)2067 2068 #duplicate_item_cache(self,item)中的#duplicate columns&possible reduced dimensionality /home/jo/anaconda2/envs/py35/lib/python3.5/site-packages/pandas/core/generic.py 1384 res = cache.get(item )1385如果res为None: - > 1386 values = self._data.get(item)1387 res = self._box_item_values(item,values)1388 cache [item] = res / home / jo / anaconda2 / envs / py35 / lib get(self,item,fastpath)中的/python3.5/site-packages/pandas/core/internals.py 3550 loc = indexer.item()3551 else: - > 3552引发ValueError(“无法使用null键标记索引“)3553 3554返回self.iget(loc,fastpath = fastpath)ValueError:无法使用null键标记索引