生活随笔
收集整理的這篇文章主要介紹了
数据挖掘实践(金融风控)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Task1 賽題理解
一、賽題
零基礎入門金融風控-貸款違約預測 該數據來自某信貸平臺的貸款記錄,總數據量超過120w,包含47列變量信息,其中15列為匿名變量 訓練數據 總共有8萬條。部分截圖如下: 測試數據 總共有2萬條 變量信息如下:
二、評測標準
提交結果為每個測試樣本是1的概率,也就是y為1的概率。評價方法為AUC評估模型效果(越大越好)。
三、結果提交
提交前請確保預測結果的格式與sample_submit.csv中的格式一致,以及提交文件后綴名為csv。
形式如下:
id,isDefault 800000,0.5 800001,0.5 800002,0.5 800003,0.5
Task2 數據分析
一、學習內容
數據總體了解:
data
. shape
data
. info
( )
data
. describe
( )
缺失值和唯一值: 缺失值 data_train.isnull() data_train.isnull().any().sum()# 統計有多少列有缺失值 查看缺失值的比例,并形成字典
have_null_fea_dict
= ( data_train
. isnull
( ) . sum ( ) / len ( data_train
) ) . to_dict
( )
missing
= data_train
. isnull
( ) . sum ( ) / len ( data_train
)
missing
= missing
[ missing
> 0 ]
missing
. sort_values
( inplace
= True )
missing
. plot
. bar
( )
縱向了解哪些列存在 “nan”, 并可以把nan的個數打印,主要的目的在于查看某一列nan存在的個數是否真的很大,如果nan存在的過多,說明這一列對label的影響幾乎不起作用了,可以考慮刪掉。如果缺失值很小一般可以選擇填充。 另外可以橫向比較,如果在數據集中,某些樣本數據的大部分列都是缺失的且樣本足夠的情況下可以考慮刪除。 Tips: 比賽大殺器lgb模型可以自動處理缺失值,Task4模型會具體學習模型了解模型哦!
唯一值–查看訓練集測試集中特征屬性只有一值的特征 one_value_fea = [col for col in data_train.columns if data_train[col].nunique() <= 1]
2.3.5 查看特征的數值類型有哪些,對象類型有哪些
深入數據-查看數據類型 特征一般都是由類別型特征 和數值型特征 組成,而數值型特征又分為連續型和離散型 。 類別型特征有時具有非數值關系,有時也具有數值關系 。比如‘grade’中的等級A,B,C等,是否只是單純的分類,還是A優于其他要結合業務判斷。 數值型特征本是可以直接入模的,但往往風控人員要對其做分箱,轉化為WOE編碼進而做標準評分卡 等操作。從模型效果上來看,特征分箱主要是為了降低變量的復雜性 ,減少變量噪音對模型的影響,提高自變量和因變量的相關度。從而使模型更加穩定 類別型數據
numerical_fea
= list ( data_train
. select_dtypes
( exclude
= [ 'object' ] ) . columns
)
category_fea
= list ( filter ( lambda x
: x
not in numerical_fea
, list ( data_train
. columns
) ) )
def get_numerical_serial_fea ( data
, feas
) : numerical_serial_fea
= [ ] numerical_noserial_fea
= [ ] for fea
in feas
: temp
= data
[ fea
] . nunique
( ) if temp
<= 10 : numerical_noserial_fea
. append
( fea
) continue numerical_serial_fea
. append
( fea
) return numerical_serial_fea
, numerical_noserial_fea
numerical_serial_fea
, numerical_noserial_fea
= get_numerical_serial_fea
( data_train
, numerical_fea
)
data_train
[ 'term' ] . value_counts
( )
data_train
[ 'policyCode' ] . value_counts
( )
data_train
[ 'n11' ] . value_counts
( )
f
= pd
. melt
( data_train
, value_vars
= numerical_serial_fea
)
g
= sns
. FacetGrid
( f
, col
= "variable" , col_wrap
= 2 , sharex
= False , sharey
= False )
g
= g
. map ( sns
. distplot
, "value" )
2.3.6 變量分布可視化 單一變量分布可視化
plt
. figure
( figsize
= ( 8 , 8 ) )
sns
. barplot
( data_train
[ "employmentLength" ] . value_counts
( dropna
= False ) [ : 20 ] , data_train
[ "employmentLength" ] . value_counts
( dropna
= False ) . keys
( ) [ : 20 ] )
plt
. show
( )
根據y值不同可視化x某個特征的分布 首先查看類別型變量在不同y值上的分布
train_loan_fr
= data_train
. loc
[ data_train
[ 'isDefault' ] == 1 ]
train_loan_nofr
= data_train
. loc
[ data_train
[ 'isDefault' ] == 0 ]
fig
, ( ( ax1
, ax2
) , ( ax3
, ax4
) ) = plt
. subplots
( 2 , 2 , figsize
= ( 15 , 8 ) )
train_loan_fr
. groupby
( 'grade' ) [ 'grade' ] . count
( ) . plot
( kind
= 'barh' , ax
= ax1
, title
= 'Count of grade fraud' )
train_loan_nofr
. groupby
( 'grade' ) [ 'grade' ] . count
( ) . plot
( kind
= 'barh' , ax
= ax2
, title
= 'Count of grade non-fraud' )
train_loan_fr
. groupby
( 'employmentLength' ) [ 'employmentLength' ] . count
( ) . plot
( kind
= 'barh' , ax
= ax3
, title
= 'Count of employmentLength fraud' )
train_loan_nofr
. groupby
( 'employmentLength' ) [ 'employmentLength' ] . count
( ) . plot
( kind
= 'barh' , ax
= ax4
, title
= 'Count of employmentLength non-fraud' )
plt
. show
( )
其次查看連續型變量在不同y值上的分布
total
= len ( data_train
)
total_amt
= data_train
. groupby
( [ 'isDefault' ] ) [ 'loanAmnt' ] . sum ( ) . sum ( )
plt
. figure
( figsize
= ( 12 , 5 ) )
plt
. subplot
( 121 )
plot_tr
= sns
. countplot
( x
= 'isDefault' , data
= data_train
)
plot_tr
. set_title
( "Fraud Loan Distribution \n 0: good user | 1: bad user" , fontsize
= 14 )
plot_tr
. set_xlabel
( "Is fraud by count" , fontsize
= 16 )
plot_tr
. set_ylabel
( 'Count' , fontsize
= 16 )
for p
in plot_tr
. patches
: height
= p
. get_height
( ) plot_tr
. text
( p
. get_x
( ) + p
. get_width
( ) / 2 . , height
+ 3 , '{:1.2f}%' . format ( height
/ total
* 100 ) , ha
= "center" , fontsize
= 15 ) percent_amt
= ( data_train
. groupby
( [ 'isDefault' ] ) [ 'loanAmnt' ] . sum ( ) )
percent_amt
= percent_amt
. reset_index
( )
plt
. subplot
( 122 )
plot_tr_2
= sns
. barplot
( x
= 'isDefault' , y
= 'loanAmnt' , dodge
= True , data
= percent_amt
)
plot_tr_2
. set_title
( "Total Amount in loanAmnt \n 0: good user | 1: bad user" , fontsize
= 14 )
plot_tr_2
. set_xlabel
( "Is fraud by percent" , fontsize
= 16 )
plot_tr_2
. set_ylabel
( 'Total Loan Amount Scalar' , fontsize
= 16 )
for p
in plot_tr_2
. patches
: height
= p
. get_height
( ) plot_tr_2
. text
( p
. get_x
( ) + p
. get_width
( ) / 2 . , height
+ 3 , '{:1.2f}%' . format ( height
/ total_amt
* 100 ) , ha
= "center" , fontsize
= 15 )
查看某一個數值型變量的分布,查看變量是否符合正態分布,如果不符合正太分布的變量可以log化后再觀察下是否符合正態分布。 如果想統一處理一批數據變標準化 必須把這些之前已經正態化的數據提出 正態化的原因:一些情況下正態非正態可以讓模型更快的收斂,一些模型要求數據正態(eg. GMM、KNN),保證數據不要過偏態即可,過于偏態可能會影響模型預測結果。
**上面我們用value_counts()等函數看了特征屬性的分布,但是圖表是概括原始信息最便捷的方式。
數無形時少直覺。
同一份數據集,在不同的尺度刻畫上顯示出來的圖形反映的規律是不一樣的。python將數據轉化成圖表,但結論是否正確需要由你保證。**
2.3.6 時間格式數據處理及查看
data_train
[ 'issueDate' ] = pd
. to_datetime
( data_train
[ 'issueDate' ] , format = '%Y-%m-%d' )
startdate
= datetime
. datetime
. strptime
( '2007-06-01' , '%Y-%m-%d' )
data_train
[ 'issueDateDT' ] = data_train
[ 'issueDate' ] . apply ( lambda x
: x
- startdate
) . dt
. days
plt
. hist
( data_train
[ 'issueDateDT' ] , label
= 'train' ) ;
plt
. legend
( ) ;
plt
. title
( 'Distribution of issueDateDT dates' ) ;
2.3.7 掌握透視圖可以讓我們更好的了解數據
pivot
= pd
. pivot_table
( data_train
, index
= [ 'grade' ] , columns
= [ 'issueDateDT' ] , values
= [ 'loanAmnt' ] , aggfunc
= np
. sum )
pivot
數據間相關關系 特征和特征之間關系 特征和目標變量之間關系 用pandas_profiling生成數據報告
import pandas_profiling
pfr
= pandas_profiling
. ProfileReport
( data_train
)
pfr
. to_file
( "./example.html" )
總結
以上是生活随笔 為你收集整理的数据挖掘实践(金融风控) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。