线性回归-机器学习

# 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()

 

发布了124 篇原创文章 · 获赞 11 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览