創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比西湖網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式西湖網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋西湖地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。這篇文章主要講解了python實(shí)現(xiàn)卡方值分箱算法的代碼詳解,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
原理很簡(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)
網(wǎng)站欄目:python實(shí)現(xiàn)卡方值分箱算法的代碼詳解-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://muchs.cn/article18/pspgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站策劃、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容