我写了一段代码来处理Kaggle的泰坦尼克号数据。我写的课程如下:GridSearchCV是否调用管道中对象的初始化程序?
class Transform(BaseEstimator,TransformerMixin):
def __init__(self,select_dict={},default=False,list_of_attributes=dataset_columns,
one_hot_default=True,one_hot={}):
if list_of_attributes is None:
list_of_attributes=dataset_columns
self.attributes=select_dict #Here I select which attributes to take
self.Transformed=[]
#if default is False(True) it changes all other attributes to
#default that I do not select (to make things a little easy
def transform_Name(self,X):
#Transformation function for specific attribute
#I made such functions for every attribute and then they append
#The transformed series in self.Transformed
self.Transformed.append(X);
def transform(self,X,y=None):
#This function calls transform function of whose value is True in
#self.attributes and finally returns the DataFrame
return pd.concat(self.Transformed_Data,axis=1)
我只显示此类的相关代码。
现在,我创建使用SVC类SKlearn的管道
transfomer=Transform(select_dict={'PassengerId': False},default=True)
svc_grid_clf=Pipeline([
('transform',transformer),
('SVC',SVC())
])
一旦我创建这个管道,通过以下参数网格中创建一个GridSearchCV
Param=[
{
'SVC__kernel': ['rbf'],
'SVC__C': [0.1,1,1.5]
}]
现在创建GridSearchCV对象
grid_svm=GridSearchCV(estimator=svc_grid_clf,cv=3,param_grid=Param)
#Now fitting
grid_svm.fit(X,y)
但是,我收到一些我无法理解的错误,即
ValueError: No objects to concatenate.
我相信错误是因为GridSearchCV或者是打电话还是不打电话变压器的初始化功能,因此有时变压器的select_dict不来了,因为它应该是。
('SVC',SVC()) - 可能是多余的括号?尝试('SVC',SVC) 在哪里实例化SVC就像使用Transform一样进行实施? – CrazyElf
“self.Transformed_Data”从哪里填满?并发布错误的完整堆栈跟踪。 –
@CrazyElf我已经尝试了你所说的从SVC()中除去'()',但是它不能调用它的__init__,因为它只是类SVC的别名。 – scipsycho