📓
Study
  • README
  • Application
    • Contest
      • 竞赛trick
  • Basic Know
    • 半监督学习
    • 贝叶斯
      • 朴素贝叶斯分类器
    • 对抗训练
    • 概率图模型
      • CRF
      • HMM
      • 概率图模型
    • 关联分析
    • 归纳偏置
      • [什么是 Inductive bias(归纳偏置)?](BasicKnow/归纳偏置/什么是 Inductive bias(归纳偏置)?.md)
    • 聚类
    • 决策树
    • 绿色深度学习
    • 树模型&集成学习
      • 提升树
      • Ada Boost
      • [集成学习]
    • 特征工程
      • 数据分桶
      • 特征工程概述
      • 特征选择
      • LDA
      • PCA
    • 线性模型
      • 感知机
      • 最大熵模型
      • SVM
        • SVM支持向量机
      • 逻辑回归
      • 线性回归
    • 优化算法
      • 拉格朗日对偶性
      • 牛顿法
        • 牛顿法&拟牛顿法
      • 梯度下降法
        • 梯度下降算法
      • 优化算法
    • 预处理
      • [1-1]正则表达式
      • [1-2]文本预处理
      • [1-3]词性
      • [1-4]语法分析
      • [1-6]文本分类
      • [1-7]网络爬取
      • 【备用】正则表达式
      • 7.re模块
      • 词典匹配
      • 分词
      • 子表达式
      • Todo
    • 主题模型
      • LDA
    • Deep Learning
      • 反向传播
      • 梯度消失&梯度爆炸
      • Batch Size
      • 1.DLbasis
      • 小概念
      • MLstrategy
      • CNN
      • RNN及其应用
      • 关于深度学习实践
      • 神经网络概述
      • Batch Normalization
      • Program CNN
      • Program D Lbasis
      • Program DN Nimprove
      • Program Neural Style Transfer
      • Summer DL
    • EM算法
    • GAN
      • Gans In Action Master
    • GNN
      • 搜广推之GNN
      • Representation Learning
        • Anomalydetection
        • Conclusion
        • Others
        • Papernotes
        • Recommadation
    • k近邻法
      • K近邻
    • Language Model
      • 语言模型解码采样策略
      • [1-1][语言模型]从N-gram模型讲起
      • [1-2][语言模型]NNLM(神经网络语言模型)
      • [1-3][语言模型]基于RNN的语言模型
      • [1-4][语言模型]用N-gram来做完形填空
      • [1-5][语言模型]用KenLM来做完形填空
    • Loss Function
      • 常用损失函数
      • Focal Loss
      • softmax+交叉熵
    • Machine Learning
      • [基础]概念
      • 待整合
      • 交叉验证
      • 无监督学习
      • 优缺点
      • ML Yearning
      • SVD
    • Statistics Math
      • 程序员的数学基础课
      • 数学基础
      • 统计&高数
      • 统计题目
      • 线性代数
      • 组合数学
      • Discrete Choice Model
      • Nested Choice Model
  • Course Note
    • 基于TensorFlow的机器学习速成课程
      • [Key ML Terminology](CourseNote/基于TensorFlow的机器学习速成课程/Key ML Terminology.md)
    • 集训营
      • 任务说明
      • 算法实践1.1模型构建
      • 算法实践1.2模型构建之集成模型
      • 算法实践2.1数据预处理
    • 李宏毅机器学习
      • 10DNN训练Tips
        • Chapter 18
      • 16无监督学习
        • Chapter 25
    • 贪心NLP
      • 贪心NLP笔记
    • Cs 224 N 2019
      • [A Simple But Tough To Beat Baseline For Sentence Embeddings](CourseNote/cs224n2019/A Simple but Tough-to-beat Baseline for Sentence Embeddings.md)
      • [Lecture 01 Introduction And Word Vectors](CourseNote/cs224n2019/Lecture 01 Introduction and Word Vectors.md)
      • [Lecture 02 Word Vectors 2 And Word Senses](CourseNote/cs224n2019/Lecture 02 Word Vectors 2 and Word Senses.md)
      • [Lecture 03 Word Window Classification Neural Networks And Matrix Calculus](CourseNote/cs224n2019/Lecture 03 Word Window Classification, Neural Networks, and Matrix Calculus.md)
      • [Lecture 04 Backpropagation And Computation Graphs](CourseNote/cs224n2019/Lecture 04 Backpropagation and Computation Graphs.md)
      • [Lecture 05 Linguistic Structure Dependency Parsing](CourseNote/cs224n2019/Lecture 05 Linguistic Structure Dependency Parsing.md)
      • [Lecture 06 The Probability Of A Sentence Recurrent Neural Networks And Language Models](CourseNote/cs224n2019/Lecture 06 The probability of a sentence Recurrent Neural Networks and Language Models.md)
      • Stanford NLP
    • Deep Learning Book Goodfellow
      • Books
        • Deep Learning Book Chapter Summaries Master
      • 提纲
      • C 5
      • C 6
      • [Part I Applied Math And Machine Learning Basics](CourseNote/Deep-Learning-Book-Goodfellow/Part I - Applied Math and Machine Learning basics.md)
    • Lihang
    • NLP实战高手课
      • 极客时间_NLP实战高手课
    • 工具&资料
    • 机器学习、深度学习面试知识点汇总
    • 七月kaggle课程
    • 算法工程师
    • 贪心科技机器学习必修知识点特训营
    • 唐宇迪机器学习
    • 语言及工具
    • AI技术内参
    • Suggestions
  • Data Related
    • 数据质量
      • 置信学习
    • 自然语言处理中的数据增广_车万翔
      • 自然语言处理中的数据增广
    • Mixup
    • 数据不均衡问题
    • 数据增强的方法
  • Knowledge Graph
    • Information Extraction
      • 联合抽取
        • PRGC
      • Code
        • BERT微调
      • NER
        • 阅读理解做NER
          • MRC
        • FLAT
        • Global Pointer
        • 命名实体识别NER
    • Keyword Extraction
      • 关键词抽取
    • 小米在知识表示学习的探索与实践
    • KG
  • Multi Task
    • EXT 5
      • Ex T 5
  • NLG
    • Dailogue
      • 比赛
        • 对话评估比赛
          • [simpread-DSTC10 开放领域对话评估比赛冠军方法总结](NLG/Dailogue/比赛/对话评估比赛/simpread-DSTC10 开放领域对话评估比赛冠军方法总结.md)
      • 任务型对话
        • DST
          • DST概述
        • NLG
          • NLG概述
        • NLU
          • NLU概述
        • 任务型对话概述
        • simpread-任务型对话系统预训练最新研究进展
      • 问答型对话
        • 检索式问答
          • 基于预训练模型的检索式对话系统
          • 检索式文本问答
        • 业界分享
          • 低资源场景下的知识图谱表示学习和问答_阿里_李杨
          • QQ浏览器搜索智能问答
        • 问答型对话系统概述
      • 闲聊型对话
        • 闲聊型对话系统概述
      • 业界分享
        • 人工智能与心理咨询
        • 腾讯多轮对话机器人
        • 微软小冰
        • 小布助手闲聊生成式算法
        • 美团智能客服实践_江会星
        • 去哪儿智能客服探索和实践
        • 实时语音对话场景下的算法实践_阿里_陈克寒
        • 智能语音交互中的无效query识别_小米_崔世起
        • UNIT智能对话
      • 主动对话
      • EVA
        • EVA分享
        • EVA模型
      • PLATO
      • RASA
    • Machine Translation
      • 业界分享
        • 爱奇艺台词翻译分享
      • Paper
        • Deep Encoder Shallow Decoder
    • RAGRelated
    • Text 2 SQL
      • M SQL
        • [M SQL 2](NLG/Text2SQL/M-SQL/M-SQL (2).md)
      • [Text2SQL Baseline解析](NLG/Text2SQL/Text2SQL Baseline解析.md)
      • Text 2 SQL
    • Text Summarization
      • [文本摘要][paper]CTRLSUM
      • 文本摘要
  • Pre Training
    • 业界分享
      • 超大语言模型与语言理解_黄民烈
        • 超大语言模型与语言理解
      • 大模型的加速算法_腾讯微信
        • 大模型的加速算法
      • 孟子轻量化预训练模型
      • 悟道文汇文图生成模型
      • 悟道文澜图文多模态大模型
      • 语义驱动可视化内容创造_微软
        • 语义驱动可视化内容创造
    • Base
      • Attention
      • Mask
        • NLP中的Mask
      • Position Encoding
        • 位置编码
    • BERT
      • ALBERT
      • Bert
        • Venv
          • Lib
            • Site Packages
              • idna-3.2.dist-info
                • LICENSE
              • Markdown-3.3.4.dist-info
                • LICENSE
              • Tensorflow
                • Include
                  • External
                    • Libjpeg Turbo
                      • LICENSE
                  • Unsupported
                    • Eigen
                      • CXX 11
                        • Src
                          • Tensor
              • Werkzeug
                • Debug
                  • Shared
                    • ICON LICENSE
        • CONTRIBUTING
        • Multilingual
      • Ro BER Ta
      • BERT
      • BERT面试问答
      • BERT源码解析
      • NSP BERT
    • BERT Flow
    • BERT Zip
      • Distilling The Knowledge In A Neural Network
      • TINYBERT
      • 模型压缩
    • CPM
    • CPT
      • 兼顾理解和生成的中文预训练模型CPT
    • ELECTRA
    • EL Mo
    • ERNIE系列语言模型
    • GPT
    • MBART
    • NEZHA
    • NLG Sum
      • [simpread-预训练时代下的文本生成|模型 & 技巧](Pre-training/NLGSum/simpread-预训练时代下的文本生成|模型 & 技巧.md)
    • Prompt
      • 预训练模型的提示学习方法_刘知远
        • 预训练模型的提示学习方法
    • T 5
      • Unified SKG
      • T 5
    • Transformer
    • Uni LM
    • XL Net
    • 预训练语言模型
    • BERT变种
  • Recsys
    • 多任务Multi-task&推荐
    • 推荐介绍
    • 推荐系统之召回与精排
      • 代码
        • Python
          • Recall
            • Deep Match Master
              • Docs
                • Source
                  • Examples
                  • FAQ
                  • Features
                  • History
                  • Model Methods
                  • Quick Start
    • 业界分享
      • 腾讯基于知识图谱长视频推荐
    • 召回
    • Sparrow Rec Sys
    • 深度学习推荐系统实战
    • 推荐模型
    • Deep FM
  • Search
    • 搜索
    • 业界分享
      • 爱奇艺搜索排序算法实践
      • 语义搜索技术和应用
    • 查询关键字理解
    • 搜索排序
    • BM 25
    • KDD21-淘宝搜索中语义向量检索技术
    • query理解
    • TFIDF
  • Self Supervised Learning
    • Contrastive Learning
      • 业界分享
        • 对比学习在微博内容表示的应用_张俊林
      • Paper
      • R Drop
      • Sim CSE
    • 自监督学习
  • Text Classification
    • [多标签分类(Multi-label Classification)](TextClassification/多标签分类(Multi-label Classification)/多标签分类(Multi-label Classification).md)
    • Fast Text
    • Text CNN
    • 文本分类
  • Text Matching
    • 文本匹配和多轮检索
    • CNN SIM
    • Word Embedding
      • Skip Gram
      • Glove
      • Word 2 Vec
    • 文本匹配概述
  • Tool
    • 埋点
    • 向量检索(Faiss等)
    • Bigdata
      • 大数据基础task1_创建虚拟机+熟悉linux
      • 任务链接
      • Mr
      • Task1参考答案
      • Task2参考答案
      • Task3参考答案
      • Task4参考答案
      • Task5参考答案
    • Docker
    • Elasticsearch
    • Keras
    • Numpy
    • Python
      • 可视化
        • Interactivegraphics
        • Matplotlib
        • Tkinter
        • Turtle
      • 数据类型
        • Datatype
      • python爬虫
        • Python Scraping Master
          • phantomjs-2.1.1-windows
        • Regularexp
        • Scrapying
        • Selenium
      • 代码优化
      • 一行代码
      • 用python进行语言检测
      • Debug
      • Exception
      • [Features Tricks](Tool/python/Features & Tricks.md)
      • Fileprocess
      • Format
      • Functional Programming
      • I Python
      • Magic
      • Math
      • Os
      • Others
      • Pandas
      • Python Datastructure
      • Python操作数据库
      • Streamlit
      • Time
    • Pytorch
      • Dive Into DL Py Torch
        • 02 Softmax And Classification
        • 03 Mlp
        • 04 Underfit Overfit
        • 05 Gradient Vanishing Exploding
        • 06 Text Preprocess
        • 07 Language Model
        • 08 Rnn Basics
        • 09 Machine Translation
        • 10 Attention Seq 2 Seq
        • 11 Transformer
        • 12 Cnn
        • 14 Batchnorm Resnet
        • 15 Convexoptim
        • 16 Gradientdescent
        • 17 Optim Advance
    • Spark
      • Pyspark
        • pyspark之填充缺失的时间数据
      • Spark
    • SQL
      • 数据库
      • Hive Sql
      • MySQL实战45讲
    • Tensor Flow
      • TensorFlow入门
  • Common
  • NLP知识体系
Powered by GitBook
On this page
  • 任务:数据预处理
  • 1.查看数据
  • 2.属性转换
  • 3.剔除不相关特征
  • 4.模型应用

Was this helpful?

  1. Course Note
  2. 集训营

算法实践2.1数据预处理

任务:数据预处理

对数据进行探索和分析:

  • 数据类型的分析

  • 无关特征删除

  • 数据类型转换

  • 缺失值处理

  • ...以及你能想到和借鉴的数据分析处理

1.查看数据

首先读入数据,查看数据的维度,并且输出5行感受一下~

import pandas as pd
data = pd.read_csv('./data_all.csv', engine='python')
print(data.shape)
data.head()

输出:

(4754, 85)

low_volume_percent

middle_volume_percent

take_amount_in_later_12_month_highest

trans_amount_increase_rate_lately

trans_activity_month

trans_activity_day

transd_mcc

trans_days_interval_filter

trans_days_interval

regional_mobility

...

consfin_product_count

consfin_max_limit

consfin_avg_limit

latest_query_day

loans_latest_day

reg_preference_for_trad

latest_query_time_month

latest_query_time_weekday

loans_latest_time_month

loans_latest_time_weekday

0

0.01

0.99

0

0.90

0.55

0.313

17.0

27.0

26.0

3.0

...

2.0

1200.0

1200.0

12.0

18.0

0

4.0

2.0

4.0

3.0

1

0.02

0.94

2000

1.28

1.00

0.458

19.0

30.0

14.0

4.0

...

6.0

22800.0

9360.0

4.0

2.0

0

5.0

3.0

5.0

5.0

2

0.04

0.96

0

1.00

1.00

0.114

13.0

68.0

22.0

1.0

...

1.0

4200.0

4200.0

2.0

6.0

0

5.0

5.0

5.0

1.0

3

0.00

0.96

2000

0.13

0.57

0.777

22.0

14.0

6.0

3.0

...

5.0

30000.0

12180.0

2.0

4.0

1

5.0

5.0

5.0

3.0

4

0.01

0.99

0

0.46

1.00

0.175

13.0

66.0

42.0

1.0

...

2.0

8400.0

8250.0

22.0

120.0

0

4.0

6.0

1.0

6.0

5 rows × 85 columns

可见一共是4754行,85列。所有的列似乎都是数值型的,为了更确定,我们用info()函数看看数据类型:

data.info()

输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4754 entries, 0 to 4753
Data columns (total 85 columns):
low_volume_percent                            4754 non-null float64
middle_volume_percent                         4754 non-null float64
take_amount_in_later_12_month_highest         4754 non-null int64
trans_amount_increase_rate_lately             4754 non-null float64
trans_activity_month                          4754 non-null float64
trans_activity_day                            4754 non-null float64
transd_mcc                                    4754 non-null float64
trans_days_interval_filter                    4754 non-null float64
trans_days_interval                           4754 non-null float64
regional_mobility                             4754 non-null float64
repayment_capability                          4754 non-null int64
is_high_user                                  4754 non-null int64
number_of_trans_from_2011                     4754 non-null float64
first_transaction_time                        4754 non-null float64
historical_trans_amount                       4754 non-null int64
historical_trans_day                          4754 non-null float64
rank_trad_1_month                             4754 non-null float64
trans_amount_3_month                          4754 non-null int64
avg_consume_less_12_valid_month               4754 non-null float64
abs                                           4754 non-null int64
top_trans_count_last_1_month                  4754 non-null float64
avg_price_last_12_month                       4754 non-null int64
avg_price_top_last_12_valid_month             4754 non-null float64
trans_top_time_last_1_month                   4754 non-null float64
trans_top_time_last_6_month                   4754 non-null float64
consume_top_time_last_1_month                 4754 non-null float64
consume_top_time_last_6_month                 4754 non-null float64
cross_consume_count_last_1_month              4754 non-null float64
trans_fail_top_count_enum_last_1_month        4754 non-null float64
trans_fail_top_count_enum_last_6_month        4754 non-null float64
trans_fail_top_count_enum_last_12_month       4754 non-null float64
consume_mini_time_last_1_month                4754 non-null float64
max_cumulative_consume_later_1_month          4754 non-null int64
max_consume_count_later_6_month               4754 non-null float64
railway_consume_count_last_12_month           4754 non-null float64
pawns_auctions_trusts_consume_last_1_month    4754 non-null int64
pawns_auctions_trusts_consume_last_6_month    4754 non-null int64
jewelry_consume_count_last_6_month            4754 non-null float64
status                                        4754 non-null int64
first_transaction_day                         4754 non-null float64
trans_day_last_12_month                       4754 non-null float64
apply_score                                   4754 non-null float64
apply_credibility                             4754 non-null float64
query_org_count                               4754 non-null float64
query_finance_count                           4754 non-null float64
query_cash_count                              4754 non-null float64
query_sum_count                               4754 non-null float64
latest_one_month_apply                        4754 non-null float64
latest_three_month_apply                      4754 non-null float64
latest_six_month_apply                        4754 non-null float64
loans_score                                   4754 non-null float64
loans_credibility_behavior                    4754 non-null float64
loans_count                                   4754 non-null float64
loans_settle_count                            4754 non-null float64
loans_overdue_count                           4754 non-null float64
loans_org_count_behavior                      4754 non-null float64
consfin_org_count_behavior                    4754 non-null float64
loans_cash_count                              4754 non-null float64
latest_one_month_loan                         4754 non-null float64
latest_three_month_loan                       4754 non-null float64
latest_six_month_loan                         4754 non-null float64
history_suc_fee                               4754 non-null float64
history_fail_fee                              4754 non-null float64
latest_one_month_suc                          4754 non-null float64
latest_one_month_fail                         4754 non-null float64
loans_long_time                               4754 non-null float64
loans_credit_limit                            4754 non-null float64
loans_credibility_limit                       4754 non-null float64
loans_org_count_current                       4754 non-null float64
loans_product_count                           4754 non-null float64
loans_max_limit                               4754 non-null float64
loans_avg_limit                               4754 non-null float64
consfin_credit_limit                          4754 non-null float64
consfin_credibility                           4754 non-null float64
consfin_org_count_current                     4754 non-null float64
consfin_product_count                         4754 non-null float64
consfin_max_limit                             4754 non-null float64
consfin_avg_limit                             4754 non-null float64
latest_query_day                              4754 non-null float64
loans_latest_day                              4754 non-null float64
reg_preference_for_trad                       4754 non-null int64
latest_query_time_month                       4754 non-null float64
latest_query_time_weekday                     4754 non-null float64
loans_latest_time_month                       4754 non-null float64
loans_latest_time_weekday                     4754 non-null float64
dtypes: float64(73), int64(12)
memory usage: 3.1 MB

85列属性中,有73列是float型,12列是int型,并没有字符串型的属性,所以无需做额外处理。

而且可以看到每个特征都没有缺失值。也可以用另一个函数查看数据中缺失值的个数:

data.isnull().sum(axis=0)

输出:

low_volume_percent                        0
middle_volume_percent                     0
take_amount_in_later_12_month_highest     0
trans_amount_increase_rate_lately         0
trans_activity_month                      0
trans_activity_day                        0
transd_mcc                                0
trans_days_interval_filter                0
trans_days_interval                       0
regional_mobility                         0
repayment_capability                      0
is_high_user                              0
number_of_trans_from_2011                 0
first_transaction_time                    0
historical_trans_amount                   0
historical_trans_day                      0
rank_trad_1_month                         0
trans_amount_3_month                      0
avg_consume_less_12_valid_month           0
abs                                       0
top_trans_count_last_1_month              0
avg_price_last_12_month                   0
avg_price_top_last_12_valid_month         0
trans_top_time_last_1_month               0
trans_top_time_last_6_month               0
consume_top_time_last_1_month             0
consume_top_time_last_6_month             0
cross_consume_count_last_1_month          0
trans_fail_top_count_enum_last_1_month    0
trans_fail_top_count_enum_last_6_month    0
                                         ..
loans_org_count_behavior                  0
consfin_org_count_behavior                0
loans_cash_count                          0
latest_one_month_loan                     0
latest_three_month_loan                   0
latest_six_month_loan                     0
history_suc_fee                           0
history_fail_fee                          0
latest_one_month_suc                      0
latest_one_month_fail                     0
loans_long_time                           0
loans_credit_limit                        0
loans_credibility_limit                   0
loans_org_count_current                   0
loans_product_count                       0
loans_max_limit                           0
loans_avg_limit                           0
consfin_credit_limit                      0
consfin_credibility                       0
consfin_org_count_current                 0
consfin_product_count                     0
consfin_max_limit                         0
consfin_avg_limit                         0
latest_query_day                          0
loans_latest_day                          0
reg_preference_for_trad                   0
latest_query_time_month                   0
latest_query_time_weekday                 0
loans_latest_time_month                   0
loans_latest_time_weekday                 0
Length: 85, dtype: int64

由于属性太多,这里没有显示全,可以将上述结果存起来,然后只输出有缺失值的列:

data_isnull = data.isnull().sum(axis=0)
data_isnull[data_isnull != 0]

输出:

Series([], dtype: int64)

series为空,说明所有列都是无缺失值的。

2.属性转换

虽然读取的数据中所有属性的数据类型都是数值型的,但实际上可能存在一些类别型属性,比如status是0和1两个类别,但是pandas会将其处理成数值的0和1。需要对这种情况进行额外处理。

对于这种类型的数据有两种处理方式,一种是one-hot encoding,把属性的每个取值对应成类似于00001这种形式。比如status有两个取值0和1,那么就可以编码成01和10。sklearn中有相应的函数,也可以用pandas的get_dummies(df)函数。另一种方式是label encoding,只要给每个属性的取值一个唯一的数字即可,比如某个属性是weekday,可以取周一到周日七个值,用label encoding就是0-7这七个数字即可。sklearn中也有相应的函数。

下面我们来看看这个数据中有没有需要转换的属性。

一般这种属性都是int型的,所以我们只看int型的那些属性都有多少个取值。在第一步中我们看到数据类型就是两种:float64和int64,可以用select_dtypes函数选出那些int64的列:

import numpy as np
column = data.select_dtypes(include=[np.int64]).columns
print(column)
for col in column:
    print(col)
    print(data[col].unique().shape)

输出:

Index(['take_amount_in_later_12_month_highest', 'repayment_capability',
       'is_high_user', 'historical_trans_amount', 'trans_amount_3_month',
       'abs', 'avg_price_last_12_month',
       'max_cumulative_consume_later_1_month',
       'pawns_auctions_trusts_consume_last_1_month',
       'pawns_auctions_trusts_consume_last_6_month', 'status',
       'reg_preference_for_trad'],
      dtype='object')
take_amount_in_later_12_month_highest
(166,)
repayment_capability
(2390,)
is_high_user
(2,)
historical_trans_amount
(4524,)
trans_amount_3_month
(3524,)
abs
(1697,)
avg_price_last_12_month
(330,)
max_cumulative_consume_later_1_month
(863,)
pawns_auctions_trusts_consume_last_1_month
(572,)
pawns_auctions_trusts_consume_last_6_month
(2730,)
status
(2,)
reg_preference_for_trad
(5,)

根据属性的取值个数和属性名称大致可以判定该属性是否需要转换。看起来is_high_user和reg_preference_for_trad可能需要转换。

is_high_user只有两个取值,用value_counts函数看看每个取值有多少个:

data['is_high_user'].value_counts()

输出:

0    4701
1      53
Name: is_high_user, dtype: int64

is_high_user取值是0和1,只有两个取值的属性其实也无需转换。

再来看看另一个属性reg_preference_for_trad:

0    3405
1    1064
2     150
3     131
4       4
Name: reg_preference_for_trad, dtype: int64

reg_preference_for_trad有5个取值,如果要用one-hot encoding方式就要转换,如果用label encoding方式就无需转换。为了展示one-hot encoding的用法,这里我们用one-hot方式。

代码如下:

regcols = ['reg_preference_for_trad_' + str(i) for i in range(5)]
print(regcols)

tmpdf = pd.get_dummies(data['reg_preference_for_trad'].replace('nan', np.nan))
tmpdf.columns = regcols
data[regcols] = tmpdf

这样就多出了5列,分别代表0-4这4种取值。

['reg_preference_for_trad_0', 'reg_preference_for_trad_1', 'reg_preference_for_trad_2', 'reg_preference_for_trad_3', 'reg_preference_for_trad_4']

3.剔除不相关特征

由于属性列太多,不方便一一解释说明(况且我也不知道每个字段的含义ε=(´ο`*))))。属性列这么多,是不是都有用呢?这就跟我们的目标有关了。从之前的实验可以知道,我们希望预测用户贷款是否会逾期(对应的字段就是status),所以可以看每个属性跟status是否有关。

在这之前需要将X和y提取出来:

y = data['status']
X = data.drop(['status'], axis=1)

sklearn提供了特征选择的模块feature_selection,里面包含了一些函数,这里使用的是特征方差提取方式,默认方差为0的特征会删除。

from sklearn.feature_selection import VarianceThreshold
selector = VarianceThreshold()
print(X.shape)
X = selector.fit_transform(X)
print(X.shape)

输出:

(4754, 89)
(4754, 89)

没有剔除任何特征。。说明这里面每个特征多少还是有点用处,只是用处大小的不同罢了。

可以输出每个特征的方差看看:

selector.variances_
array([1.72347223e-03, 2.09733428e-02, 1.53943134e+07, 4.81481182e+05,
       3.87692950e-02, 2.89599675e-02, 2.00186059e+01, 5.15710959e+02,
       2.71270743e+02, 7.92284964e-01, 2.72612789e+09, 1.10242173e-02,
       1.01111554e+02, 2.19082523e+08, 1.02694235e+11, 9.93174477e+03,
       6.96454281e-02, 1.03500977e+10, 1.93296547e+00, 7.29256913e+08,
       1.22878638e-01, 5.86439064e+05, 9.86150936e-03, 2.83157843e+01,
       1.67784749e+02, 2.97955539e+01, 1.72043674e+02, 5.03120265e+00,
       3.64002171e+00, 1.98057119e+01, 2.25829209e+01, 1.39411696e+11,
       1.16906146e+08, 3.22683811e+01, 2.28337441e-01, 4.37714017e+07,
       7.94572765e+08, 4.06031438e-02, 2.88337722e+05, 3.63453287e+02,
       2.51783949e+03, 1.63248201e+01, 4.78827044e+01, 1.36140674e+01,
       6.51328333e+00, 1.22335903e+02, 2.02883578e+01, 5.63612962e+01,
       8.29306545e+01, 3.62433789e+03, 4.68725564e+00, 5.84720597e+02,
       4.51090812e+02, 9.62979328e+00, 5.40022384e+01, 8.46934738e+00,
       2.86055044e+01, 2.15115553e+00, 1.16604483e+01, 1.15677503e+02,
       8.68487650e+02, 6.08384690e+02, 3.63337054e+00, 1.43106069e+01,
       1.23543567e+03, 4.71113041e+05, 1.10618434e+02, 2.86055044e+01,
       3.29804734e+01, 2.10664740e+06, 5.13126727e+05, 5.58729026e+07,
       1.98297162e+02, 8.46934738e+00, 1.11852954e+01, 1.92755652e+08,
       3.39896154e+07, 1.33199490e+03, 2.83489836e+03, 4.72394037e-01,
       1.77858966e+00, 3.73234062e+00, 8.92465960e+00, 3.59356702e+00,
       2.03240714e-01, 1.73719927e-01, 3.05568245e-02, 2.67964236e-02,
       8.40688770e-04])

有些特征的方差很大,有些特征的方差很小,其实看看特征的范围就大概能理解这一点了,方差大的特征大多是因为取值范围大,也不能完全靠这个说明特征与标签的相关性大小。可以设置阈值剔除一些不重要的特征,这里就不剔除了。

4.模型应用

根据之前的实验,选出一个模型看看效果即可。这里选择用虽然简单但效果好的逻辑回归模型。

def get_metric(clf, X, y_true):
    y_pred = clf.predict(X)
    y_proba = clf.predict_proba(X)

    acc = metrics.accuracy_score(y_true, y_pred)
    p = metrics.precision_score(y_true, y_pred)
    r = metrics.recall_score(y_true, y_pred)
    f1 = metrics.f1_score(y_true, y_pred)
    fpr, tpr, thresholds = metrics.roc_curve(y_true, y_proba[:, 1])
    auc = metrics.auc(fpr, tpr)
    return acc, p, r, f1, fpr, tpr, auc


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegressionCV
import matplotlib.pyplot as plt
from sklearn import metrics
from sklearn.preprocessing import StandardScaler

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2018)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)

clf = LogisticRegressionCV(class_weight='balanced', max_iter=1000)

clf.fit(X_train, y_train)
acc, p, r, f1, fpr_train, tpr_train, auc_train = get_metric(clf, X_train, y_train)
print("train accuracy:{:.2%}, precision:{:.2%}, recall:{:.2%}, F1:{:.2}".format(acc, p, r, f1))
acc, p, r, f1, fpr_test, tpr_test, auc_test = get_metric(clf, X_test, y_test)
print("test accuracy:{:.2%}, precision:{:.2%}, recall:{:.2%}, F1:{:.2}".format(acc, p, r, f1))

plt.figure()
lw = 2
plt.plot(fpr_train, tpr_train, color='darkorange', lw=lw, label='train (AUC:%0.2f)' % auc_train)
plt.plot(fpr_test, tpr_test, color='cornflowerblue', lw=lw, label='test (AUC:%0.2f)' % auc_test)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic of LR')
plt.legend(loc="lower right")
plt.show()

输出:

train accuracy:76.28%, precision:52.01%, recall:69.78%, F1:0.6
test accuracy:72.32%, precision:46.51%, recall:66.85%, F1:0.55
Previous算法实践1.2模型构建之集成模型Next李宏毅机器学习

Last updated 3 years ago

Was this helpful?