亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python 基于卡方值分箱算法的實(shí)現(xiàn)示例

 更新時(shí)間:2020年07月17日 09:41:04   作者:wyzwyzwyzo  
這篇文章主要介紹了python 基于卡方值分箱算法的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

原理很簡(jiǎn)單,初始分20箱或更多,先確保每箱中都含有0,1標(biāo)簽,對(duì)不包含0,1標(biāo)簽的箱向前合并,計(jì)算各箱卡方值,對(duì)卡方值最小的箱向后合并,代碼如下

import pandas as pd
import numpy as np
import scipy
from scipy import stats
def chi_bin(DF,var,target,binnum=5,maxcut=20):
  '''
  DF:data
  var:variable
  target:target / label
  binnum: the number of bins output
  maxcut: initial bins number 
  '''
  
  data=DF[[var,target]]
  #equifrequent cut the var into maxcut bins
  data["cut"],breaks=pd.qcut(data[var],q=maxcut,duplicates="drop",retbins=True)
  #count 1,0 in each bin
  count_1=data.loc[data[target]==1].groupby("cut")[target].count()
  count_0=data.loc[data[target]==0].groupby("cut")[target].count()
  #get bins value: min,max,count 0,count 1
  bins_value=[*zip(breaks[:maxcut-1],breaks[1:],count_0,count_1)]
  #define woe
  def woe_value(bins_value):
    df_woe=pd.DataFrame(bins_value)
    df_woe.columns=["min","max","count_0","count_1"]
    df_woe["total"]=df_woe.count_1+df_woe.count_0
    df_woe["bad_rate"]=df_woe.count_1/df_woe.total
    df_woe["woe"]=np.log((df_woe.count_0/df_woe.count_0.sum())/(df_woe.count_1/df_woe.count_1.sum()))
    return df_woe
  #define iv
  def iv_value(df_woe):
    rate=(df_woe.count_0/df_woe.count_0.sum())-(df_woe.count_1/df_woe.count_1.sum())
    iv=np.sum(rate * df_woe.woe)
    return iv
  #make sure every bin contain 1 and 0
  ##first bin merge backwards
  for i in range(len(bins_value)):
    if 0 in bins_value[0][2:]:
      bins_value[0:2]=[(
        bins_value[0][0],
        bins_value[1][1],
        bins_value[0][2]+bins_value[1][2],
        bins_value[0][3]+bins_value[1][3])]
      continue
  ##bins merge forwards
    if 0 in bins_value[i][2:]:
      bins_value[i-1:i+1]=[(
        bins_value[i-1][0],
        bins_value[i][1],
        bins_value[i-1][2]+bins_value[i][2],
        bins_value[i-1][3]+bins_value[i][3])]
      break
    else:
      break
  
  #calculate chi-square merge the minimum chisquare    
  while len(bins_value)>binnum:
    chi_squares=[]
    for i in range(len(bins_value)-1):
      a=bins_value[i][2:]
      b=bins_value[i+1][2:]
      chi_square=scipy.stats.chi2_contingency([a,b])[0]
      chi_squares.append(chi_square)
  #merge the minimum chisquare backwards
    i = chi_squares.index(min(chi_squares))
               
    bins_value[i:i+2]=[(
      bins_value[i][0],
      bins_value[i+1][1],
      bins_value[i][2]+bins_value[i+1][2],
      bins_value[i][3]+bins_value[i+1][3])]
    
    df_woe=woe_value(bins_value)
    
  #print bin number and iv
    print("箱數(shù):{},iv:{:.6f}".format(len(bins_value),iv_value(df_woe)))
  #return bins and woe information 
  return woe_value(bins_value)             

以下是效果:

初始分成10箱,目標(biāo)為3箱

chi_bin(data,"age","SeriousDlqin2yrs",binnum=3,maxcut=10)

箱數(shù):8,iv:0.184862
箱數(shù):7,iv:0.184128
箱數(shù):6,iv:0.179518
箱數(shù):5,iv:0.176980
箱數(shù):4,iv:0.172406
箱數(shù):3,iv:0.160015
min max count_0 count_1 total bad_rate woe
0 0.0 52.0 70293 7077 77370 0.091470 -0.266233
1 52.0 61.0 29318 1774 31092 0.057056 0.242909
2 61.0 72.0 26332 865 27197 0.031805 0.853755

到此這篇關(guān)于python 基于卡方值分箱算法的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)python 卡方值分箱算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用Python實(shí)現(xiàn)微信找房機(jī)器人實(shí)例教程

    利用Python實(shí)現(xiàn)微信找房機(jī)器人實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于如何利用Python實(shí)現(xiàn)微信找房機(jī)器人的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 定位python內(nèi)存泄漏問題及解決

    定位python內(nèi)存泄漏問題及解決

    這篇文章主要介紹了定位python內(nèi)存泄漏問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • pycharm實(shí)現(xiàn)在子類中添加一個(gè)父類沒有的屬性

    pycharm實(shí)現(xiàn)在子類中添加一個(gè)父類沒有的屬性

    這篇文章主要介紹了pycharm實(shí)現(xiàn)在子類中添加一個(gè)父類沒有的屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python解析Dwarf2格式ELF文件示例

    python解析Dwarf2格式ELF文件示例

    這篇文章主要為大家介紹了python解析Dwarf2格式ELF文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Python腳本Selenium及頁面Web元素定位詳解

    Python腳本Selenium及頁面Web元素定位詳解

    這篇文章主要為大家介紹了Python腳本中如何使用Selenium定位頁面Web元素的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Python學(xué)習(xí)之私有函數(shù),私有變量及封裝詳解

    Python學(xué)習(xí)之私有函數(shù),私有變量及封裝詳解

    私有函數(shù)與私有變量中的私有就是獨(dú)自擁有、不公開、不分享的意思。放到函數(shù)與變量中就是獨(dú)自擁有的函數(shù)與獨(dú)自擁有的變量,并且不公開。本文將通過示例詳細(xì)講解Python中的私有函數(shù)、私有變量及封裝,感興趣的可以學(xué)習(xí)一下
    2022-03-03
  • Flask??response?對(duì)象詳情

    Flask??response?對(duì)象詳情

    在?Flask?中,響應(yīng)使用?Response?對(duì)象表示,響應(yīng)報(bào)文中的大部分內(nèi)容由服務(wù)器處理,一般情況下,我們只負(fù)責(zé)返回主體內(nèi)容即可。在之前的文章中,我們了解到?Flask?會(huì)先匹配請(qǐng)求?url?的路由,調(diào)用對(duì)應(yīng)的視圖函數(shù),視圖函數(shù)的返回值構(gòu)成了響應(yīng)報(bào)文的主體內(nèi)容。
    2021-11-11
  • Python如何使用input函數(shù)獲取輸入

    Python如何使用input函數(shù)獲取輸入

    這篇文章主要介紹了Python如何使用input函數(shù)獲取輸入,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python RSA加密的示例

    python RSA加密的示例

    這篇文章主要介紹了python RSA加密的示例,幫助大家更好的理解和使用加密算法,感興趣的朋友可以了解下
    2020-12-12
  • Python中JSON的使用方法(超詳細(xì))

    Python中JSON的使用方法(超詳細(xì))

    JSON是一種輕量級(jí)的數(shù)據(jù)交換格式,它是JavaScript的子集,易于人閱讀和編寫,這篇文章主要介紹了Python中JSON的基本使用,需要的朋友可以參考下
    2022-11-11

最新評(píng)論