【发布时间】:2022-03-02 18:42:22
【问题描述】:
我正在试验MultiOutputRegressor(),我想知道一旦测量到损失,是否有可能在多输出回归任务的训练集和测试集上达到损失曲线。
我尝试过的:
import matplotlib.pyplot as plt
import numpy as np
#from sklearn import datasets, ensemble
#from sklearn.inspection import permutation_importance
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.multioutput import MultiOutputRegressor
from sklearn.linear_model import Ridge
# Load the data
#diabetes = datasets.load_diabetes()
#X, y = diabetes.data, diabetes.target
from sklearn.datasets import load_linnerud
X, y = load_linnerud(return_X_y=True)
#Data preprocessing
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.1, random_state=13
)
params = {
"n_estimators": 500,
"max_depth": 4,
"min_samples_split": 5,
"learning_rate": 0.01,
"loss": "squared_error",
}
# Fit regression model
#reg = ensemble.GradientBoostingRegressor(**params).fit(X_train, y_train)
# Fit MultiOutput regression model
reg = MultiOutputRegressor(Ridge(random_state=123)).fit(X_train, y_train)
mse = mean_squared_error(y_test, reg.predict(X_test))
print("The mean squared error (MSE) on test set: {:.4f}".format(mse))
#The mean squared error (MSE) on test set: 37.6474
所以我试图激发post 并尝试以下方法:
# Plot training deviance
test_score = np.zeros((params["n_estimators"]), dtype=np.float64)
for i, y_pred in enumerate(reg.staged_predict(X_test)):
test_score[i] = reg.loss_(y_test, y_pred)
fig = plt.figure(figsize=(6, 6))
plt.subplot(1, 1, 1)
plt.title("Deviance")
plt.plot(np.arange(params["n_estimators"]) + 1,
reg.train_score_,
"b-",
label="Training Set Deviance")
plt.plot(np.arange(params["n_estimators"]) + 1,
test_score, "r-",
label="Test Set Deviance")
plt.legend(loc="upper right")
plt.xlabel("Boosting Iterations")
plt.ylabel("Deviance")
fig.tight_layout()
plt.show()
我遇到了这个错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-5db7534b636e> in <module>()
1 test_score = np.zeros((params["n_estimators"],), dtype=np.float64)
----> 2 for i, y_pred in enumerate(reg.staged_predict(X_test)):
3 test_score[i] = reg.loss_(y_test, y_pred)
4
5 fig = plt.figure(figsize=(6, 6))
AttributeError: 'MultiOutputRegressor' object has no attribute 'staged_pre
我检查了这个post 试过了:
import matplotlib.pyplot as plt
plt.plot(reg.loss_curve_)
#plt.plot(reg.validation_scores_)
但面对:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-25-a6d600185ec3> in <module>()
42
43 # Plot training deviance
---> 44 plt.plot(reg.loss_curve_)
AttributeError: 'MultiOutputRegressor' object has no attribute 'loss_curve_'
我不确定我是否可以使用基于 keras 的方法来收集跨时期的评估(例如 MSE),正如 asnwer 中提到的那样。因为我能够计算mse,所以它应该是plot loss curves 的一种方法,用于(多输出)回归任务,比如时期/训练迭代?我也进行了一些搜索,发现了一些 post 和 post2,但我无法达到适合我的问题的输出。
【问题讨论】:
标签: python machine-learning scikit-learn regression loss-function