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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

从0到1建立一张评分卡之模型建立

發(fā)布時(shí)間:2025/3/21 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从0到1建立一张评分卡之模型建立 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? 評(píng)分卡建模常用邏輯回歸模型,將邏輯回歸輸出的概率值映射成分?jǐn)?shù),最后得到標(biāo)準(zhǔn)評(píng)分卡。關(guān)于評(píng)分卡映射的邏輯,可以看之前的文章邏輯回歸評(píng)分卡映射邏輯。
? 下面接著分箱之后的數(shù)據(jù)開(kāi)始建模。首先將每個(gè)變量分箱的結(jié)果進(jìn)行轉(zhuǎn)換,得到每個(gè)變量對(duì)應(yīng)的woe結(jié)果表。

# 變量woe結(jié)果表 def woe_df_concat(bin_df):"""bin_df:list形式,里面存儲(chǔ)每個(gè)變量的分箱結(jié)果return :woe結(jié)果表"""woe_df_list =[]for df in bin_df:woe_df = df.reset_index().assign(col=df.index.name).rename(columns={df.index.name:'bin'})woe_df_list.append(woe_df)woe_result = pd.concat(woe_df_list,axis=0)# 為了便于查看,將字段名列移到第一列的位置上woe_result1 = woe_result['col']woe_result2 = woe_result.iloc[:,:-1]woe_result_df = pd.concat([woe_result1,woe_result2],axis=1)woe_result_df = woe_result_df.reset_index(drop=True)return woe_result_df df_woe_cat=woe_df_concat(bin_df_cat) df_woe_num=woe_df_concat(bin_df_num) df_woe=pd.concat([df_woe_cat,df_woe_num],axis=0)

? 這一步的目的是將變量分箱之后的結(jié)果進(jìn)行整理匯總,看一下變量分箱之后的大致情況。

? 整理之后可以看到每個(gè)變量的分箱情況、每一箱的好壞占比、WOE、IV值。注意,最好每一箱的WOE值不要超過(guò)1。附上檢驗(yàn)WOE值是否大于1的代碼。

# 檢查某個(gè)區(qū)間的woe是否大于1 def woe_large(bin_df):"""bin_df:list形式,里面存儲(chǔ)每個(gè)變量的分箱結(jié)果return:woe_large_col: 某個(gè)區(qū)間woe大于1的變量,list集合woe_judge_df :df形式,每個(gè)變量的檢驗(yàn)結(jié)果"""woe_large_col=[]col_list =[]woe_judge =[]for woe_df in bin_df:col_name = woe_df.index.namewoe_list = list(woe_df.woe)woe_large = list(filter(lambda x:x>=1,woe_list))if len(woe_large)>0:col_list.append(col_name)woe_judge.append('True')woe_large_col.append(col_name)else:col_list.append(col_name)woe_judge.append('False')woe_judge_df = pd.DataFrame({'col':col_list,'judge_large':woe_judge})return woe_large_col,woe_judge_df

? 接著就是將變量的值映射為變量的WOE值準(zhǔn)備入模。

# woe轉(zhuǎn)換 def woe_transform(df,target,df_woe):"""df:數(shù)據(jù)集target:目標(biāo)變量的字段名df_woe:woe結(jié)果表return:woe轉(zhuǎn)化之后的數(shù)據(jù)集"""df2 = df.copy()for col in df2.drop([target],axis=1).columns:x = df2[col]bin_map = df_woe[df_woe.col==col]bin_res = np.array([0]*x.shape[0],dtype=float)for i in bin_map.index:lower = bin_map['min_bin'][i]upper = bin_map['max_bin'][i]if lower == upper:x1 = x[np.where(x == lower)[0]]else:x1 = x[np.where((x>=lower)&(x<=upper))[0]]mask = np.in1d(x,x1)bin_res[mask] = bin_map['woe'][i]bin_res = pd.Series(bin_res,index=x.index)bin_res.name = x.namedf2[col] = bin_resreturn df2

? 以上是轉(zhuǎn)化后的數(shù)據(jù),所有變量的值都轉(zhuǎn)化成了對(duì)應(yīng)的WOE值,后面就是進(jìn)行建模。

feature_list=num_features+cat_features x = df_train[feature_list] y = df_train['y']lr_model = LogisticRegression(C=0.1) lr_model.fit(x,y) df_train['prob'] = lr_model.predict_proba(x)[:,1]

? 建模的代碼很簡(jiǎn)單,以上幾行就完成邏輯回歸建模的過(guò)程了。然后進(jìn)行評(píng)分映射。以下代碼需要理解評(píng)分卡分?jǐn)?shù)映射的邏輯。

# 評(píng)分卡刻度 def cal_scale(score,odds,PDO,model):"""odds:設(shè)定的壞好比score:在這個(gè)odds下的分?jǐn)?shù)PDO: 好壞翻倍比model:邏輯回歸模型return :A,B,base_score"""B = PDO/np.log(2)A = score+B*np.log(odds)# base_score = A+B*model.intercept_[0]print('B: {:.2f}'.format(B))print('A: {:.2f}'.format(A))# print('基礎(chǔ)分為:{:.2f}'.format(base_score))return A,B cal_scale(50,0.05,10,lr_model)

? 假定在5%的好壞比之下的分值為50分,PDO為10分,計(jì)算A和B兩個(gè)參數(shù)。A為14.43,B為6.78。

def Prob2Score(prob, A,B):#將概率轉(zhuǎn)化成分?jǐn)?shù)且為正整數(shù)y = np.log(prob/(1-prob))return float(A-B*y) df_train['score'] = df_train['prob'].map(lambda x:Prob2Score(x,6.78,14.43))

? 分?jǐn)?shù)的計(jì)算公式即A-B*log(odds)。到此評(píng)分卡就建立完成了,后續(xù)還需要對(duì)模型進(jìn)行評(píng)估、對(duì)評(píng)分卡的分?jǐn)?shù)進(jìn)行監(jiān)控,在之前的文章中已經(jīng)寫(xiě)過(guò)。

  • 風(fēng)控模型評(píng)估
  • 評(píng)分卡實(shí)現(xiàn)和評(píng)估
  • ? 評(píng)分卡系列是我自己從網(wǎng)上找的代碼和數(shù)據(jù)集,一點(diǎn)點(diǎn)實(shí)現(xiàn)了一遍,記錄一下自己學(xué)習(xí)的過(guò)程。后續(xù)會(huì)找一份機(jī)器學(xué)習(xí)模型的代碼練習(xí)。作為初學(xué)者,本文理解尚有不到位之處,歡迎大家多多指正。量化成長(zhǎng)軌跡,共同交流與成長(zhǎng)。

    【作者】:Labryant
    【原創(chuàng)公眾號(hào)】:風(fēng)控獵人
    【簡(jiǎn)介】:某創(chuàng)業(yè)公司策略分析師,積極上進(jìn),努力提升。乾坤未定,你我都是黑馬。
    【轉(zhuǎn)載說(shuō)明】:轉(zhuǎn)載請(qǐng)說(shuō)明出處,謝謝合作!~

    總結(jié)

    以上是生活随笔為你收集整理的从0到1建立一张评分卡之模型建立的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。