import pandas as pd
import numpy as np
import sklearn.model_selection as sm
import sklearn.linear_model as lm
import sklearn.preprocessing as sp
from sklearn.metrics import r2_score
from sklearn.pipeline import make_pipeline
data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]
(x_train, x_test, y_train, y_test) = sm.train_test_split(data, target, test_size=0.2, random_state=2)
boston_lr_model = lm.LinearRegression()
boston_lr_model.fit(x_train,y_train)
yhat= boston_lr_model.predict(x_train)
pred_y = boston_lr_model.predict(x_test)
print("训练集:", r2_score(y_train, yhat))
print("测试集:", r2_score(y_test, pred_y))
boston_ridge_model = lm.Ridge(alpha=0.5)
boston_ridge_model.fit(x_train, y_train)
yhat = boston_ridge_model.predict(x_train)
pred_y = boston_ridge_model.predict(x_test)
print("训练集:", r2_score(y_train, yhat))
print("测试集:", r2_score(y_test, pred_y))
boston_pr_model = make_pipeline(sp.PolynomialFeatures(degree=2),lm.LinearRegression())
boston_pr_model.fit(x_train,y_train)
yhat = boston_pr_model.predict(x_train)
pred_y = boston_pr_model.predict(x_test)
print("训练集:", r2_score(y_train, yhat))
print("测试集:", r2_score(y_test, pred_y))