import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data = pd.read_csv("creditcard.csv")
data.head()
count_classes = pd.value_counts(data['Class'], sort = True).sort_index()
count_classes.plot(kind = 'bar')
plt.title("Fraud class histogram")
plt.xlabel("Class")
plt.ylabel("Frequency")
通过柱状图可以发现数据集中,异常值远远少于正常值,通常有两种方式解决:下采样,过采样.
from sklearn.preprocessing import StandardScaler
data['normAmount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
data = data.drop(['Time','Amount'],axis=1)
data.head()
X = data.ix[:, data.columns != 'Class']
y = data.ix[:, data.columns == 'Class']
# 获取异常数据的数量和异常数据的索引转换为numpy数组
number_records_fraud = len(data[data.Class == 1])
fraud_indices = np.array(data[data.Class == 1].index)
# 获取正常数据的索引
normal_indices = data[data.Class == 0].index
# 这里是采用的下采样,从正常数据中随机选出异常数据量大小的正常数据量的索引
random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace = False)
random_normal_indices = np.array(random_normal_indices)
# 将异常数据索引和选出的正常数据的索引拼接起来
under_sample_indices = np.concatenate([fraud_indices,random_normal_indices])
# 获取采样完后的数据,包含特征值x和标签y
under_sample_data = data.iloc[under_sample_indices,:]
X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'Class']
y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'Class']
# 展示数据集数据分布
print("Percentage of normal transactions: ", len(under_sample_data[under_sample_data.Class == 0])/len(under_sample_data))
print("Percentage of fraud transactions: ", len(under_sample_data[under_sample_data.Class == 1])/len(under_sample_data))
print("Total number of transactions in resampled data: ", len(under_sample_data))
#数据集切割工具包
from sklearn.model_selection import train_test_split
# 原始数据切割,测试集和训练集比例为3:7
X_train, X_test, y_train, y_t