逻辑回归–信用卡欺诈检测
交叉验证 recall 正则化惩罚项 已经安排上了
代码和数据链接:https://pan.baidu.com/s/1E-n0iCNr4oFr4VxPPFrSBg 密码:fjvk
机器学习慢慢入门
1.看数据
#信用卡欺诈检测
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv("creditcard.csv")
print(data.head())
count_classes = pd.value_counts(data['Class'], sort=True).sort_index()
print(count_classes)
#value_counts统计Class列的属性值0,1各有多少个
count_classes.plot(kind='bar') #条形图,可以用pandas直接画
plt.title("Fraud class histogram")
plt.xlabel("Class")
plt.ylabel("Frequency")
plt.xticks(rotation=0)
plt.show()
0,1分度差异
按照常识,我们推断 Class列为0,表示正常的交易,为1,表示异常交易,可能存在信用卡欺诈,这也就是我们要找的一类。
看表发现:
1.1数据分布查异大
V1-V28列数值基本在-2到1之间,而Amount列中数据分布查异很大,从前5条数据看最小的2.69,最大的378.66。可能会对机器学习算法造成误导,认为数值大的特征更重要,数值小的特征不重要。为了使每个特征重要程度相当,用sklearn的preprocessing模块导进来StandardScaler标准化模块
fit_transform对数据进行变换
from sklearn.preprocessing import StandardScaler
data['normAmount'] = StandardScaler().fit_transform(data['Amount'].reshape(-1, 1)) #转换成新的特征
data = data.drop(['Time', 'Amount'], axis=1)#删除没用的特征
print(data.head())
打印前五条,已经变换好了
1.2 正常样本和异常样本,样本极度不均衡
样本不均衡解决方案:
过采样:
对少的那个进行生成,使样本一样多下采样:
从多的里面找与少的数量一样的样本,再组合起来就ok了。使样本一样少
2 下采样策略
2.1 获取下采样数据集
X = data.ix[:, data.columns != 'Class'] #拿出所有样本 ,不包括Class列
y = data.ix[:, data.columns == 'Class'] ##拿出所有样本 ,只拿出Class列,我们的label列
# Number of data points in the minority class
number_records_fraud = len(data[data.Class == 1]) #class=1的有多少个
fraud_indices = np.array(data[data.Class == 1].index) #把class=1的样本的索引拿出来
#print(fraud_indices)
# Picking the indices of the normal classes
normal_indices = data[data.Class == 0].index #把class=0的样本的索引拿出来
# Out of the indices we picked, randomly select "x" number (number_records_fraud) replace是否进行代替
random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace=False)
random_normal_indices = np.array(random_normal_indices)
#拿出索引后再转换为np.array格式
# Appending the 2 indices 合并索引
under_sample_indices = np.concatenate([fraud_indices, random_normal_indices])
# Under sample dataset 现在合并之后的数据
under_sample_data = data.iloc[under_sample_indices, :]
X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'Class']
y_undersample