20190701——波士顿房价预测

20190701——波士顿房价预测_第1张图片

流程分析
获取数据集
划分数据集
特征工程:
无量纲化 ——标准化
预估器流程
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()

你可能感兴趣的:(黑马人工智能)