我正试图用一个标准的sklearn scaler和一个自写模型类建立一个管道。在写costum课程时,我遵循了sklearn documentation。管道应该是这样的
model_piped = make_pipeline(StandardScaler(), model)
问题在于,在拟合管道肋模型后,它没有使用StandardScaler的逆_变换方法来重新缩放其预测。因此,Costam模型只对输入进行缩放。当然,管道适用于标准的sklearn模型,如Lasso回归
一个有效的例子:
import numpy as np
from sklearn.linear_model import Lasso
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from matplotlib import pyplot as plt
# =============================================================================
# An example costum model
# =============================================================================
class mean_stacker():
def __init__(self, desc = 'Simple Mean Stacker'):
self.description = desc
def predict(self, X):
check_is_fitted(self, ['coef_'])
X = check_array(X)
return np.mean(X, axis = 1)
def fit(self, X, y):
X, y = check_X_y(X, y)
self.coef_ = 'mean'
return self
# =============================================================================
# test data
# =============================================================================
yhats = np.array([[11.64543231, 11.49851957, 11.89059499, 11.77613068],
[12.0166365 , 12.18640595, 11.89059499, 12.03356647],
[11.91435714, 12.00392321, 11.89059499, 12.00279713],
[11.74216858, 11.57740889, 11.89059499, 11.57306004],
[11.9827991 , 12.09409814, 11.89059499, 12.14146709],
[11.64009661, 11.55337117, 11.89059499, 11.539958 ],
[11.8658174 , 11.93479133, 11.89059499, 11.88695717],
[11.53478821, 11.24788878, 11.89059499, 11.47217846],
[12.03600978, 12.16789499, 11.89059499, 12.09874916],
[12.07294432, 12.20473012, 11.89059499, 12.20545864],
[11.62189652, 11.34157305, 11.89059499, 11.4359684 ],
[11.5167136 , 11.66579694, 11.89059499, 11.58799839]])
ytrue = np.array(
[[11.6524265 ],
[11.89470562],
[12.12673719],
[12.7966 ],
[11.86452555],
[11.85743673],
[11.45650325],
[11.96433224],
[12.33647352],
[11.96876678],
[11.24377724],
[11.2209676 ]])
# =============================================================================
# Define set of stacker models, wrap them into pipelines and fit
# =============================================================================
stacker_models =[
Lasso(alpha = 0.0005, random_state = 4),
mean_stacker()
]
stacker = []
for model in stacker_models:
stkr = make_pipeline(StandardScaler(), model)
stkr.fit(yhats, ytrue)
stacker.append(stkr)
# =============================================================================
# plot predicts from models
# the costum model does not rescale the predicts correctly
# =============================================================================
plt.plot(ytrue, label = 'ytrue')
plt.plot(stacker[0].predict(yhats), label = 'yLasso')
plt.plot(stacker[1].predict(yhats), label = 'yMean')
plt.legend()
有没有关于如何修复Costam模型的提示
目前没有回答
相关问题 更多 >
编程相关推荐