python实现信用卡欺诈检测 logistic回归逻辑回归算法

1.数据集下载 :链接: https://pan.baidu.com/s/1zUxSxwiProvfmAAWjyYb4w 密码: 6eai

      代码下载 :链接: https://pan.baidu.com/s/1KyVOEU3p-sfCQIauCXGWIA 密码: tgrh

2.代码的实现:

#添加声明
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#读数据并显示前五行
data = pd.read_csv('creditcard.csv')
data.head()
#假设 class=0表示正常   class=1表示异常 用柱状图显示出样本的分布
count_classes = pd.value_counts(data['Class'], sort = True).sort_index()
count_classes.plot(kind = 'bar')
plt.title('Fraud class histofram')
plt.xlabel('Class')
plt.ylabel('Frequence')
plt.show()
from sklearn.preprocessing import StandardScaler   #里面的数据进行操作对Amount的数值进行操作得到normAmount   删除Amount和Time列。由于Amount的数值比较大,对其标准化操作一下。
#reshape中的-1表示  我的数据是1列 多少行你程序自己看着办。
data['normAmount'] = StandardScaler().fit_transform(data['Amount'].reshape(-1,1))
data = data.drop(['Time','Amount'],axis=1)
data.head()
#下采样,0和1的样本数据数量一样少
#本数据集中class=1的样本很少,我们取0的样本数和1的样本数一样多。 组成一个下采样集。
X = data.ix[:,data.columns !='Class']    #除了Class列的值 所有列的值都输入进去
y= data.ix[:,data.columns =='Class']      

print(len(y))
print(len(X))


number_records_fraud = len(data[data.Class==1])   #取calss=1的数量

fraud_indices = np.array(data[data.Class==1].index) #将class=1的索引存储到fraud_indices

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])

under_sample_data = data.iloc[under_sample_indices,:]
#下采样数据集中  X_undersample 和y_undersample标签
X_undersample = under_sample_data.ix[:,under_sample_data.columns!='Class']
y_undersample = under_sample_data.ix[:,under_sample_data.columns=='Class']

print(len(under_sample_data[under_sample_data.Class==1])/len(under_sample_data),len(under_sample_data[under_sample_data.Class==1]))
print(len(under_sample_data[under_sample_data.Class==0])/len(under_sample_data),len(under_sample_data[under_sample_data.Class==0]))

print(len(under_sample_data))
#交叉验证    数据切分成训练集和测试集  假设训练集平均分三份 1,2训练  3来验证 |  1,3训练 2验证 |  2,3训练 1验证
from sklearn.cross_validation import train_test_split

#所有数据集切分  7成的训练 3成的测试
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.3, random_state = 0)

print(len(X_train))
print(len(X_test))
print(len(y_train))
print(len(y_test))

#y_undersample  下采样数据集切分
X_train_undersample,X_test_undersample,y_train_undersample,y_test_undersample = train_test_split(X_undersample,y_undersample,test_size = 0.3, random_state = 0)
print(len(X_train_undersample))
print(len(X_test_undersample))
print(len(y_train_undersample))
print(len(y_test_undersample))
#模型建立
#recall召回率 作为模型评估标准   Recall = TP/(FP+TP)    

from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import KFold,cross_val_score   #KFold 几倍的交叉验证
from sklearn.metrics import confusion_matrix,recall_score,classification_report
def printing_Kfold_scores(x_train_data,y_train_data):
    
    fold = KFold(len(y_train_data),5,shuffle=False) #将训练集分成5分  交叉验证

    # 惩罚项的惩罚力度
    c_param_range = [0.01,0.1,1,10,100]

    results_table = pd.DataFrame(index = range(len(c_param_range),2), columns = ['C_parameter','Mean recall score'])
    results_table['C_parameter'] = c_param_range

    # the k-fold will give 2 lists: train_indices = indices[0], test_indices = indices[1]
    j = 0
    for c_param in c_param_range:
        print('-------------------------------------------')
        print('C parameter: ', c_param)
        print('-------------------------------------------')
        print('')

        recall_accs = []
        for iteration, indices in enumerate(fold,start=1):

            #   L1正则惩罚  + 惩罚发力度
            lr = LogisticRegression(C = c_param, penalty = 'l1')

           
            #用训练数据中的训练数据去 训练模型。
            lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())

            # 用训练数据里面的  验证数据来验证
            y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values)

            # 计算召回率
            recall_acc = recall_score(y_train_data.iloc[indices[1],:].values,y_pred_undersample)
            recall_accs.append(recall_acc)
            print('Iteration ', iteration,': recall score = ', recall_acc)

        # 求平均召回率
        results_table.ix[j,'Mean recall score'] = np.mean(recall_accs)
        j += 1
        print('')
        print('Mean recall score ', np.mean(recall_accs))
        print('')

    best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter']
    
    # Finally, we can check which C parameter is the best amongst the chosen.
    print('*********************************************************************************')
    print('Best model to choose from cross validation is with C parameter = ', best_c)
    print('*********************************************************************************')
    
    return best_c
best_c = printing_Kfold_scores(X_train_undersample,y_train_undersample)     #用下采样样本训练模型
#混淆矩阵的生成。
def plot_confusion_matrix(cm, classes,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=0)
    plt.yticks(tick_marks, classes)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
import itertools   #用测试数据上面跑的结果。
lr = LogisticRegression(C = best_c, penalty = 'l1')
lr.fit(X_train_undersample,y_train_undersample.values.ravel())
y_pred_undersample = lr.predict(X_test_undersample.values)

# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test_undersample,y_pred_undersample)
np.set_printoptions(precision=2)

print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))

# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
                      , classes=class_names
                      , title='Confusion matrix')
plt.show()
 
 
#图中可以看出来 召唤率为 136/(136+11) = 0.92517召唤率比较高  但是存在很高的误杀率:7263个样本。
#  采用L1正则惩罚  C表示惩罚的力度
lr = LogisticRegression(C = best_c, penalty = 'l1')    
lr.fit(X_train_undersample,y_train_undersample.values.ravel())
y_pred = lr.predict(X_test.values)

# 计算混淆矩阵
cnf_matrix = confusion_matrix(y_test,y_pred)
np.set_printoptions(precision=2)

print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))

# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
                      , classes=class_names
                      , title='Confusion matrix')
plt.show()


best_c = printing_Kfold_scores(X_train,y_train)    #用所有数据训练模型
#误杀率比较低只有  12的样本误杀,但是 召唤率低。
lr = LogisticRegression(C = best_c, penalty = 'l1')
lr.fit(X_train,y_train.values.ravel())
y_pred_undersample = lr.predict(X_test.values)

# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test,y_pred_undersample)
np.set_printoptions(precision=2)

print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))

# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
                      , classes=class_names
                      , title='Confusion matrix')
plt.show()
#采用不同的阈值
lr = LogisticRegression(C = 0.01, penalty = 'l1')
lr.fit(X_train_undersample,y_train_undersample.values.ravel())

y_pred_undersample_proba = lr.predict_proba(X_test_undersample.values)   #设置不同的阈值的测试结果

thresholds = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]

plt.figure(figsize=(10,10))

#当阈值为0.5和0.6的时候整体结果是比较好的。当阈值为0.1,0.2,0.3的时候召唤率是100%但是误杀率也是100%  当阈值是0.8,0.9的时候召唤率低但是误杀率也低。
j = 1
for i in thresholds:    
    y_test_predictions_high_recall = y_pred_undersample_proba[:,1] > i
    
    plt.subplot(3,3,j)
    j += 1
    
    # Compute confusion matrix
    cnf_matrix = confusion_matrix(y_test_undersample,y_test_predictions_high_recall)
    np.set_printoptions(precision=2)

    print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))

    # Plot non-normalized confusion matrix
    class_names = [0,1]
    plot_confusion_matrix(cnf_matrix
                          , classes=class_names
                          , title='Threshold >= %s'%i) 
plt.show()

#增加负样本数量   像本次的测试数据一样   负样本太少,导致训练的不是很理想。我们要自动生成一些负样本。
import pandas as pd
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split

#读取样本数据
credit_cards=pd.read_csv('creditcard.csv')

columns=credit_cards.columns
# The labels are in the last column ('Class'). Simply remove it to obtain features columns
features_columns=columns.delete(len(columns)-1)

features=credit_cards[features_columns]
labels=credit_cards['Class']

features_train, features_test, labels_train, labels_test = train_test_split(features, 
                                                                            labels, 
                                                                            test_size=0.2, 
                                                                            random_state=0)

#用SMOTE生成负样本,数量和正样本差不多。
oversampler=SMOTE(random_state=0)
os_features,os_labels=oversampler.fit_sample(features_train,labels_train)
#生成的负样本的数量
len(os_labels[os_labels==1])
#生成负样本之后在进行训练。   得到的结果比之前要好很多
os_features = pd.DataFrame(os_features)
os_labels = pd.DataFrame(os_labels)
best_c = printing_Kfold_scores(os_features,os_labels)


lr = LogisticRegression(C = best_c, penalty = 'l1')
lr.fit(os_features,os_labels.values.ravel())
y_pred = lr.predict(features_test.values)

# Compute confusion matrix
cnf_matrix = confusion_matrix(labels_test,y_pred)
np.set_printoptions(precision=2)

print("Recall metric in the testing dataset: ", cnf_matrix[1,1]/(cnf_matrix[1,0]+cnf_matrix[1,1]))

# Plot non-normalized confusion matrix
class_names = [0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
                      , classes=class_names
                      , title='Confusion matrix')
plt.show()














猜你喜欢

转载自blog.csdn.net/donkey_1993/article/details/79898113