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()
count_classes.plot(kind='bar')
plt.title('Fraud Class histogram')
plt.xlabel('Class')
plt.ylabel("Frequency")
plt.show()
样本不均衡
样本数据不均衡的情况时 采用 下采样 和 过采样
下采样 :让0和1数据一样小,样本同样少 过采样: 样本同样多
from sklearn.preprocessing import StandardScaler
data['normAmount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
data = data.drop(['Time', 'Amount'], axis=1)
print(data.head())
下采样:
# 下采样
X = data.ix[:, data.columns !='Class']
y = data.ix[:, data.columns =='Class']
number_records_fraud = len(data[data.Class == 1])
fraud_indeices = 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_indeices,random_normal_indices])
under_sample_data = data.iloc[under_sample_indices,:]
X_undersample = under_sample_data.ix[:,under_sample_data.columns !='Class']
X_undersample = under_sample_data.ix[:,under_sample_data.columns =='Class']
print('Percentage of nomal transaction:,', len(under_sample_data[under_sample_data.Class == 0])/len(under_sample_data))
print('Percentage of Fraud transaction:,', len(under_sample_data[under_sample_data.Class == 1])/len(under_sample_data))
print('reasmpled data 总的 transactions:', len(under_sample_data))
交叉验证
#交叉验证
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state = 0)
X_train_undersample, X_test_undersample, y_train_undersample,y_test_undersample =train_test_split(X_undersample,y_undersample,test_size,random_state)
print('')
print('Number transact train dataset: ', len(X_train))
print('Number transact test dataset: ', len(X_test))
print('Total number of transaction: ', len(X_train_undersample)+len(X_test_undersample))