zoukankan      html  css  js  c++  java
  • 体验机器学习编程

    最近看有人发了机器学习的培训挺便宜的,就尝试了一下。

    讲的比较基础,很适合入门,用来了解这个行业。即使不从事此行业,用工具去画一个数学模型的图也是好的。

    其他的各种机器学习概念,高数离散等数学基础知识都一样,没什么可写的。

    只有安装的工具和使用方法,值得记录一下。

    1.安装anaconda

    到anaconda官网,下载安装就行,不过记住安装的时候选上注册环境变量,要不然使用起来麻烦。

    2.安装jupyter notebook

    打开命令终端,执行命令conda install jupyter

    3.启动jupyter notebook

    打开命令终端,执行jupyter notebook,然后发现它会自动打开你的浏览器,显示一个页面。

    4.执行程序

    (1)把要分析的文件在操作页面上上传,

    (2)在右侧有个"new"按钮,点击new->python3

    (3)在命令终端上执行机器学习代码:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression
    #显示前几行
    data = pd.read_csv("data/Advertising.csv")
    data.head()
    
    #查看列标题
    data.columns
    
    #数据可视化
    plt.figure(figsize=(16,8))
    plt.scatter(data['TV'],data['sales'],c='black')
    plt.xlabel("Money spent on TV ads")
    plt.ylabel("Sales")
    plt.show()
    
    #训练线性回归模型
    x = data['TV'].values.reshape(-1,1)
    y = data['sales'].values.reshape(-1,1)
    
    reg = LinearRegression()
    reg.fit(x,y)
    
    #打印结果
    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]))
    
    #画出预测曲线
    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")
    plt.ylabel("Sales")
    plt.show()
    
    #预测当投入100亿时的结果
    predictions = reg.predict([[100]])
    print('投入一亿元的电视广告,预计的销售量为{:.5}亿'.format( predictions[0][0]))
    
    predictions = reg.predict([[100],[200],[300]])
    print('投入一亿、二亿、三亿元的电视广告,预计的销售量分别为为{:.5}、{:.5}、{:.5}亿'.format( predictions[0][0], predictions[1][0], predictions[2][0]))
    View Code

    基本的入门流程就是这样,剩下的就是学习各种数学模型,用需求驱动,各种实践了。

    参考地址:

    https://zhuanlan.zhihu.com/c_1122543639770791936

  • 相关阅读:
    图片上传-下载-删除等图片管理的若干经验总结3-单一业务场景的完整解决方案
    图片上传-下载-删除等图片管理的若干经验总结2
    HDU 1195 Open the Lock
    HDU 1690 Bus System
    HDU 2647 Reward
    HDU 2680 Choose the best route
    HDU 1596 find the safest road
    POJ 1904 King's Quest
    CDOJ 889 Battle for Silver
    CDOJ 888 Absurdistan Roads
  • 原文地址:https://www.cnblogs.com/bugutian/p/11081001.html
Copyright © 2011-2022 走看看