我寫了一段代碼來處理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