逻辑回归之信用评估实例

进行数据展示

第一步我们先导入数据,查看一下要区分的类别得类别数,以及每个类别得样本数,并进行展示。信用贷款得CLASS只有两类,0和1,两者拥有的数据量差异极大,面临数据不平衡问题

import pandas as pd
import numpy as np
import matplotlib.pyplot  as plt
import os 
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression#引入逻辑回归分类器
from sklearn.model_selection import KFold, cross_val_score#引入K折交差验证
from sklearn.metrics import confusion_matrix,recall_score,classification_report #引入混淆矩阵,召回率,以及分类报告
import itertools
from imblearn.over_sampling  import SMOTE

if __name__=="__main__":
    
    path="D:\Python base\Test\逻辑回归-信用卡欺诈检测"+os.sep +"creditcard.csv"
    data= pd.read_csv(path)

   if __name__=="__main__":
    
    path="D:\Python base\Test\逻辑回归-信用卡欺诈检测"+os.sep +"creditcard.csv"
    data= pd.read_csv(path)

    #1.1进行数据展示
    class_number=data.loc[:,'Classnew'].value_counts()#统计某一列各个数值得出现次数
    count_classes = pd.value_counts(data['Classnew'], sort = True).sort_index()#统计某一列各个数值得出现次数
    class_number.plot(kind = 'bar')#kind可以是’line’, ‘bar’, ‘barh’, ‘kde’
    plt.title("Fraud class histogram")
    plt.xlabel("Class")
    plt.ylabel("Frequency")
    plt.show()

在这里插入图片描述

进行归一化和下采样

 #对某列数据进行归一化
    data['normAmount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))#对某列数据进行归一化
    data = data.drop(['Time','Amount'],axis=1)#删除没用的两列
    X=data.loc[:,data.columns != "Classnew"]
    y=data.loc[:,data.columns == "Classnew"]
    #1.2进行下采样
    class1=data[data.Classnew == 1] #利用条件充当Index挑选class=1的数据
    print("class=1的数据为:",class1)
    #class0=data[data["Classnew"] == 0] #利用
    class1_number=len(class1)#统计class为1数据有多少
    class1_index=np.array(data[data.Classnew == 1].index)
    class0_index=np.array(data[data.Classnew == 0].index)
    class0_indices = np.random.choice(class0_index, class1_number, replace = False)#随机选取class1_number个为零的数值索引
    class0_indices = np.array(class0_indices)
    under_sample_indices = np.concatenate([class1_index,class0_indices])#下采样的所有数据的Index
    under_sample_data = data.iloc[under_sample_indices,:]#获取的下采样数据
    X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'Classnew']
    y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'Classnew']
    print("Percentage of normal transactions: ", len(under_sample_data[under_sample_data.Classnew  == 0])/len(under_sample_data))
    print("Percentage of fraud transactions: ", len(under_sample_data[under_sample_data.Classnew  == 1])/len(under_sample_data))
    print("Total number of transactions in resampled data: ", len(under_sample_data))

在这里插入图片描述

原始数据和下采样数据(训练集和测试集的划分)

#训练集和验证集划分
    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 = 0.3
                                                                                                   ,random_state = 0)

K折交叉验证(对划分的下采样训练集数据)

假如train分为10分,前9分用于训练,后一份用于测试
在这里插入图片描述

#K折交叉验证
def printing_Kfold_scores(x_train_data,y_train_data):
    #k折交叉验证
    fold = KFold(n_splits=5,shuffle=False)
    #不同的C参数
    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
    #k折操作将会给出两个列表: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 = []
        #enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
        for iteration,indices in enumerate(fold.split(x_train_data),start=1):
            #把c_param_range代入到逻辑回归模型中,并使用了l1正则化,C代表正则化系数
            #solver优化算法选择参数,{‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’
            lr = LogisticRegression(C = c_param,penalty = 'l1',solver='liblinear')
            #使用indices[0]的数据进行拟合曲线,使用indices[1]的数据进行误差测试
            lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())#模型训练
            #在indices[1]数据上预测值
            y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values)
            #根据不同的c_parameter计算召回率
            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.loc[j,'Mean recall score'] = np.mean(recall_accs)#计算召回率的平均值
        j += 1
        print('')
        print('Mean recall score ', np.mean(recall_accs))
        print('')
    print("results_table is:",results_table) #输出每个参数c所对应的 K折交叉验证的平均召回率  
    best_c = results_table.loc[results_table['Mean recall score'].values.argmax()]['C_parameter']#返回最大平均召回率所对应的参数C
    #最后选择最好的 C parameter
    print('*********************************************************************************')
    print('Best model to choose from cross validation is with C parameter = ', best_c)
    print('*********************************************************************************')
    return best_c

对于这个代码:

from sklearn.cross_validation import KFold

fold = KFold(50,5,shuffle=False)

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

    print(iteration, indices)

在这里插入图片描述

利用K折交叉验证返回的参数C进行模型的训练和预测

    best_c = printing_Kfold_scores(X_train_undersample,y_train_undersample)
    lr = LogisticRegression(C = best_c, penalty = 'l1',solver='liblinear')#创建模型
    lr.fit(X_train_undersample,y_train_undersample.values.ravel())#模型预训练
    y_pred_undersample = lr.predict(X_test_undersample.values)#模型预测

混淆矩阵(混淆矩阵的y轴代表真实值,X轴代表预测值)

在这里插入图片描述
TP代表真积极,T代表真,P代表正类;FP代表假积极,F代表假,P代表判为正类;FN代表假消极,F代表假,N代表负类;TN代表真负类,T代表判断正确,判为父类。召回率(recall)=TP/(TP+FN)

#混淆矩阵绘制
#cm为混淆矩阵,classes为混淆矩阵的类标    
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)#interpolation插值方式,#cmap表示绘图时的样式,cm是混淆矩阵
    plt.title(title)
    plt.colorbar()#给图配渐变色时,常常需要在图旁边把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')
    # Compute confusion matrix
    cnf_matrix = confusion_matrix(y_test_undersample,y_pred_undersample)#计算混淆矩阵
    np.set_printoptions(precision=2)#控制输出的小数点个数是2
    print("混淆矩阵为:",cnf_matrix)
    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',solver='liblinear')
    lr.fit(X_train_undersample,y_train_undersample.values.ravel())
    y_pred_undersample_proba = lr.predict_proba(X_test_undersample.values)#预测y值的概率
   
    thresholds = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]

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

    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) 

在这里插入图片描述

标题

import pandas as pd
import numpy as np
import matplotlib.pyplot  as plt
import os 
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression#引入逻辑回归分类器
from sklearn.model_selection import KFold, cross_val_score#引入K折交差验证
from sklearn.metrics import confusion_matrix,recall_score,classification_report #引入混淆矩阵,召回率,以及分类报告
import itertools
from imblearn.over_sampling  import SMOTE
#过采样
    features_train, features_test, labels_train, labels_test = train_test_split(X, 
                                                                            y, 
                                                                            test_size=0.2, 
                                                                            random_state=0)
    
    oversampler=SMOTE(random_state=0)
    os_features,os_labels=oversampler.fit_sample(features_train,labels_train)#对训练特征和标签数据过采样
    print(len(os_labels[os_labels==1]))
    print(len(os_labels[os_labels==0])
    os_features = pd.DataFrame(os_features)
    os_labels = pd.DataFrame(os_labels)
    best_c = printing_Kfold_scores(os_features,os_labels)

整个代码

# -*- coding: utf-8 -*-
"""
Created on Fri Feb 28 07:07:23 2020

@author: User
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot  as plt
import os 
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression#引入逻辑回归分类器
from sklearn.model_selection import KFold, cross_val_score#引入K折交差验证
from sklearn.metrics import confusion_matrix,recall_score,classification_report #引入混淆矩阵,召回率,以及分类报告
import itertools
from imblearn.over_sampling  import SMOTE

#K折交叉验证
def printing_Kfold_scores(x_train_data,y_train_data):
    #k折交叉验证
    fold = KFold(n_splits=5,shuffle=False)
    #不同的C参数
    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
    #k折操作将会给出两个列表: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 = []
        #enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
        for iteration,indices in enumerate(fold.split(x_train_data),start=1):
            #把c_param_range代入到逻辑回归模型中,并使用了l1正则化,C代表正则化系数
            #solver优化算法选择参数,{‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’
            lr = LogisticRegression(C = c_param,penalty = 'l1',solver='liblinear')
            #使用indices[0]的数据进行拟合曲线,使用indices[1]的数据进行误差测试
            lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())#模型训练
            #在indices[1]数据上预测值
            y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values)
            #根据不同的c_parameter计算召回率
            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.loc[j,'Mean recall score'] = np.mean(recall_accs)#计算召回率的平均值
        j += 1
        print('')
        print('Mean recall score ', np.mean(recall_accs))
        print('')
    print("results_table is:",results_table) #输出每个参数c所对应的 K折交叉验证的平均召回率  
    best_c = results_table.loc[results_table['Mean recall score'].values.argmax()]['C_parameter']#返回最大平均召回率所对应的参数C
    #最后选择最好的 C parameter
    print('*********************************************************************************')
    print('Best model to choose from cross validation is with C parameter = ', best_c)
    print('*********************************************************************************')
    return best_c


#混淆矩阵绘制
#cm为混淆矩阵,classes为混淆矩阵的类标    
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)#interpolation插值方式,#cmap表示绘图时的样式,cm是混淆矩阵
    plt.title(title)
    plt.colorbar()#给图配渐变色时,常常需要在图旁边把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')


if __name__=="__main__":
    
    path="D:\Python base\Test\逻辑回归-信用卡欺诈检测"+os.sep +"creditcard.csv"
    data= pd.read_csv(path)

    #1.1进行数据展示
    class_number=data.loc[:,'Classnew'].value_counts()#统计某一列各个数值得出现次数
    count_classes = pd.value_counts(data['Classnew'], sort = True).sort_index()#统计某一列各个数值得出现次数
    class_number.plot(kind = 'bar')#kind可以是’line’, ‘bar’, ‘barh’, ‘kde’
    plt.title("Fraud class histogram")
    plt.xlabel("Class")
    plt.ylabel("Frequency")
    plt.show()

    #对某列数据进行归一化
    data['normAmount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))#对某列数据进行归一化
    data = data.drop(['Time','Amount'],axis=1)#删除没用的两列
    X=data.loc[:,data.columns != "Classnew"]
    y=data.loc[:,data.columns == "Classnew"]
    #1.2进行下采样
    class1=data[data.Classnew == 1] #利用条件充当Index挑选class=1的数据
    print("class=1的数据为:",class1)
    #class0=data[data["Classnew"] == 0] #利用
    class1_number=len(class1)#统计class为1数据有多少
    class1_index=np.array(data[data.Classnew == 1].index)
    class0_index=np.array(data[data.Classnew == 0].index)
    class0_indices = np.random.choice(class0_index, class1_number, replace = False)#随机选取class1_number个为零的数值索引
    class0_indices = np.array(class0_indices)
    under_sample_indices = np.concatenate([class1_index,class0_indices])#下采样的所有数据的Index
    under_sample_data = data.iloc[under_sample_indices,:]#获取的下采样数据
    X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'Classnew']
    y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'Classnew']
    print("Percentage of normal transactions: ", len(under_sample_data[under_sample_data.Classnew  == 0])/len(under_sample_data))
    print("Percentage of fraud transactions: ", len(under_sample_data[under_sample_data.Classnew  == 1])/len(under_sample_data))
    print("Total number of transactions in resampled data: ", len(under_sample_data))
#
    #训练集和验证集划分
    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 = 0.3
                                                                                                   ,random_state = 0)

   
    
    
    features_train, features_test, labels_train, labels_test = train_test_split(X, 
                                                                            y, 
                                                                            test_size=0.2, 
                                                                            random_state=0)
    
    oversampler=SMOTE(random_state=0)
    os_features,os_labels=oversampler.fit_sample(features_train,labels_train)#对训练特征和标签数据过采样
    print(len(os_labels[os_labels==1]))
    print(len(os_labels[os_labels==0])
    os_features = pd.DataFrame(os_features)
    os_labels = pd.DataFrame(os_labels)
    best_c = printing_Kfold_scores(os_features,os_labels)


   
    
    best_c = printing_Kfold_scores(X_train_undersample,y_train_undersample)
    lr = LogisticRegression(C = best_c, penalty = 'l1',solver='liblinear')#创建模型
    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)#控制输出的小数点个数是2
    print("混淆矩阵为:",cnf_matrix)
    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',solver='liblinear')
    lr.fit(X_train_undersample,y_train_undersample.values.ravel())
    y_pred_undersample_proba = lr.predict_proba(X_test_undersample.values)#预测y值的概率
   
    thresholds = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]

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

    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) 

#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值