【问题标题】:Regression validation score doesn't look good回归验证分数看起来不太好
【发布时间】:2022-01-17 19:08:57
【问题描述】:

我有问题。当您查看第一张照片时,您会发现验证分数并不好看。 当我注释掉这一行 plt.plot(np.sqrt(val_errors), "b-", linewidth=3, label="val") 时,您可以完美地看到训练图。 为什么 val 看起来那么大?

dfListingsFeature_regression = pd.read_csv(r"https://raw.githubusercontent.com/Coderanker3/dataset4/main/listings_cleaned.csv")
d = {True: 1, False: 0, np.nan : np.nan} 
dfListingsFeature_regression['host_is_superhost'] = dfListingsFeature_regression[
                                                             'host_is_superhost'].map(d).astype('int')

X = dfListingsFeature_regression.drop(columns=['host_id', 'id', 'price']) # Features
y = dfListingsFeature_regression['price'] # Target variable
print(dfListingsFeature_nor.shape)

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

def plot_learning_curves(model, X, y):
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=10)
    train_errors, val_errors = [], []
    for m in range(1, 1000 + 1):
        model.fit(X_train[:m], y_train[:m])
        y_train_predict = model.predict(X_train[:m])
        y_val_predict = model.predict(X_val)
        train_errors.append(mean_squared_error(y_train[:m], y_train_predict))
        val_errors.append(mean_squared_error(y_val, y_val_predict))

    plt.figure( figsize=(20,20))
    plt.plot(np.sqrt(train_errors), "r-+", linewidth=2, label="train")
    plt.plot(np.sqrt(val_errors), "b-", linewidth=3, label="val")
    plt.legend(loc="upper right", fontsize=14)   
    plt.xlabel("Training set size", fontsize=14) 
    plt.ylabel("RMSE", fontsize=14)          

lin_reg = LinearRegression(copy_X=True, fit_intercept=True,n_jobs=1, normalize=True)
plot_learning_curves(lin_reg, X, y)
#plt.axis([0, 80, 0, 3])                       
plt.show()   

【问题讨论】:

  • 不稳定是什么意思?随着模型更好地学习泛化数据,验证分数将下降。
  • 好吧,也许你是对的,我更正了不稳定这个词。是的,那是正确的。当模型知道要做什么时,我希望看到模型的减少和增加。但是在第一张图中,我无法从中准备任何有用的东西,而第二张图清楚地显示了红线在增加。我想优化第一张图片的视图。那将非常有帮助。谢谢!

标签: python testing scikit-learn regression training-data


【解决方案1】:

你可以简单地在y轴上设置一个logarithmic scale

plt.yscale("log")

或将其与删除低于某个分位数的 RMSE 值结合起来以更好地可视化

import numpy as np

# Remove errors beyon 80% quantile
val_errors_quantile = np.array(val_errors)[np.where(val_errors < np.quantile(val_errors, 0.8))]

plt.figure( figsize=(12,12))
plt.plot(np.sqrt(val_errors_sample), "b-", linewidth=3, label="val")
plt.legend(loc="upper right", fontsize=14)   
plt.xlabel("Training set size", fontsize=14) 
plt.ylabel("RMSE", fontsize=14)
plt.yscale("log")
plt.show() 

这可以更好地解释 RMSE 值的下降

如果您对如何扩展它有其他想法,可以将custom function 传递给plt.yscale

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多