线性回归、岭回归、多项式回归估计boston房价[sklearn]


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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值