线性回归:正规方程法案例_波士顿房价预测

线性回归,正规方程法案例。本例是波士顿房价预测。

import pandas as pd
import numpy as np
# from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from  sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error


# 1、获取数据,因为数据集load_boston在新的版本中已下线,所以通过网址获取
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]

# raw_df.to_csv('./data/boston.csv',index=False)

# 2、划分数据集
x_train,x_test,y_train,y_test=train_test_split(data,target,test_size=0.2,random_state=22)

# 3、数据处理:标准化
process = StandardScaler()
x_train = process.fit_transform(x_train)
x_test = process.transform(x_test)

# 4、创建模型,LinearRegression正规方程法,fit_intercept=True计算偏置
model = LinearRegression(fit_intercept=True)

# 5、模型训练
model.fit(x_train,y_train)
# print(model.coef_)
# print(model.intercept_)

# 6、模型预测
y_predict = model.predict(x_test)
print(y_predict )

# 7、模型评估,用MAE方法
print(mean_squared_error(y_test,y_predict))