Python分析建模,日常問(wèn)題整理(七)
2018.09.03~2018.09.09
- 1 拒絕推斷
一般情況下,風(fēng)控模型中的只有通過(guò)放款的客群才有好壞標(biāo)記(是否逾期),而對(duì)于未放款的部分客群是沒(méi)有進(jìn)入模型的。
隨著時(shí)間的推移,模型的優(yōu)化迭代,進(jìn)入模型的是越來(lái)越好的客群。
而風(fēng)控模型真實(shí)面對(duì)的客群卻包括了“評(píng)分較差”的用戶,模型在“評(píng)分較差”用戶中無(wú)法得到驗(yàn)證,導(dǎo)致訓(xùn)練的模型越來(lái)越偏離實(shí)際情況,甚至通過(guò)了大量應(yīng)該被拒絕的壞用戶,致使大量壞賬出現(xiàn),直接帶來(lái)巨大經(jīng)濟(jì)損失。因此,在只有最優(yōu)質(zhì)的放款用戶好壞標(biāo)簽的情況下,如何保證建模對(duì)所有放款用戶和拒絕用戶都有良好的排序能力,需要用到拒絕推斷的思想。
- 2 jupyter隱藏代碼
導(dǎo)出文件時(shí)輸出但可以隱藏代碼,點(diǎn)擊按鈕隱藏
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="點(diǎn)擊隱藏或者顯示代碼"></form>''')
導(dǎo)出文件時(shí)可不輸出也不隱藏代碼,好好用呀
import IPython.core.display as di;
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) \
{ jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)
- 3 datafame表格格式
結(jié)合pyecharts和markdown,輸出好看的分析報(bào)告。
這種應(yīng)用類似于excel中的條件格式-數(shù)據(jù)條-色階等功能。
def color_negative_red(val):
"""
對(duì)滿足條件的數(shù)據(jù)標(biāo)記為紅色
"""
color = 'red' if val < 0.3 else 'black'
return 'color: %s' % color
dat_all.head().ix[:,2:].style.applymap(color_negative_red)
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
選取一行/列最大值填充為黃色
'''
return ['background-color: yellow' if v == s.max() else '' for v in s]
df.style.apply(highlight_max,axis=1)
cm = sns.light_palette("green", as_cmap=True)
df.style.bar(subset=['a','b','c','d'], color='#5fba7d')
對(duì)某些列按大小設(shè)定綠色數(shù)據(jù)條
df.style.bar(subset=['a','b','c','d'], color='#5fba7d').applymap(color_negative_red)
對(duì)某些列先按大小設(shè)定綠色數(shù)據(jù)條,再按條件設(shè)定數(shù)字顏色
'''
先對(duì)滿足條件的數(shù)據(jù)標(biāo)記為紅色,再對(duì)每行最大的數(shù)值填充為黃色
'''
df.style.applymap(color_negative_red).apply(highlight_max,axis=1)
def highlight_max(data, color='yellow'):
'''
highlight the maximum in a Series or DataFrame
對(duì)表中最大值填充為橘黃色
'''
attr = 'background-color: {}'.format(color)
if data.ndim == 1: # Series from .apply(axis=0) or axis=1
is_max = data == data.max()
return [attr if v else '' for v in is_max]
else: # from .apply(axis=None)
is_max = data == data.max().max()
return pd.DataFrame(np.where(is_max, attr, ''),
index=data.index, columns=data.columns)
df.style.apply(highlight_max, color='darkorange', axis=None)
來(lái)自于↓
df.style官方文檔
- 4 將dataframe某一列作為索引
df.set_index(["Column"], inplace=True)