线性回归对波士顿房价进行预测

#!/usr/bin/python
# coding=utf-8
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.linear_model import SGDRegressor
from sklearn.metrics import mean_squared_error

def liner1():
    #线性回归的正规方程优化对波士顿房价进行预测

    #获取数据
    boston=load_boston()
    print "\n", boston.data.shape

    #数据划分
    x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)

    #特征工程:标准化
    transform = StandardScaler()
    x_train = transform.fit_transform(x_train)
    x_test = transform.transform(x_test)

    #线性回归预估器
    estimator = LinearRegression()
    estimator.fit(x_train, y_train)
    print "正规化方程系数:\n", estimator.coef_
    print "正骨化偏重:\n", estimator.intercept_

    #均方误差进行评估
    y_predict = estimator.predict(x_test)
    error = mean_squared_error(y_test, y_predict)
    print "正规方程均方误差:\n", error


    return None

def liner2():
    #线性回归的梯度下降优化对波士顿房价进行预测

    #获取数据
    boston=load_boston()
    print "\n", boston.data.shape

    #数据划分
    x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)

    #特征工程:标准化
    transform = StandardScaler()
    x_train = transform.fit_transform(x_train)
    x_test = transform.transform(x_test)

    #线性回归预估器
    estimator = SGDRegressor()
    estimator.fit(x_train, y_train)
    print "正规化方程系数:\n", estimator.coef_
    print "正骨化偏重:\n", estimator.intercept_

    #模型评估
    # 均方误差进行评估
    y_predict = estimator.predict(x_test)
    error = mean_squared_error(y_test, y_predict)
    print "梯度下降均方误差:\n", error

    return None
liner1()
liner2()

 

posted on 2021-03-13 10:36  一仟零一夜丶  阅读(294)  评论(0)    收藏  举报