美文网首页
1、机器学习入门

1、机器学习入门

作者: 波罗的海de夏天 | 来源:发表于2020-05-08 15:21 被阅读0次

    一、什么是机器学习?

    • 机器学习是人工智能(AI)的一部分,研究如何让计算机从数据学习某种规律
    • 机器学习并不是人工智能的全部,也不等同于人工智能;
    • 人工智能 > 机器学习 > 深度学习
    人工智能范畴.png

    备注:手工创建的规则,属于AI,不属于ML

    二、机器学习 VS 数据挖掘 VS 大数据

    机器学习 VS 大数据.png

    三、理解机器学习

    • 通过计算机程序根据数据去优化一个评价指标;
    • 自动的从数据发现规律,使用这些规律作出预测;
    • 根据过去预测未来。

    机器学习别名:

    • 推理/估计:统计学
    • 模式识别

    四、机器学习家族

    • 监督式学习 -- 训练数据包括输入和预期的输出
      • 分类
        • 垃圾邮件/短信检测
        • 自动车牌号识别
        • 人脸识别
        • 手写字符识别
        • 语音识别
        • 医疗图片的病症诊断
        • ......
      • 回归
        • 二手车股价
        • 股票价格预测
        • 气温预测
        • 自动驾驶
        • ......
    • 非监督式学习 -- 训练数据只有输入,没有预期的输出
      • 聚类: 把对象分成不同的子集(subset),使得属于同一个子集中的成员对象都有相似的一些属性

    五、机器学习流程

    机器学习流程.png
    1. 数据获取
    2. 数据清洗
    3. 特征工程
    4. 预处理
      • 特征提取
      • 处理缺失数据
      • 数据定标
        • 归一化
        • 标准化
      • 数据转换
    5. 选择机器学习模型
    6. 模型训练 <==> 模型调参
    7. 模型部署

    六、线性回归示例 1

    Question:

    你所在的公司在电视上做产品广告, 收集到了电视广告投入x(以百万为单位)与产品销售量y(以亿为单位)的数据. 你作为公司的数据科学家, 希望通过分析这些数据, 了解电视广告投入x(以百万为单位)与产品销售量y的关系.

    假设x与y的之间的关系是线性的, 也就是说 y = ax + b. 通过线性回归(Linear Regression), 我们就可以得知 a 和 b 的值. 于是我们在未来做规划的时候, 通过电视广告投入x, 就可以预测产品销售量y, 从而可以提前做好生产和物流, 仓储的规划. 为客户提供更好的服务.

    data:
        TV  sales
    0   230.1   22.1
    1   44.5    10.4
    2   17.2    9.3
    3   151.5   18.5
    4   180.8   12.9
    5   8.7 7.2
    6   57.5    11.8
    7   120.2   13.2
    8   8.6 4.8
    9   199.8   10.6
    10  66.1    8.6
    ... ... ...
    

    Script Demo:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression
    
    1. 加载数据
    # 加载数据
    data = pd.read_csv('data/Advertising.csv')
    # 查看部分数据
    data.head()
    # 查看列索引
    data.columns
    # 查看行索引
    data.index
    
    # 通过数据可视化分析数据
    plt.figure(figsize=(16, 8))
    plt.scatter(data['TV'], data['sales'], c='blue')
    plt.xlabel('Money spent on TV ads (million)')
    plt.ylabel('Sales (hundred million)')
    plt.show()
    

    散点图

    数据可视化.png
    1. 训练线性回归数据
    # 训练线性回归数据
    X = data['TV'].values.reshape(-1, 1)
    y = data['sales'].values.reshape(-1, 1)
    
    reg = LinearRegression()
    reg.fit(X, y)
    
    1. 打印线性模型
    reg.coef_[0][0]
    reg.intercept_[0]
    print('a = {:.5}'.format(reg.coef_[0][0]))
    print('b = {:.5}'.format(reg.intercept_[0]))
    print('线性模型:y = {:.5}X + {:.5}'.format(reg.coef_[0][0], reg.intercept_[0]))
    

    打印结果:
    >>> a = 0.047537
    >>> b = 7.0326
    >>> 线性模型:y = 0.047537X + 7.0326

    1. 可视化训练好的线性回归模型
    # 可视化训练好的线性回归模型
    predictions = reg.predict(X)
    plt.figure(figsize=(16, 8))
    plt.scatter(data['TV'], data['sales'], c='black')
    plt.plot(data['TV'], predictions, c='blue', linewidth=2)
    plt.xlabel('Money spent on TV ads (million)')
    plt.ylabel('Sales (hundred million)')
    plt.show()
    

    散点图

    模型可视化.png
    1. 预测
    test = [[100], [200], [300]]
    predictions = reg.predict(test)
    for investment, prediction in zip(test, predictions):
        print('>>> 投入{:.2}亿元,预计销售量:{:.5}'.format(investment[0]/100.0, prediction[0]))
    

    打印结果:
    >>> 投入1.0亿元,预计销售量:11.786
    >>> 投入2.0亿元,预计销售量:16.54
    >>> 投入3.0亿元,预计销售量:21.294

    七、线性回归示例 2

    Question:

    气温会随着海拔高度的升高而降低, 我们可以通过测量不同海拔高度的气温来预测海拔高度和气温的关系. 我们假设海拔高度和气温的关系可以使用如下公式表达: y(气温) = a * x(海拔高度) + b

    理论上来讲, 确定以上公式 a 和 b的值只需在两个不同高度测试, 就可以算出来 a 和 b 的值了. 但是由于所有的设备都是有误差的, 而使用更多的高度测试的值可以使得预测的值更加准确. 我们提供了在9个不同高度测量的气温值, 请你根据今天学习的线性回归方法预测 a 和 b 的值. 根据这个公式, 我们预测一下在8000米的海拔, 气温会是多少?

    data:
    height  temperature
    0   0.0 12.834044
    1   500.0   10.190649
    2   1000.0  5.500229
    3   1500.0  2.854665
    4   2000.0  -0.706488
    5   2500.0  -4.065323
    6   3000.0  -7.127480
    7   3500.0  -10.058879
    8   4000.0  -13.206465
    

    Script Demo:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression
    
    1. 加载数据
    # 加载数据
    data = pd.read_csv('exercise/height.vs.temperature.csv')
    # 查看部分数据
    data.head()
    # 查看列索引
    data.columns
    # 查看行索引
    data.index
    
    1. 可视化分析数据
    plt.figure(figsize=(16, 8))
    plt.scatter(data['height'], data['temperature'], c='blue')
    plt.xlabel('height (m)')
    plt.ylabel('temperature (℃)')
    plt.show()
    

    散点图

    数据可视化.png
    1. 训练线性回归模型
    X = data['height'].values.reshape(-1, 1)
    y = data['temperature'].values.reshape(-1, 1)
    reg = LinearRegression()
    reg.fit(X, y)
    
    1. 打印线性模型
    reg.coef_[0][0]
    reg.intercept_[0]
    print('>>> a = {:.6}'.format(reg.coef_[0][0]))
    print('>>> b = {:.6}'.format(reg.intercept_[0]))
    print('>>> 线性模型:y = {:.6}X + {:.6}'.format(reg.coef_[0][0], reg.intercept_[0]))
    

    打印结果:
    >>> a = -0.00656953
    >>> b = 12.7185
    >>> 线性模型:y = -0.00656953X + 12.7185

    1. 可视化线性回归模型
    predictions = reg.predict(X)
    plt.figure(figsize=(16, 8))
    plt.scatter(data['height'], data['temperature'], c='black')
    plt.plot(data['height'], predictions, c='blue', linewidth=2)
    plt.show()
    

    散点图

    模型可视化.png
    1. 预测
    test = [[8000], [9000], [10000]]
    predictions = reg.predict(test)
    for height, temperature in zip(test, predictions):
        print(height)
        print(temperature)
        print('>>> 海拔{}m,气温为:{:.6}℃'.format(height[0], temperature[0]))
    

    打印结果:
    >>> 海拔8000m,气温为:-39.8378℃
    >>> 海拔9000m,气温为:-46.4073℃
    >>> 海拔10000m,气温为:-52.9768℃

    相关文章

      网友评论

          本文标题:1、机器学习入门

          本文链接:https://www.haomeiwen.com/subject/rrmxnhtx.html