国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

數(shù)據(jù)分析:人工智能篇

這篇具有很好參考價值的文章主要介紹了數(shù)據(jù)分析:人工智能篇。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

第三章 數(shù)據(jù)可視化庫matplotlib

3.1 matplotlib基本繪圖操作

  • import matplotlib.pyplot as plt
    import numpy as np
    # 中文設(shè)置
    plt.rcParams['font.sans-serif'] =['KaiTi'] # 指定默認字體
    plt.rcParams['axes.unicode_minus'] = False #解決保存圖像是負號'-'顯示為方塊的問題
    
    # 設(shè)置圖形大小,即設(shè)置畫布
    plt.figure(figsize=(10,5))
    
    # 畫直線
    x = np.arange(10)
    y = 2 * x + 10
    plt.plot(x, y) # 畫圖
    
    # x,y軸的名稱
    plt.xlabel('x')
    plt.ylabel('y')
    
    # 標題
    plt.title("簡單的直線")
    
    plt.show() # 展示圖形
    

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

3.2 plot的線條和顏色

  1. 線條形狀設(shè)置

    • 字符 線條類型 字符 線條類型
      ‘-’ 實線 ‘–’ 虛線
      ‘-.’ 虛點線 ‘:’ 點線
      ‘.’ ‘,’ 像素點
      ‘o’ 圓點 ‘v’ 下三角點
      ‘^’ 上三角形 ‘<’ 左三角形
      ‘>’ 右三角形 ‘1’ 下三叉點
      ‘2’ 上三叉點 ‘3’ 左三叉點
      ‘4’ 右三叉點 ‘s’ 正方點
      ‘p’ 五角點 ‘*’ 星形點
      ‘h’ 六邊形點 ‘H’ 六邊形點2
      ‘+’ +號點 ‘x’ 乘號點
      ‘D’ 實習菱形點 ‘d’ 瘦菱形點
  2. 常用顏色縮寫

    • 字符 顏色 英文全稱
      ‘b’ 藍色 blue
      ‘g’ 綠色 green
      ‘r’ 紅色 reed
      ‘c’ 青色 cyan
      ‘m’ 品紅 magenta
      ‘y’ 黃色 yellow
      ‘k’ 黑色 black
      ‘w’ 白色 white
  3. 示例:

    • import matplotlib.pyplot as plt
      import numpy as np
      x = np.linspace(-10,10)
      len(x)
      
      ## 50
      
      y = np.sin(x)
      len(y)
      
      ## 50
      
      
      # 設(shè)置圖形大小,即設(shè)置畫布
      plt.figure(figsize=(10,5))
      
      # plt.plot(x, y,'-.',color='r') # 畫圖
      plt.plot(x, y,'b-.') 
      
      # 標題
      plt.title("正弦函數(shù)")
      # x,y軸的名稱
      plt.xlabel('x')
      plt.ylabel('y')
      
      plt.show() ## 展示圖片
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

3.3 條形圖分析

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  1. 示例一:

    • import matplotlib.pyplot as plt
      import numpy as np
      
      # 中文設(shè)置
      plt.rcParams['font.sans-serif'] = ['KaiTi'] # 設(shè)置默認字體
      plt.rcParams['axes.unicode_minus'] = False #解決保存圖像是負號'-'顯示為方塊的問題
      
      x = ['北京', '上海', '深圳', '廣州']
      y = [20, 18, 21, 18]
      
      # 設(shè)置畫布
      plt.figure(figsize=(10,6))
      # 設(shè)置標題
      plt.title('各個城市的銷量',fontsize=16) ## fontsize設(shè)置字體大小
      # 畫條形圖
      plt.bar(x, y)
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

      # 設(shè)置畫布
      plt.figure(figsize=(10,6))
      # 設(shè)置標題
      plt.title('各個城市的銷量',fontsize=16) ## fontsize設(shè)置字體大小
      # 畫條形圖
      plt.barh(x, y) 
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  2. 示例二(數(shù)據(jù)實操):

    • # 讀取data文件夾下面的學(xué)生信息表
      import pandas as pd
      data = pd.read_excel("data/學(xué)生信息.xlsx")
      data
      
      ## 
      	班級	學(xué)號	性別	身高	體重
      0	11101173	63
      1	11102192	73
      2	11103186	82
      3	11104167	81
      4	11105159	64
      5	21201188	68
      6	21202176	94
      7	21203160	53
      8	21204162	63
      9	21205167	63
      10	31301161	68
      11	31302175	57
      12	31303188	82
      13	31304195	70
      14	31305187	69
      15	12101174	84
      16	12102161	61
      17	12103157	61
      18	12104159	97
      19	12105170	81
      20	22201193	100
      21	22202194	77
      22	22203155	91
      23	22204175	74
      24	22205183	76
      25	32301157	78
      26	32302171	88
      27	32303190	99
      28	32304164	81
      29	32305187	73
      30	42401192	62
      31	42402166	82
      32	42403158	60
      33	42404160	84
      34	42405193	54
      
    • # 查看有多少個班級,分析各個班學(xué)生的身高分布(即平均值)
      data['班級'].unique() ##array(['1班', '2班', '3班', '4班'], dtype=object)
      a = data.groupby("班級")["身高"].mean().reset_index()
      a
      
      ##
      	班級	身高
      0	1169.8
      1	2175.3
      2	3177.5
      3	4173.8
      
      plt.figure(figsize=(10,6))
      plt.bar(a["班級"],a["身高"])
      plt.show()
      
      
      
      
      # 分析各個班級的體重
      b = data.groupby("班級")["體重"].mean().reset_index()
      b
      
      ##
      	班級	體重
      0	174.7
      1	275.9
      2	376.5
      3	468.4
      
      plt.figure(figsize=(10,6))
      plt.bar(b["班級"],b["體重"])
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

3.4 箱型圖分析

  1. 箱型圖

    • 反映一組數(shù)據(jù)的分布特征,如:分布是否對稱,是否存在異常點;
    • 對多維數(shù)據(jù)的分布可以進行比較;
    • 針對連續(xù)性變量分析;

    數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  2. 示例:

    • ## data文件夾下的箱型圖數(shù)據(jù).xlsx
      import matplotlib.pyplot as plt
      import numpy as np
      import pandas as pd
      
      data = pd.read_excel("data/箱型圖數(shù)據(jù).xlsx")
      data
      
      ##
      	數(shù)據(jù)1			數(shù)據(jù)2		數(shù)據(jù)3
      0	0.673772	2.877434	2.049346
      1	2.094364	1.744089	-2.000739
      2	-0.229255	-3.478537	-1.174358
      3	0.162415	-0.161255	-0.192022
      4	1.601201	0.249620	-3.260043
      ...	...	...	...
      95	0.802054	-2.125556	4.469550
      96	0.704063	-0.020990	-0.325966
      97	-1.003454	-0.645414	-3.517653
      98	1.009918	1.299786	1.303022
      99	0.798712	2.160066	4.128328
      100 rows × 3 columns
      
      
      
      # 單個
      plt.boxplot(data["數(shù)據(jù)1"])
      plt.show()
      
      # 三個放一起
      plt.boxplot([data["數(shù)據(jù)1"],data["數(shù)據(jù)2"],data["數(shù)據(jù)3"]],labels=["數(shù)據(jù)1", "數(shù)據(jù)2", "數(shù)據(jù)3"])
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

3.5 直方圖分析

  1. 直方圖:

    • 直方圖又稱頻率分布圖,是一種顯示數(shù)據(jù)分布情況的柱形圖,即不同數(shù)據(jù)出現(xiàn)的頻率

    • 通過這些高度不同的柱形,可以直觀、快速地觀察數(shù)據(jù)的分散程度和中心趨勢,從而分析流程滿足客戶的程度

    • 數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  2. 示例(數(shù)據(jù)實操):

    • # data文件夾下的直方圖數(shù)據(jù)下有兩個xlsx文件
      import matplotlib.pyplot as plt
      import pandas as pd
      
      data = pd.read_excel("data/直方圖數(shù)據(jù)/乘客信息.xlsx")
      data
      
      ##
      	乘客編號	年齡
      0	1	22
      1	2	38
      2	3	26
      3	4	35
      4	5	35
      ...	...	...
      709	886	39
      710	887	27
      711	888	19
      712	890	26
      713	891	32
      714 rows × 2 columns
      
      
      data1 = pd.read_excel("data/直方圖數(shù)據(jù)/學(xué)生分數(shù).xlsx")
      data1
      
      ## 
      學(xué)生編號	分數(shù)
      0	162	56
      1	129	28
      2	25	2
      3	114	21
      4	130	29
      ...	...	...
      185	178	78
      186	148	42
      187	123	25
      188	82	10
      189	30	3
      190 rows × 2 columns
      
    • # 分析其年齡分布情況
      
      # 中文設(shè)置
      plt.rcParams["font.sans-serif"] = ["KaiTi"] # 設(shè)置默認字體
      plt.rcParams["axes.unicode_minus"] = False # 解決"-"號顯示為方塊的問題
      
      plt.hist(data["年齡"], bins=20, density=True, color='r', edgecolor='k') 
      # bins表示區(qū)間數(shù) # density表示對直方圖作出規(guī)劃(縱軸變?yōu)轭l率了)# edgecolor表示對邊緣加顏色
      plt.xlabel("年齡")
      plt.ylabel("頻率")
      plt.title("直方圖")
      plt.show() 
      
      
      
      # 分析班級里面學(xué)生成績的分布情況
      plt.hist(data1["分數(shù)"], bins=50, density=True,color='y',edgecolor='k')
      plt.xlabel("分數(shù)")
      plt.ylabel("頻率")
      plt.title("直方圖")
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

3.6 散點圖分析

  1. 散點圖:

    • 用兩組數(shù)據(jù)構(gòu)成多個坐標點,考察坐標點之間的分布,判斷兩變量之間是否存在某種關(guān)聯(lián)或總結(jié)坐標點的分布模式
    • 散點圖主要用來研究兩個連續(xù)性變量之間的關(guān)系
    • 數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn
  2. 示例(數(shù)據(jù)實操):

    • # data文件夾下的國民經(jīng)濟核算季度數(shù)據(jù).xlsx
      import matplotlib.pyplot as plt
      import pandas as pd
      
      data = pd.read_excel("data/國民經(jīng)濟核算季度數(shù)據(jù).xlsx")
      data.columns
      
      ##
      Index(['序號', '時間', '國內(nèi)生產(chǎn)總值_當季值(億元)', '第一產(chǎn)業(yè)增加值_當季值(億元)', '第二產(chǎn)業(yè)增加值_當季值(億元)',
             '第三產(chǎn)業(yè)增加值_當季值(億元)', '農(nóng)林牧漁業(yè)增加值_當季值(億元)', '工業(yè)增加值_當季值(億元)',
             '建筑業(yè)增加值_當季值(億元)', '批發(fā)和零售業(yè)增加值_當季值(億元)', '交通運輸、倉儲和郵政業(yè)增加值_當季值(億元)',
             '住宿和餐飲業(yè)增加值_當季值(億元)', '金融業(yè)增加值_當季值(億元)', '房地產(chǎn)業(yè)增加值_當季值(億元)',
             '其他行業(yè)增加值_當季值(億元)'],
            dtype='object')
      
    • # 分析國內(nèi)生產(chǎn)總值和第一產(chǎn)業(yè)的值之間的相關(guān)性
      # 單個散點圖
      plt.scatter(data["國內(nèi)生產(chǎn)總值_當季值(億元)"],data["第一產(chǎn)業(yè)增加值_當季值(億元)"])
      plt.show()
      
      
      
      # 分析國內(nèi)生產(chǎn)總值和第一產(chǎn)業(yè)的值之間的相關(guān)性
      # 分析國內(nèi)生產(chǎn)總值和第二產(chǎn)業(yè)的值之間的相關(guān)性
      # 分析國內(nèi)生產(chǎn)總值和第三產(chǎn)業(yè)的值之間的相關(guān)性
      # 多個散點圖
      plt.scatter(data["國內(nèi)生產(chǎn)總值_當季值(億元)"],data["第一產(chǎn)業(yè)增加值_當季值(億元)"], label="第一產(chǎn)業(yè)")
      plt.scatter(data["國內(nèi)生產(chǎn)總值_當季值(億元)"],data["第二產(chǎn)業(yè)增加值_當季值(億元)"], label="第二產(chǎn)業(yè)")
      plt.scatter(data["國內(nèi)生產(chǎn)總值_當季值(億元)"],data["第三產(chǎn)業(yè)增加值_當季值(億元)"], label="第三產(chǎn)業(yè)")
      plt.legend() # legend可以將label調(diào)用
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

?

3.7 圖表的美化

  1. 基本設(shè)置:

    • 圖例設(shè)置 plt.legend(loc=“best”)
      畫布設(shè)置 plt.figure(figsize=(10,6))
      標題設(shè)置 plt.title(string,size=10,color=“red”)
      橫軸設(shè)置 plt.xlabel(string,fontsize=10)
      縱軸設(shè)置 plt.ylabel(string,fontsize=10)
      是否顯示網(wǎng)絡(luò) plt.grid(False)
    • loc=“best” 自動找到最佳位置
      loc=“upper left” 左上角位置
      loc=“upper right” 右上角位置
      loc=“l(fā)ower left” 左下角位置
      loc=“l(fā)ower right” 右下角位置
      loc=“center left” 左邊中間位置
      loc=“center right” 右邊中間位置
  2. 示例:

    • import matplotlib.pyplot as plt
      import numpy as np
      
      # 中文設(shè)置
      plt.rcParams["font.sans-serif"] = ["KaiTi"] # 設(shè)置默認字體
      plt.rcParams["axes.unicode_minus"] = False # 解決"-"號顯示為方塊的問題
      
      plt.figure(figsize=(10,6)) # 畫布大小
      x = np.linspace(0,20,100)
      plt.plot(x, 2*x, label="曲線1")
      plt.plot(x, 3*x, label="曲線2")
      plt.plot(x, 4*x, label="曲線2")
      plt.legend(loc="best")  ## 默認best
      plt.title("三條曲線",size=16,color="r")
      plt.xlabel("變量1", fontsize=16,color='c')
      plt.ylabel("變量2", fontsize=16, color='m')
      plt.grid(True) 
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

第四章 數(shù)據(jù)預(yù)測庫Sklearn

4.1 sklearn預(yù)測未來

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

4.2 回歸數(shù)據(jù)的預(yù)測

4.2.1 回歸數(shù)據(jù)的切分

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  1. 示例(數(shù)據(jù)實操):

    • # data文件夾下的房價數(shù)據(jù).xlsx
      import pandas as pd
      
      data = pd.read_excel("data/房價數(shù)據(jù).xlsx")
      data
      
      ##	
         	城鎮(zhèn)人均犯罪率	城鎮(zhèn)非零售商用土地比例	一氧化氮濃度	住宅平均房間數(shù)	到城市中心區(qū)域的加權(quán)距離	房價(萬元)
      0	0.00632	2.31	0.538	6.575	4.0900	2.40
      1	0.02731	7.07	0.469	6.421	4.9671	2.16
      2	0.02729	7.07	0.469	7.185	4.9671	3.47
      3	0.03237	2.18	0.458	6.998	6.0622	3.34
      4	0.06905	2.18	0.458	7.147	6.0622	3.62
      ...	...	...	...	...	...	...
      501	0.06263	11.93	0.573	6.593	2.4786	2.24
      502	0.04527	11.93	0.573	6.120	2.2875	2.06
      503	0.06076	11.93	0.573	6.976	2.1675	2.39
      504	0.10959	11.93	0.573	6.794	2.3889	2.20
      505	0.04741	11.93	0.573	6.030	2.5050	1.19
      506 rows × 6 columns
      
    • ## 用前五個屬性來預(yù)測房價
      from sklearn.model_selection import train_test_split  # 訓(xùn)練跟測試切分的方法
      train, valid = train_test_split(data, test_size=0.2, shuffle=True, random_state=2020)  # 0.8作為訓(xùn)練集,0.2作為驗證集,0.2表示把驗證集 大小切成0.2
      # shuffle=True切分的時候把數(shù)據(jù)打亂, 便于切出來的數(shù)據(jù)比較均勻  # random_state 隨機種子,返回的是一個列表
      # 返回的數(shù)據(jù)分別是訓(xùn)練集和驗證集,分別把他們賦值給train和valid兩個變量
      
      data.shape  ## 未切分前的形狀
      
      ## (506, 6)
      
      
      train.shape  ## 切分后的形狀
      
      ## (404, 6)
      
      
      valid.shape ## 切分后的形狀
      
      ## (102, 6)
      
      
      404 / 506 # 訓(xùn)練集
      ## 0.7984189723320159
      
      102 / 506 # 驗證集
      ## 0.2015810276679842
      
      
    • train
      
      ##
      城鎮(zhèn)人均犯罪率	城鎮(zhèn)非零售商用土地比例	一氧化氮濃度	住宅平均房間數(shù)	到城市中心區(qū)域的加權(quán)距離	房價(萬元)
      215	0.19802	10.59	0.489	6.182	3.9454	2.50
      191	0.06911	3.44	0.437	6.739	6.4798	3.05
      107	0.13117	8.56	0.520	6.127	2.1224	2.04
      442	5.66637	18.10	0.740	6.219	2.0048	1.84
      230	0.53700	6.20	0.504	5.981	3.6715	2.43
      ...	...	...	...	...	...	...
      195	0.01381	0.46	0.422	7.875	5.6484	5.00
      118	0.13058	10.01	0.547	5.872	2.4775	2.04
      323	0.28392	7.38	0.493	5.708	4.7211	1.85
      392	11.57790	18.10	0.700	5.036	1.7700	0.97
      352	0.07244	1.69	0.411	5.884	10.7103	1.86
      404 rows × 6 columns
      
      
      valid
      
      ##
      城鎮(zhèn)人均犯罪率	城鎮(zhèn)非零售商用土地比例	一氧化氮濃度	住宅平均房間數(shù)	到城市中心區(qū)域的加權(quán)距離	房價(萬元)
      409	14.43830	18.10	0.5970	6.852	1.4655	2.75
      247	0.19657	5.86	0.4310	6.226	8.0555	2.05
      399	9.91655	18.10	0.6930	5.852	1.5004	0.63
      300	0.04417	2.24	0.4000	6.871	7.8278	2.48
      321	0.18159	7.38	0.4930	6.376	4.5404	2.31
      ...	...	...	...	...	...	...
      204	0.02009	2.68	0.4161	8.034	5.1180	5.00
      495	0.17899	9.69	0.5850	5.670	2.7986	2.31
      244	0.20608	5.86	0.4310	5.593	7.9549	1.76
      413	28.65580	18.10	0.5970	5.155	1.5894	1.63
      216	0.04560	13.89	0.5500	5.888	3.1121	2.33
      102 rows × 6 columns
      
4.2.2 線性回歸數(shù)據(jù)模型

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  1. 示例(實操接4.2.1數(shù)據(jù)):

    • # 預(yù)測房價
      # 導(dǎo)入線性回歸模型
      from sklearn.linear_model import LinearRegression
      
      model = LinearRegression() # 定義一個數(shù)據(jù)模型
      
      # train , valid # 訓(xùn)練集訓(xùn)練數(shù)據(jù),驗證集用來預(yù)測數(shù)據(jù)
      model.fit(train[['城鎮(zhèn)人均犯罪率', '城鎮(zhèn)非零售商用土地比例','一氧化氮濃度','住宅平均房間數(shù)','到城市中心區(qū)域的加權(quán)距離']], train['房價(萬元)'])  
      # fit傳入需要訓(xùn)練的特征,這里需要傳入除預(yù)測外的所有屬性 ,fit表示訓(xùn)練的意思
      
      ## LinearRegression()
      
      
      pred = model.predict(valid[['城鎮(zhèn)人均犯罪率', '城鎮(zhèn)非零售商用土地比例','一氧化氮濃度','住宅平均房間數(shù)','到城市中心區(qū)域的加權(quán)距離']])
      pred
      # predict表示預(yù)測,對驗證集的每條數(shù)據(jù)進行預(yù)測
      
      ##
      array([ 2.46474589,  2.2128151 ,  1.65888896,  2.85265327,  2.52879848,
              2.2258853 ,  2.57939134,  2.59693129,  1.82202357,  2.40885497,
              2.77841379,  1.64989292,  2.25647387,  3.30694586,  1.94037339,
              2.49620692,  2.03597603,  2.61239847,  1.95491352,  1.73957756,
              2.1573622 ,  2.82385479,  2.09397049,  2.33096025,  0.80547778,
              0.83617233,  2.43902438,  1.73929665,  3.29007521,  0.96528621,
              2.80937346,  2.75473389, -0.04440117,  2.4362086 ,  2.053418  ,
              0.87830185,  2.00948295,  2.45519188,  1.94278751,  2.27900789,
              2.80594378,  2.2755299 ,  2.15347854,  3.23576954,  1.9600308 ,
              2.26173723,  2.29422412,  2.06932986,  2.27333606,  2.95266904,
              2.84799092,  2.59296111,  2.65458646,  1.42464319,  1.82023523,
              1.16141491,  2.70465962,  2.31695954,  1.55678262,  2.46458798,
              2.43287322,  2.23338117,  2.67973044,  4.33672353,  2.95615622,
              2.53254281,  2.70089423,  0.27060842,  1.71579957,  2.12512989,
              2.5633941 ,  2.31433153,  2.84858425,  2.03601581,  2.46245774,
              2.02078349,  2.25731773,  2.38199773,  2.14867614,  2.46881375,
              2.32091925,  2.16436359,  3.11908093,  3.43351395,  3.3118866 ,
              1.69532309,  1.87010176,  2.3609788 ,  3.13228058,  2.08536776,
              1.75231547,  1.93768106,  2.04788031,  2.28601279,  2.1095751 ,
              2.38820313,  1.59125697,  3.93697614,  1.97308141,  1.75644051,
              0.92296715,  2.05833759])
      
      
      len(pred)
      
      ## 102
      
      
      train.columns
      
      ## 
      Index(['城鎮(zhèn)人均犯罪率', '城鎮(zhèn)非零售商用土地比例', '一氧化氮濃度', '住宅平均房間數(shù)', '到城市中心區(qū)域的加權(quán)距離',
             '房價(萬元)'],
            dtype='object')
      
      
      valid.columns
      
      ##
      Index(['城鎮(zhèn)人均犯罪率', '城鎮(zhèn)非零售商用土地比例', '一氧化氮濃度', '住宅平均房間數(shù)', '到城市中心區(qū)域的加權(quán)距離',
             '房價(萬元)'],
            dtype='object')
      
4.2.3 回歸模型評估方法-MSE

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  1. MSE

    • 即均方誤差
    • MSE的值越大,表明預(yù)測效果越差
  2. 示例(實操接4.2.2數(shù)據(jù)):

    • # vaild驗證集是真實準確的數(shù)據(jù),pred是經(jīng)過驗證集預(yù)測的數(shù)據(jù)
      import numpy as np
      # 導(dǎo)入均方誤差
      from sklearn.metrics import mean_squared_error  # mean_squared_error表示均方誤差
      
      mse_error = mean_squared_error(valid['房價(萬元)'],pred)  # 先傳入實際值,再傳入預(yù)測值
      mse_error
      
      ## 0.38184398467340286
      
      
      np.sqrt(mse_error)  # 對均方誤差進行開方,就可以知道每個樣本的偏離程度 # np.sqrt 是numpy的開方
      
      ## 0.6179352592896791
      
      

4.3 二分類數(shù)據(jù)的預(yù)測

4.3.1 二分類數(shù)據(jù)的切分
  1. 示例(數(shù)據(jù)實操):

    • # data文件夾下的泰坦尼克號數(shù)據(jù).xlsx  # 0是死亡,1是存活
      import pandas as pd
      data = pd.read_excel("data/泰坦尼克號數(shù)據(jù).xlsx")
      data
      
      ## 
      	乘客編號	船票種類	性別	年齡	乘客兄弟姐妹/配偶的個數(shù)	乘客父母/孩子的個數(shù)	是否存活
      0	1	3	male	22.0	1	0	0
      1	2	1	female	38.0	1	0	1
      2	3	3	female	26.0	0	0	1
      3	4	1	female	35.0	1	0	1
      4	5	3	male	35.0	0	0	0
      ...	...	...	...	...	...	...	...
      886	887	2	male	27.0	0	0	0
      887	888	1	female	19.0	0	0	1
      888	889	3	female	-10.0	1	2	0
      889	890	1	male	26.0	0	0	1
      890	891	3	male	32.0	0	0	0
      891 rows × 7 columns
      
      data.dtypes  
      # 性別 是字符串類型,不符合字符類型,需要轉(zhuǎn)為數(shù)值類型。因為數(shù)學(xué)模型里面必須要都是數(shù)值類型才可以分析
      
      ##
      乘客編號              int64
      船票種類              int64
      性別               object
      年齡              float64
      乘客兄弟姐妹/配偶的個數(shù)      int64
      乘客父母/孩子的個數(shù)        int64
      是否存活              int64
      dtype: object
          
      data['性別'].value_counts()
      
      ##
      male      577
      female    314
      Name: 性別, dtype: int64
              
      data['性別'] = data['性別'].apply(lambda x: 0 if x == 'male'else 1)  
      # 用pandas學(xué)的apply快速實現(xiàn)轉(zhuǎn)換,然后更新列的數(shù)據(jù)
      data.head()  # 顯示修改后的前五條數(shù)據(jù)
      
      ##
      	乘客編號	船票種類	性別	年齡	乘客兄弟姐妹/配偶的個數(shù)	乘客父母/孩子的個數(shù)	是否存活
      0	1	3	1	22.0	1	0	0
      1	2	1	1	38.0	1	0	1
      2	3	3	1	26.0	0	0	1
      3	4	1	1	35.0	1	0	1
      4	5	3	1	35.0	0	0	0
      
    • from sklearn.model_selection import train_test_split
      
      train,valid = train_test_split(data, test_size=0.2, shuffle=True, random_state=2020)
      
      data["是否存活"].value_counts() # 0是死亡,1是存活
      
      ##
      0    549
      1    342
      Name: 是否存活, dtype: int64
              
      549 / 342 # 死亡:活著
      ##1.605263157894737
      
      train['是否存活'].value_counts()
      
      ##
      0    444
      1    268
      Name: 是否存活, dtype: int64
              
      444/268
      ## 1.6567164179104477
      
      valid['是否存活'].value_counts()
      
      ##
      0    105
      1     74
      Name: 是否存活, dtype: int64
              
      105/74
      ## 1.4189189189189189
      
      # 由以上數(shù)據(jù)可知 訓(xùn)練集是1.6多,而驗證集是1.4,跟預(yù)測數(shù)據(jù)1.60的比值差別太大差別太大
      
      ### 解決辦法 # 用stratify,進行分層抽樣
      train,valid = train_test_split(data, test_size=0.2, shuffle=True, random_state=2020, stratify=data["是否存活"])
      
      train['是否存活'].value_counts()
      
      ##
      0    439
      1    273
      Name: 是否存活, dtype: int64
      
      439/273
      ## 1.6080586080586081
      
      valid['是否存活'].value_counts()
      
      ##
      0    110
      1     69
      Name: 是否存活, dtype: int64
          
      110/69
      ## 1.5942028985507246
      
      
4.3.2 邏輯回歸數(shù)據(jù)模型

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  1. logistic函數(shù):

    • # logistic函數(shù)曲線
      import numpy as np
      import matplotlib.pyplot as plt
      x = np.linspace(-10, 10, 100)
      y = 1 / (1 + np.exp(-x))  # np.exp就是數(shù)學(xué)中的自然e,np.exp(-x)表示e的-x次方
      plt.plot(x, y)
      plt.show()
      

      數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  2. 示例(實操接4.3.1數(shù)據(jù)):

    • from sklearn.linear_model import LogisticRegression # LogisticRegression 邏輯回歸
      
      model = LogisticRegression() # 定義一個數(shù)據(jù)模型
      
      train.columns # 因為每個乘客的乘客編號都不同,就不熟他們特有的特征了,索引只取其他五個特有的屬性
      
      ##
      Index(['乘客編號', '船票種類', '性別', '年齡', '乘客兄弟姐妹/配偶的個數(shù)', '乘客父母/孩子的個數(shù)', '是否存活'], dtype='object')
      
      
      # 訓(xùn)練
      model.fit(train[['船票種類', '性別', '年齡', '乘客兄弟姐妹/配偶的個數(shù)', '乘客父母/孩子的個數(shù)']], train['是否存活'])
      
      ## LogisticRegression() # 成功
      
      
      # 預(yù)測
      pred = model.predict(valid[['船票種類', '性別', '年齡', '乘客兄弟姐妹/配偶的個數(shù)', '乘客父母/孩子的個數(shù)']])
      pred # 預(yù)測結(jié)果
      
      ##
      array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0,
             0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
             1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1,
             0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,
             1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
             0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0,
             1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1,
             0, 0, 0], dtype=int64)
      
      
      
4.3.3 二分類模型評估指標-準確率

數(shù)據(jù)分析:人工智能篇,python,數(shù)據(jù)分析,matplotlib,數(shù)據(jù)分析,python,matplotlib,sklearn

  1. 準確率:

    • 準確率,Accuracy,表示模型預(yù)測正確地比例

    • A c c u r a c y = M / N Accuracy = M/N Accuracy=M/N

    • M表示預(yù)測對的數(shù)量,N表示需要預(yù)測的數(shù)據(jù)

    • Accuracy的值越大,表明預(yù)測的效果越好

  2. 示例(實操接4.3.2數(shù)據(jù)):文章來源地址http://www.zghlxwxcb.cn/news/detail-737613.html

    • from sklearn.metrics import accuracy_score # accuracy_score 準確率分數(shù),用來計算準確率的
      accuracy_score(valid["是否存活"], pred)  # 先傳入真實數(shù)據(jù)再傳入預(yù)測數(shù)據(jù)
      
      ## 0.776536312849162
      
      

到了這里,關(guān)于數(shù)據(jù)分析:人工智能篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包