1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import matplotlib.pyplot as plt from sklearn.datasets import fetch_california_housing from sklearn.model_selection import train_test_split from sklearn.linear_model import linearregression from sklearn.metrics import mean_squared_error
def main():
housing_22 = fetch_california_housing() x_22, y_22 = housing_22.data, housing_22.target
x_train_22, x_test_22, y_train_22, y_test_22 = train_test_split( x_22, y_22, test_size=0.2, random_state=42)
model_22 = linearregression()
model_22.fit(x_train_22, y_train_22)
y_pred_22 = model_22.predict(x_test_22)
mse_22 = mean_squared_error(y_test_22, y_pred_22) print(f'mean squared error on test set: {mse_22}')
plt.scatter(y_test_22, y_pred_22) plt.xlabel('true values') plt.ylabel('predictions') plt.title('true values vs. predictions') plt.show()
if __name__ == "__main__": main()
|