我被这个错误困住了,我明白它的意思,但我不知道如何处理它。在
我要做的是:
class PreProcessing(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def transform(self, df):
#Here i select the features and transform them for exemple:
age_band=0
if age<=10
age_band=1
else #... etc to 90
age_band=9
....
other feature engineering
....
encoder = ce.BinaryEncoder(cols=selectedCols)
encoder.fit(df)
df = encoder.transform(df)
return df.as_matrix()
def fit(self, df, y=None, **fit_params):
return self
pipe = make_pipeline(PreProcessing(),
SelectKBest(f_classif,k=23),
RandomForestClassifier())
param_grid = {"randomforestclassifier__n_estimators" : [100,400],
"randomforestclassifier__max_depth" : [None],
"randomforestclassifier__max_leaf_nodes": [2,3,5],
"randomforestclassifier__min_samples_leaf":[3,5,8],
"randomforestclassifier__class_weight":['balanced'],
"randomforestclassifier__n_jobs":[-1]
}
grid_search = GridSearchCV(pipe,param_grid,cv=5,scoring='recall',verbose=1,n_jobs=15)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
grid_search.fit(X_train,y_train)
grid_search.predict(X_test)
filename = 'myModel.pk'
with open(filename, 'wb') as file:
pickle.dump(grid_search, file)
所以在这里一切都像一个魔咒。 但有了真实世界的数据:(不是列车测试文件)
^{pr2}$我得到了一个错误:值错误:X的形状与装配时不同。在
我所理解的是,并不是所有的模式都在我的“真实文件”中,因为想象一下,在我的列车文件中,我有一列“couple”,值为“yes,no,I don know”,然后二进制编码器将创建存储所有模式所需的列作为二进制。 但在我的现实生活文件中,我只能对这些列的值“是的,不是”做出预测,所以最后,X的形状与拟合时的不一样。。。 所以我唯一要做的就是在预处理中创建所有缺失的模式,cols值为0。。。在
我想我错过了什么。在
注:培训和测试文件来自某个数据源。我需要预测的数据来自另一个来源,所以我首先将这些真实数据“转换”为相同的X_train/Test格式,然后模型.predit(df)。所以我确信在二进制编码器之前我有相同数量的col(17)预处理.转换()但在之后,如果我在运行时记录df的形状,则执行二进制编码器模型.预测(X检验)显示df为41 cols,并且模型.预测(realData)只有31列。在
这似乎是您的“特性选择/创建”过程中的一个问题。每次将一组新的输入传递到管道时,都要安装一个
BinaryEncoder
。这意味着,每当在指定列中有不同数量的唯一值时,代码都会因此错误而中断。在我的猜测是,如果您将
BinaryEncoder
保存为PreProcessing
实例的一部分,假设您的训练数据具有此列可以接受的所有可能值,则这不会是一个问题。在更好的是,您可以将BinaryEncoder插入到管道中,并将其完全排除在
^{pr2}$PreProcessing
之外吗?在相关问题 更多 >
编程相关推荐