机器学习实战(1)信用卡欺诈

The datasets contains transactions made by credit cards in September 2013 by european cardholders. This dataset presents transactions that occurred in two days, where we have 492 frauds out of 284,807 transactions. The dataset is highly unbalanced, the positive class (frauds) account for 0.172% of all transactions. It contains only numerical input variables which are the result of a PCA transformation. Unfortunately, due to confidentiality issues, we cannot provide the original features and more background information about the data. Features V1, V2, … V28 are the principal components obtained with PCA, the only features which have not been transformed with PCA are ‘Time’ and ‘Amount’. Feature ‘Time’ contains the seconds elapsed between each transaction and the first transaction in the dataset. The feature ‘Amount’ is the transaction Amount, this feature can be used for example-dependant cost-senstive learning. Feature ‘Class’ is the response variable and it takes value 1 in case of fraud and 0 otherwise.


1 数据分析

1.1 数据分布

1.1.1平衡性

题目中已经提到了该数据集非常不平衡,到达什么程度了?欺诈数据仅占0.172%。 但是就算题目没有明显提出来该问题,作为数据分析的第一步应该是查看数据集的分布。 首先载入下载好的数据集:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = pd.read_csv("D:/PROSOFT/Cadence/creditcard.csv")

对于数字型的变量,直接看分布。

num_classes = pd.value_counts(data['Class'], sort = True).sort_index()
num_classes.plot(kind = 'bar')
plt.title("Fraud class histogram")
plt.xlabel("Class")
plt.ylabel("Frequency")

这里写图片描述

不能更明显的数据分布不平衡

那么问题来了,这么不平衡的数据该怎么处理?
一是欠采样,该方法学习到的模型不完整;
二是上采样。但是随机过采样容易产生过拟合,所以有一些改进型的过采样可以稍微避免过拟合。

1.1.2 离群点

离群点就是明显与大部分数据相差很远的数据,离群点将会影响最后的分布,所以需要丢弃。

1.2 完整性
1.2.1 缺失值
接下来查看数据是否完整,是否有缺失值。

1.3 数据相关性

1.3.1 属性信息

Fraud
count     492.000000
mean      122.211321
std       256.683288
min         0.000000
25%         1.000000
50%         9.250000
75%       105.890000
max      2125.870000
Name: Amount, dtype: float64

Normal
count    284315.000000
mean         88.291022
std         250.105092
min           0.000000
25%           5.650000
50%          22.000000
75%          77.050000
max       25691.160000
Name: Amount, dtype: float64

该题因为特殊原因,真正有用的属性信息就是amount 接下来就看这些属性与类别标签的关系

1.3.2 类别信息

f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(12,4))
bins = 30
ax1.hist(data.Amount[data.Class == 1], bins = bins)
ax1.set_title('Fraud')

ax2.hist(data.Amount[data.Class == 0], bins = bins)
ax2.set_title('Normal')

plt.xlabel('Amount ($)')
plt.ylabel('Number of Transactions')
plt.yscale('log')
plt.show()

这里写图片描述

可以看出 金额其实越小,发生诈骗的机会越多。

1.4 归一化

这里是为了消除量纲的影响

from sklearn.preprocessing import StandardScaler
data['Amount'] = StandardScaler().fit_transform(data['Amount'].reshape(-1, 1))

现在可以对数据的不平衡进行处理了: 直接上采样。 完毕后查看,好了现在到了五五开了,于是产生了新的数据集了。

2 特征选择

特征越多,能够选出来的特征就越多。 同时,特征少,速度快; 特征之间如果有相关性就会冗余;
但是像该题,其特征选择很模糊。

于是我们就可以用随机森林中的 Feature Importance来选择,实用又不贵。 随机森林中的树的数量越多,模型越靠谱。


未完待续————————————————

2.1 平均不纯度减少

随机森林由多个决策树构成。决策树中的每一个节点都是关于某个特征的条件,为的是将数据集按照不同的响应变量一分为二。利用不纯度可以确定节点(最优条件),对于分类问题,通常采用基尼不纯度或者信息增益,对于回归问题,通常采用的是方差或者最小二乘拟合。当训练决策树的时候,可以计算出每个特征减少了多少树的不纯度。对于一个决策树森林来说,可以算出每个特征平均减少了多少不纯度,并把它平均减少的不纯度作为特征选择的值。【1】
2.2 平均精确率减少

另一种常用的特征选择方法就是直接度量每个特征对模型精确率的影响。主要思路是打乱每个特征的特征值顺序,并且度量顺序变动对模型的精确率的影响。很明显,对于不重要的变量来说,打乱顺序对模型的精确率影响不会太大,但是对于重要的变量来说,打乱顺序就会降低模型的精确率。【1】

3、模型选择
一般先用随机森林试一下是不会有错的。

xgboost。

4、模型训练

我看比较推荐的是Grid Search,网格搜索。 毕竟相比处理过程和收益,这点代价可以忽略不计。费尽心机优化搜索方法最后得到的也不大。 根据推荐和实践,最后Random Forest 一般在 max_features 设为 Feature 数量的平方根附近得到最佳结果。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值