海量特征按照缺失值null/NAN数量异同进行分组归类
生活随笔
收集整理的這篇文章主要介紹了
海量特征按照缺失值null/NAN数量异同进行分组归类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
有些數(shù)據(jù)集中,很多特征都有大量的缺失值
如果一些特征的缺失值數(shù)量是一致的,那么這些特征可能是重復(fù)特征或者是關(guān)聯(lián)特別大的.
代碼如下:
########################### Let's find nan groups in V columns#這里是重點####################### nans_groups = {} nans_df = pd.concat([train_df, test_df]).isna()#顯示每列中是否有null值 # print("nans_df=",nans_df)i_cols = ['V'+str(i) for i in range(1,340)]#所有的相似列名for col in i_cols:cur_group = nans_df[col].sum()#統(tǒng)計該列中的null數(shù)量,然后把null數(shù)量當(dāng)做key值if cur_group>0:try:#如果key對應(yīng)的列表存在nans_groups[cur_group].append(col)#字典,key是cur_group,value是nans_groups[cur_group](類型是list)except:#如果key對應(yīng)的列表不存在nans_groups[cur_group]=[col]for n_group,n_members in nans_groups.items():print("n_group=",n_group)#打印的是null數(shù)量print("n_members=",n_members)print("------------------------------------------------------------")?
進行KS檢驗,檢查train和test的某個特定特征的數(shù)據(jù)的分布是否一致(因為上面的代碼僅僅是檢測null數(shù)量一致而已,null數(shù)量一致并不代表分布就一致,所以下面進行重復(fù)加檢查)
########################### Let's check p values ########################### features_check = []for col in test_group:features_check.append(ks_2samp(train_df[col], test_df[col])[1])#比較train和test中同一個屬性的概率分布是否一致features_check = pd.Series(features_check, index=test_group).sort_values() print(features_check)?
用這些同分布的特征進行建模
########################### Lest try model with these features if MAKE_MODEL_TEST:remove_features = [col for col in remove_features if col not in test_group]test_predictions = make_test_predictions(train_df, test_df, TARGET, lgb_params)print(metrics.roc_auc_score(test_predictions[TARGET], test_predictions['prediction']))remove_features += test_group?
[2]中提到一個觀點:
NAN一致的特征都來自同一個數(shù)據(jù)源
?
?
?
Reference:
[1]https://www.kaggle.com/kyakovlev/ieee-v-columns-pv
[2]https://www.kaggle.com/c/ieee-fraud-detection/discussion/105130#latest-625590
總結(jié)
以上是生活随笔為你收集整理的海量特征按照缺失值null/NAN数量异同进行分组归类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用K-S检验一个数列是否服从正态分布、
- 下一篇: 多进程减少多个文件的内存占用