日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据分箱技术Binning

發布時間:2024/3/24 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据分箱技术Binning 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據分箱技術Binning

  • 數據分箱技術Binning
    • 引入相關庫
    • 數據獲取
    • 數據分箱

數據分箱技術Binning

引入相關庫

import numpy as np import pandas as pd from pandas import Series,DataFrame

數據獲取

產生一些考試的成績分數,一共20個數據在25到100之間

score_list=np.random.randint(25,100,size=20) score_list array([66, 40, 32, 55, 81, 91, 49, 57, 36, 96, 83, 55, 98, 38, 36, 82, 71,39, 73, 60])

數據分箱

把0-59作為不及格,60-70作為ok,70-80作為良好,80-100作為優秀,定義一個list作為數據的取值范圍

bins=[0,59,70,80,100]

通過cut方法,第一個參數為對哪個數據做分箱,第二個參數作為分箱的范圍

pd.cut(score_list,bins) [(59, 70], (0, 59], (0, 59], (0, 59], (80, 100], ..., (80, 100], (70, 80], (0, 59], (70, 80], (59, 70]] Length: 20 Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 80] < (80, 100]] score_cat=pd.cut(score_list,bins)

統計每個分數段人數的結果

pd.value_counts(score_cat) (0, 59] 10 (80, 100] 6 (70, 80] 2 (59, 70] 2 dtype: int64

創建一個空的DataFrame

df=DataFrame()

把‘score’這一列用score_list賦值

df['score']=score_list df score012345678910111213141516171819
66
40
32
55
81
91
49
57
36
96
83
55
98
38
36
82
71
39
73
60

通過rands生成隨機的20個字符串,作為學生的姓名

df['student']=[pd.util.testing.rands(3) for i in range(20)] df scorestudent012345678910111213141516171819
66mDG
40pCe
32Iuv
55sWt
81eaR
915Gw
498Xc
57Tu2
36IRS
96VQU
83lsc
55nek
98cFQ
38ZeB
36Lfi
82jYv
71x6q
39t9I
73CJg
60hF4

把DataFrame的score作cut

pd.cut(df['score'],bins) 0 (59, 70] 1 (0, 59] 2 (0, 59] 3 (0, 59] 4 (80, 100] 5 (80, 100] 6 (0, 59] 7 (0, 59] 8 (0, 59] 9 (80, 100] 10 (80, 100] 11 (0, 59] 12 (80, 100] 13 (0, 59] 14 (0, 59] 15 (80, 100] 16 (70, 80] 17 (0, 59] 18 (70, 80] 19 (59, 70] Name: score, dtype: category Categories (4, interval[int64]): [(0, 59] < (59, 70] < (70, 80] < (80, 100]]

把做完cut的Categorie賦給DataFrame的‘Categories’

df['Categories ']=pd.cut(df['score'],bins) df scorestudentCategories012345678910111213141516171819
66mDG(59, 70]
40pCe(0, 59]
32Iuv(0, 59]
55sWt(0, 59]
81eaR(80, 100]
915Gw(80, 100]
498Xc(0, 59]
57Tu2(0, 59]
36IRS(0, 59]
96VQU(80, 100]
83lsc(80, 100]
55nek(0, 59]
98cFQ(80, 100]
38ZeB(0, 59]
36Lfi(0, 59]
82jYv(80, 100]
71x6q(70, 80]
39t9I(0, 59]
73CJg(70, 80]
60hF4(59, 70]

定義一個list,作為labels方法的參數,通過標簽的方法,把Categories標志出來,更適合人的閱讀

df['Categories ']=pd.cut(df['score'],bins,labels=['Low','OK','Good','Great']) df scorestudentCategories012345678910111213141516171819
66mDGOK
40pCeLow
32IuvLow
55sWtLow
81eaRGreat
915GwGreat
498XcLow
57Tu2Low
36IRSLow
96VQUGreat
83lscGreat
55nekLow
98cFQGreat
38ZeBLow
36LfiLow
82jYvGreat
71x6qGood
39t9ILow
73CJgGood
60hF4OK

總結

以上是生活随笔為你收集整理的数据分箱技术Binning的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。