# 线性回归解决波士顿房价预测问题

# 一、数据集

波士顿房价预测也是个经典的数据集,利用sklearn.datasetssklearn.datasets 的数据集直接进行训练

数据集获取以及相关库导入

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import sklearn.datasets as datasets
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from sklearn.linear_model import LinearRegression
def load_dataset():
	boston = datasets.load_boston()
	feature_name = boston['feature_names']
	train = boston.data
	target = boston.target
	X_train,X_test,Y_train,Y_test = train_test_split(train,target,test_size=0.3,random_state=666)
	return X_train,X_test,Y_train,Y_test
	# 0.3 的测试集,0.7 的训练集

# 二、Lasso 回归预测

# 1、利用 sklearn 库进行预测

def Lasso_train(X_train,Y_train): # 训练模型
	lasso = Lasso()
	lasso.fit(X_train,Y_train)
	predict = lasso.predict(X_train)
	return lasso,predict
def Lasso_draw(Y_test,Y_predict):
	plt.plot(Y_test,label='Y_True')
	plt.plot(Y_predict,'r:',label='Y_Predict')
	plt.legend()
	plt.show()
X_train,X_test,Y_train,Y_test = load_dataset()
lasso,lasso_predict = Lasso_train(X_train,Y_train)
print('w=',lasso.coef_) # w 向量
print('b=',lasson.intercept_)  # b 向量
print("训练集得分:{:.2f}".format(lasso.score(X_train, Y_train)))
print("测试集得分:{:.2f}".format(lasso.score(X_test, Y_test)))
Lasso_draw()

# 2、输出图片如下:

输出图片

# 三、Ridge 回归预测

# 1、利用 sklearn 库进行检测

def Ridge_train(X_train,Y_train): # 训练模型
	ridge = Ridge(alpha=1,max_iter=1000)
	# alpha: 正则化力度,必须是一个浮点整数。正则化提升问题条件,减少估计器的方差
	# max_iter: 共轭梯度求解器的最大迭代次数
	ridge.fit(X_train,Y_train)
	predict = lasso.predict(X_train)
	return ridge,predict
def Ridge_draw(Y_test,Y_predict):
	plt.plot(Y_test,label='Y_True')
	plt.plot(Y_predict,'r:',label='Y_Predict')
	plt.legend()
	plt.show()
X_train,X_test,Y_train,Y_test = load_dataset()
Ridge,Ridge_predict = Ridge_train(X_train,Y_train)
print('w=',Ridge.coef_) # w 向量
print('b=',Ridge.intercept_)  # b 向量
print("训练集得分:{:.2f}".format(Ridge.score(X_train, Y_train)))
print("测试集得分:{:.2f}".format(Ridge.score(X_test, Y_test)))
Ridge_draw()

# 2、输出图片如下:

输出图片

# 四、Linear_Regression 多元回归预测

# 1、利用 sklearn 库进行检测

def Linear_train(X_train,Y_train): # 训练模型
	linear = LinearRegression()
	# alpha: 正则化力度,必须是一个浮点整数。正则化提升问题条件,减少估计器的方差
	# max_iter: 共轭梯度求解器的最大迭代次数
	linear.fit(X_train,Y_train)
	predict = linear.predict(X_train)
	return linear,predict
def Linear_draw(Y_test,Y_predict):
	plt.plot(Y_test,label='Y_True')
	plt.plot(Y_predict,'r:',label='Y_Predict')
	plt.legend()
	plt.show()
X_train,X_test,Y_train,Y_test = load_dataset()
linear,linear_predict = Linear_train(X_train,Y_train)
print('w=',linear.coef_) # w 向量
print('b=',linear.intercept_)  # b 向量
print("训练集得分:{:.2f}".format(linear.score(X_train, Y_train)))
print("测试集得分:{:.2f}".format(linear.score(X_test, Y_test)))
Linear_draw()

# 2、输出图片如下:

输出图片