• 机器学习预测汽车油耗效率 MPG


    流程

    1. 数据获取
    2. 导入需要的包
    3. 引入文件,查看内容
    4. 划分训练集和测试集
    5. 调用模型
    6. 查看准确率

    数据获取

    链接:https://pan.baidu.com/s/1KeIJykbcVpsfEk0xjhiICA?pwd=30oe 
    提取码:30oe 
    --来自百度网盘超级会员V1的分享
    
    • 1
    • 2
    • 3

    导入需要的包

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    
    • 1
    • 2
    • 3
    • 4
    • 5

    引入文件,查看内容

    path = 'auto-mpg.data'
    columns = ["mpg", "cylinders", "displacement", "horsepower", "weight", "acceleration", "model year", "origin", "car name"]
    cars = pd.read_csv(path, delim_whitespace=True, names=columns)
    cars.head()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    划分训练集和测试集

    这里先用重量做特征

    Y = cars['mpg']
    X = cars[['weight']]
    
    • 1
    • 2
    X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.2,random_state=0)
    
    • 1

    引入模型

    线性回归

    lr = LinearRegression()
    lr = lr.fit(X_train,Y_train)
    
    • 1
    • 2

    查看准确率

    文字

    print('score = {}'.format(lr.score(X,Y)))
    #score = 0.691680406988993
    
    • 1
    • 2

    可视化查看

    plt.scatter(X_test, Y_test, color = 'red', alpha=0.3)
    plt.scatter(X_test, lr.predict(X_test),color = 'green',alpha=0.3)
    plt.xlabel('weight')
    plt.ylabel('mpg')
    plt.title('test data')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    准确率只有0.69因为只用到了weight

    此时使用多变量线性回归

    选三个变量建模

    cars = cars[cars.horsepower != '?']
    mul = ['weight','horsepower','displacement'] # 选择三个变量进行建立模型
    mul_lr = LinearRegression()
    mul_lr.fit(cars[mul],cars['mpg']) # 训练模型
    cars['mpg_prediction'] = mul_lr.predict(cars[mul])
    cars.head()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    预测准确率

    mul_score = mul_lr.score(cars[mul],cars['mpg'])
    mul_score
    #0.7069554693444708
    
    • 1
    • 2
    • 3

    从这里可以看出准确率上升了一个点

    fig = plt.figure(figsize = (8,10))
    ax1 = fig.add_subplot(3,1,1)
    ax2 = fig.add_subplot(3,1,2)
    ax3 = fig.add_subplot(3,1,3)
    ax1.scatter(cars['weight'], cars['mpg'], c='blue', alpha=0.3)
    ax1.scatter(cars['weight'], cars['mpg_prediction'], c='red', alpha=0.3)
    ax1.set_title('weight')
    ax2.scatter([ float(x) for x in cars['horsepower'].tolist()], cars['mpg'], c='blue', alpha=0.3)
    ax2.scatter([ float(x) for x in cars['horsepower'].tolist()], cars['mpg_prediction'], c='red', alpha=0.3)
    ax2.set_title('horsepower')
    ax3.scatter(cars['displacement'], cars['mpg'], c='blue', alpha=0.3)
    ax3.scatter(cars['displacement'], cars['mpg_prediction'], c='red', alpha=0.3)
    ax3.set_title('displacement')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

  • 相关阅读:
    记录 | 修改docker存储路径
    Spring Authorization Server优化篇:Redis值序列化器添加Jackson Mixin,解决Redis反序列化失败问题
    Morris遍历
    LC15.三数之和、LC22括号生成
    Mybatis实现分页查询
    万界星空科技电机行业MES+商业电机行业开源MES+项目合作
    谷粒商城 (十六) --------- 商品服务 API 品牌管理 ② OSS 云存储开通整合
    速盾:cdn与云服务区别有哪些方面?
    element-china-area-data插件vue3做省市区的下拉选择,用3个独立的el-select实现
    下载安全证书到jdk中的cacerts证书库
  • 原文地址:https://blog.csdn.net/weixin_45079974/article/details/137964592