【发布时间】: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