20190701——波士顿房价预测

在这里插入图片描述

流程分析
获取数据集
划分数据集
特征工程:
无量纲化 ——标准化
预估器流程
fit()
coef_ intercept_
模型评估

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
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)标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    #4)预估器
    estimator = LinearRegression()
    estimator.fit(x_train, y_train)
    #5)得出模型
    print("正规方程权重系数为:\n", estimator.coef_)
    print("正规方程偏置为:\n", estimator.intercept_)
    #6)模型评估

    return None


def linear2():
    """
    用梯度下降的优化方法对波士顿放假进行预测
    :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)标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)
    # 4)预估器
    estimator = SGDRegressor()
    estimator.fit(x_train, y_train)
    # 5)得出模型
    print("梯度下降权重系数为:\n", estimator.coef_)
    print("梯度下降偏置为:\n", estimator.intercept_)
    # 6)模型评估

    return None
if __name__ == 'main':
    #代码1 用正规方程的优化方法对波士顿放假进行预测
    linear1()
    #代码2 用梯度下降的优化方法对波士顿放假进行预测
    linear2()
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值