使用交叉验证

Keras在房价预测有编写k折交叉验证的函数

for循环k次,每次划分训练和验证集并加以操作

Sklearn有专门函数实现交叉验证

from sklearn.model_selection import cross_val_score

scores = cross_val_score(tree_reg, housing_prepared, housing_labels,
                        scoring="neg_mean_squared_error", cv=10)
tree_rmse_scores = np.sqrt(-scores)

def display_score(scores):
    print("Scores:", scores)
    print("Mean:", scores.mean())
    print("Standard deviation:", scores.std())

display_score(tree_rmse_scores)
pd.Series(np.sqrt(-scores)).describe()
eg:
scores = cross_val_score(lin_reg, housing_prepared, housing_labels, scoring="neg_mean_squared_error", cv=10)
pd.Series(np.sqrt(-scores)).describe()

count       10.000000
mean     69051.635544
std       2880.193332
min      64969.630564
25%      67136.363758
50%      68149.396085
75%      70985.098467
max      74742.024207
dtype: float64