亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Pipeline 和 TransformedTargetRegressor 在嵌套回歸器上處理

使用 Pipeline 和 TransformedTargetRegressor 在嵌套回歸器上處理

冉冉說 2023-07-05 17:41:21
我想使用 Pipeline 和 TransformedTargetRegressor 來處理 BaggingRegressor 及其所有估計器上的所有縮放(數據和目標)。我的第一次嘗試效果很好(沒有使用 Pipeline 和 TransformedTargetRegressor)$ cat test1.py#!/usr/bin/python# -*- coding: UTF-8 -*-import numpy as npimport matplotlib.pyplot as pltfrom sklearn.ensemble import BaggingRegressorfrom sklearn.svm import SVRdef f(x):    return x*np.cos(x) + np.random.normal(size=500)*2def main():    # Generate random data.    x = np.linspace(0, 10, 500)    rng = np.random.RandomState(0)    rng.shuffle(x)    x = np.sort(x[:])    y = f(x)    # Plot random data.    fig, axis = plt.subplots(1, 1, figsize=(20, 10))    axis.plot(x, y, 'o', color='black', markersize=2, label='random data')    # Create bagging models.    model = BaggingRegressor(n_estimators=5, base_estimator=SVR())    x_augmented = np.array([x, x**2, x**3, x**4, x**5]).T    model.fit(x_augmented, y)    # Plot intermediate regression estimations.    axis.plot(x, model.predict(x_augmented), '-', color='red', label=model.__class__.__name__)    for i, tree in enumerate(model.estimators_):        y_pred = tree.predict(x_augmented)        axis.plot(x, y_pred, '--', label='tree '+str(i))    axis.axis('off')    axis.legend()    plt.show()if __name__ == '__main__':    main()哪個可以:裝袋回歸器疊加到所有估計器上
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

我認為您的代碼沒有問題,而是繪圖部分有問題。


# Plot intermediate regression estimations.

    axis.plot(x, treg.predict(x_augmented), '-', color='red', label=model.__class__.__name__)

    for i, tree in enumerate(treg.regressor_['model'].estimators_):

        y_hat = tree.predict(x_augmented)

        y_transformer = preprocessing.MinMaxScaler().fit(y.reshape(-1, 1))

        y_pred = y_transformer.inverse_transform(y_hat.reshape(-1, 1))

        axis.plot(x, y_pred, '--', label='tree '+str(i))

tree這里將是一個,您在上一部分中SVR()預測,并用 a 進行縮放。因此,預測與您的期望不符。x_augmentedx_augmentedStandardScaler


因此,通過使用以下代碼片段更改代碼,就可以了:


# Plot intermediate regression estimations.

axis.plot(x, treg.predict(x_augmented), '-', color='red', label=model.__class__.__name__)

for i, tree in enumerate(treg.regressor_['model'].estimators_):

    x_augmented_scaled = treg.regressor_.named_steps['scale'].transform(x_augmented)

    y_hat = tree.predict(x_augmented_scaled)

    y_transformer = preprocessing.MinMaxScaler().fit(y.reshape(-1, 1))

    y_pred = y_transformer.inverse_transform(y_hat.reshape(-1, 1))

    axis.plot(x, y_pred, '--', label='tree '+str(i))

axis.axis('off')

axis.legend()

plt.show()


查看完整回答
反對 回復 2023-07-05
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號