<span role="heading" aria-level="2">线性回归案例</span>

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, Ridge, RidgeCV
from sklearn.metrics import mean_squared_error


def linear_model1():
    """
    正规方程
    :return: None
    """
    # 1.获取数据
    boston = load_boston()

    # 2.数据基本处理
    # 2.1 数据集划分
    x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2)

    # 3.特征工程 --标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    # 4.机器学习(线性回归)
    estimator = LinearRegression()
    estimator.fit(x_train, y_train)
    print("这个模型的偏置是:\n", estimator.intercept_)

    # 5.模型评估
    # 5.1 预测值和准确率
    y_pre = estimator.predict(x_test)
    print("预测值是:\n", y_pre)

    score = estimator.score(x_test, y_test)
    print("准确率是:\n", score)

    # 5.2 均方误差
    ret = mean_squared_error(y_test, y_pre)
    print("均方误差是:\n", ret)


def linear_model2():
    """
    梯度下降法
    :return: None
    """
    # 1.获取数据
    boston = load_boston()

    # 2.数据基本处理
    # 2.1 数据集划分
    x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2)

    # 3.特征工程 --标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    # 4.机器学习(线性回归)
    estimator = SGDRegressor(max_iter=1000, learning_rate="constant", eta0=0.001)

    estimator.fit(x_train, y_train)
    print("这个模型的偏置是:\n", estimator.intercept_)

    # 5.模型评估
    # 5.1 预测值和准确率
    y_pre = estimator.predict(x_test)
    print("预测值是:\n", y_pre)

    score = estimator.score(x_test, y_test)
    print("准确率是:\n", score)

    # 5.2 均方误差
    ret = mean_squared_error(y_test, y_pre)
    print("均方误差是:\n", ret)


def linear_model3():
    """
    岭回归
    :return: None
    """
    # 1.获取数据
    boston = load_boston()

    # 2.数据基本处理
    # 2.1 数据集划分
    x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2)

    # 3.特征工程 --标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    # 4.机器学习(线性回归)
    # estimator = Ridge()
    estimator = RidgeCV(alphas=(0.001, 0.1, 1, 10, 100))

    estimator.fit(x_train, y_train)
    print("这个模型的偏置是:\n", estimator.intercept_)

    # 5.模型评估
    # 5.1 预测值和准确率
    y_pre = estimator.predict(x_test)
    print("预测值是:\n", y_pre)

    score = estimator.score(x_test, y_test)
    print("准确率是:\n", score)

    # 5.2 均方误差
    ret = mean_squared_error(y_test, y_pre)
    print("均方误差是:\n", ret)


if __name__ == '__main__':
    linear_model1()
    linear_model2()
    linear_model3()

全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务