示例三(3)——人物画像特征提取

前言:一个人的信用评级一般用人物画像来评判,如何从很多的人物特征中提取有用的特征呢?
下面以一个金融反欺诈模型为例子来对特征提取有一个简单的理解。
数据下载地址:Notes offered by Prospectus (https://www.lendingclub.com/info/prospectus.action)
一共有145行特征,
1删除了肉眼看的见的空值列

import pandas as pd
import numpy as np
import sys

df = pd.read_csv('./data/LoanStats3a.csv', skiprows = 1, low_memory = True)#skiprows跳过第一行,low_memory低内存加载,报错就该成False
'''读入接待信息'''
# print(df.head(10))
# print(df.info())
'''查看数据特征表格信息'''
df.drop('id', axis = 1, inplace = True)
df.drop('member_id', axis = 1, inplace = True)

2清洗数据,去除特征中的特殊字符

df.term.replace(to_replace = '[^0-9]+', value = '', inplace = True, regex = True)#regex正则打开
df.int_rate.replace('%', value = '', inplace = True)#去不掉说明就是浮点型
df.drop('sub_grade', axis = 1, inplace = True)
df.drop('emp_title', axis = 1, inplace = True)

df.emp_length.replace('n/a', np.nan, inplace = True)
df.emp_length.replace(to_replace = '[^0-9]+', value = '', inplace = True, regex = True)
#这一步是必须做的,这样做以后才能,用info查看
df.dropna(axis = 1, how = 'all', inplace = True)
df.dropna(axis = 0, how = 'all', inplace = True)

3删除空值较多的列

'''debt_settlement_flag_date     98 non-null object
settlement_status             155 non-null object
settlement_date               155 non-null object
settlement_amount             155 non-null float64
settlement_percentage         155 non-null float64
settlement_term               155 non-null float64'''
df.drop(['debt_settlement_flag_date','settlement_status','settlement_date',\
         'settlement_amount','settlement_percentage',\
         'settlement_term'], axis = 1, inplace = True)

4删除不为空,但是重复较多的列;先删float,再删object

# for col in df.select_dtypes(include = ['float']).columns:
#     print('col {} has {}'.format(col, len(df[col].unique())))

'''
col delinq_2yrs has 13
col inq_last_6mths has 29
col mths_since_last_delinq has 96
col mths_since_last_record has 114
col open_acc has 45
col pub_rec has 7

col total_acc has 84
col out_prncp has 1
col out_prncp_inv has 1

col collections_12_mths_ex_med has 2
col policy_code has 1
col acc_now_delinq has 3
col chargeoff_within_12_mths has 2
col delinq_amnt has 4
col pub_rec_bankruptcies has 4
col tax_liens has 3
'''
df.drop(['delinq_2yrs','inq_last_6mths','mths_since_last_delinq',\
         'mths_since_last_record','open_acc','pub_rec','total_acc',\
         'out_prncp','out_prncp_inv','collections_12_mths_ex_med',\
         'policy_code','acc_now_delinq','chargeoff_within_12_mths',\
         'delinq_amnt','pub_rec_bankruptcies',\
         'tax_liens'], axis = 1, inplace = True)

'''删除objetct类型中数据重复较多的值'''
# for col in df.select_dtypes(include = ['object']).columns:
    # print('col {} has {}'.format(col, len(df[col].unique())))

'''
col term has 2
col grade has 7
col emp_length has 11
col home_ownership has 5
col verification_status has 3
col issue_d has 55

col pymnt_plan has 1
col purpose has 1
col zip_code has 837
col addr_state has 50
col earliest_cr_line has 531
col initial_list_status has 1

col last_pymnt_d has 113
col next_pymnt_d has 99
col last_credit_pull_d has 125
col application_type has 1
col hardship_flag has 1
col disbursement_method has 1
col debt_settlement_flag has 2
''' 
df.drop(['term','grade','emp_length','home_ownership','verification_status'\
         ,'issue_d','pymnt_plan','purpose','zip_code','addr_state',\
         'earliest_cr_line','initial_list_status','last_pymnt_d',\
         'next_pymnt_d','last_credit_pull_d','application_type','hardship_flag',
         'disbursement_method','debt_settlement_flag'], axis = 1, inplace = True)

df.drop(['desc','title'], axis = 1, inplace = True)

5标签二值化

df.loan_status.replace('Fully Paid', value = int(1), inplace = True)
df.loan_status.replace('Charged Off', value = int(0), inplace = True)
df.loan_status.replace('Does not meet the credit policy. Status:Fully Paid', \
                       np.nan, inplace = True)
df.loan_status.replace('Does not meet the credit policy. Status:Charged Off', \
                       np.nan, inplace = True)
'''删除标签为空的实力,大概删除了3000个不到的实力'''
df.dropna(subset = ['loan_status'], how = 'any', inplace = True)

6把样本中的空值用0.0去填充

df.fillna(0.0, inplace = True)

7计算清洁后样本数据的相关性,删除相关系数大于0.95的列

cor = df.corr()#协方差矩阵
# cor.iloc[:, :] = np.tril(cor, k= -1)
# cor = cor.stack()
# print(cor[(cor>0.55)|(cor<-0.55)])
# sys.exit(0)
'''loan_amnt
funded_amnt
total_pymnt'''
'''删除相关系数大于0.95的列'''
df.drop(['loan_amnt','funded_amnt','total_pymnt'], axis = 1, inplace = True)
print(df.info())#revol_util                 39786 non-null object"%"会默认为object,其实他是数值
# sys.exit(0)
df = pd.get_dummies(df)#哑变量
df.to_csv('./data/feature03.csv')
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 机器学习是做NLP和计算机视觉这类应用算法的基础,虽然现在深度学习模型大行其道,但是懂一些传统算法的原理和它们之间...
    在河之简阅读 20,399评论 4 65
  • 昨宵无言花弄影,青菡萏,白圭玷。长乐未央,故伊何处是。吟罢叹罢黪发,嘲影只,睹物寒。落絮飞苟驹过隙。白螺杯,金玉徽...
    常叔叔不舒服阅读 373评论 0 3
  • 前几天写了一个SpringBoot对拦截器的使用,在实际项目中,对一些情况需要做一些安全验证,比如在没有登录的情况...
    dalaoyang阅读 1,239评论 0 29
  • 今天个案小非又因为近期在新跳槽的单位中遇到了难题来找我做疗愈。她感觉刚刚进入新环境还没来及适应,就要挑起很大的责任...
    小神姐阅读 230评论 0 0