应用正规方程和梯度下降预估器来预测sklearn中的波士顿房价

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,SGDRegressor
import numpy
def Linear1():
“”"
正规化算法预测波士顿房价
:return:
“”"
# 1.导入数据
boston = load_boston()
#2.划分数据集
x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)

#3.数据标准化
transfor = StandardScaler()
x_train = transfor.fit_transform(x_train)
x_test = transfor.transform(x_test)
#4.预估器
estimator = LinearRegression()
estimator.fit(x_train,y_train)

#5.得出模型
print("正规化-权重系数为:\n",estimator.coef_)
print("正规化-偏置为:\n",estimator.intercept_)
return None

def Linear2():
“”"
正规化算法预测波士顿房价
:return:
“”"
# 1.导入数据
boston = load_boston()
print(“特征数量:\n”,boston.data.shape)
#2.划分数据集
x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)

#3.数据标准化
transfor = StandardScaler()
x_train = transfor.fit_transform(x_train)
x_test = transfor.transform(x_test)
#4.预估器
estimator = SGDRegressor()
estimator.fit(x_train,y_train)

#5.得出模型
print("梯度下降-权重系数为:\n",estimator.coef_)
print("梯度下降-偏置为:\n",estimator.intercept_)
return None

if name==“main”:
#代码1.正规方程预测波士顿房价
Linear1()
# 代码2.梯度下降预测波士顿房价
Linear2()

你可能感兴趣的:(应用正规方程和梯度下降预估器来预测sklearn中的波士顿房价)