# coding=utf-8
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 正规方程预测房价
def myliner():
"""
这是一个问题
:return:
"""
print("-"*100)
# 获取数据
lb = load_boston()
# 数据分割
x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)
# 数据标准化处理 -> 目标值与特征值一起标准化
# print(y_train, y_test)
std_x = StandardScaler()
x_train = std_x.fit_transform(x_train)
x_test = std_x.transform(x_test)
std_y = StandardScaler()
y_train = std_y.fit_transform(y_train.reshape(-1, 1))
y_test = std_y.transform(y_test.reshape(-1, 1))
# print("-"*100)
# print(y_train, y_test)
# 开始学习
lr = LinearRegression()
lr.fit(x_train, y_train)
print(lr.coef_)
# 预测结果
y_predict = lr.predict(x_test)
print("预测结果: ", y_predict)
# 梯度下降预测
sgd = SGDRegressor()
sgd.fit(x_train, y_train)
print(sgd.coef_)
y_predict2 = sgd.predict(x_test)
print("预测结果2:", y_predict2)
print("-"*100)
# 岭回归
rd = Ridge(alpha=1.0)
rd.fit(x_train, y_train)
print(rd.coef_)
print(rd.predict(x_test))
return None
# 最小二乘法?
if __name__ == "__main__":
"""
线性回归:目标值是连续的
"""
myliner()