import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
'''
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import KFold
因为库更新后会抛出error
'''
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix,recall_score,classification_report
data = pd.read_csv("E:\Python program\逻辑回归3\creditcard.csv", engine='python')
# print(data.head())#打印查看数据形式
def test1():#画条形图查看0和1 的数据分布情况
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()
# test1()
#reshape函数可以重新调整矩阵的行数、列数、维数-1,1 意思:指定为1列,行数根据数据自动处理,生成矩阵
#fit_transform对数据进行变换
#将Amount这列数据作为一个列向量保存起来
'''
data['Amount'].reshape(-1, 1)会抛出error
Series数据类型没有reshape函数
解决办法:
用values方法将Series对象转化成numpy的ndarray,再用ndarray的reshape方法.
data[‘Amount’].values.reshape(-1, 1)
另外:pandas有两种对象:Series 和DataFrame。
可以这么理解,DataFrame像一张表,Series是指表里的某一行数据或某一列数据
'''
data['normAount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
#将原始数据中去除掉time和amount没用的整列数据,axis 1为列0为行
data = data.drop(['Time','Amount'],axis=1)
X = data.iloc[:, data.columns!='Class']
y = data.iloc[:, data.columns=='Class']
'''
下取样方式效果使得01样本同样少
下采样是有一种误杀率大的问题,即FP大问题,即查准率P低
'''
#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的索引取出来作为一个列向量保存
#Pincking the indices of the normal classes
normal_indices = data[data.Class == 0].index
#Out of the indices we picked, randomly select 'x' number (number_records_fraud)
#在normal_indices中进行随机选择number_records_fraud个数据
random_normal_indices = np.random.choice(normal_indices,number_records_fraud,replace = False)
random_normal_indices = np.array(random_normal_indices)#转化为列向量的格式
#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.iloc[:,under_sample_data.columns != 'Class']
y_undersample = under_sample_data.iloc[:,under_sample_data.columns == 'Class']
#showing ratio
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 resampledata: ",len(under_sample_data))
'''交叉验证'''
#Whole dadtaset
#random_state=0给定一个伪随机参数0来打乱数据,原始数据在抽取0.7部分作为训练集
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0)
print("Number transactions train dataset: ",len(X_train))
print("Number transactions test dataset: ",len(X_test))
print("Totle number of transactions: ",len(X_train)+len(X_test))
#Undersampled dataset
#下采样中抽取0.3作为测试集
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("")
print("Number transactions train dataset: ",len(X_train_undersample))
print("Number transactions test dataset: ",len(X_test_undersample))
print("Totle number of transactions: ",len(X_train_undersample)+len(X_test_undersample))
'''正则化惩罚,用于判断相同效果下,哪个模型参数更优稳定'''
'''
失败的版本1
def plot_confusion_matrix(cm, title='Confusion Matrix', cmap=plt.cm.binary):
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
xlocations = np.array(range(len(labels)))
plt.xticks(xlocations, labels, rotation=90)
plt.yticks(xlocations, labels)
plt.ylabel('True label')
plt.xlabel('Predicted label')
'''
import itertools
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
#传入原始训练集再进行切分
def printing_Kfold_scores(X_train_data,Y_train_data):
fold = KFold(5, shuffle=False)
#正则化中不同的惩罚项参数
c_param_range =[0.01,0.1,1,10,100]
results_table=pd.DataFrame(index=range(len(c_param_range)),columns=['C_parameter','Mean recall score'])
results_table['C_parameter'] = c_param_range
#the k-fold will gives 2 lists: train_indexes=indexes[0],test_indexes=indexes[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.split(X_train_data)):
# Call the logistic regression model with a certain C parameter
'''
缺少指定solver='liblinear'会抛出如下警告
FutureWarning: Default solver will be changed to 'lbfgs' in 0.22
然后参考了LogisticRegression对solver说明:
solver str , {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’, ‘saga’}默认:'liblinear'
求解优化问题使用的算法。
小数据集使用”liblinear“是个不错的选择,但是”sag“和”saga“对于较大的数据集速度更快
多分类问题只能使用”newton-cg“,"sag","saga"和”lbfgs“处理多分类损失,”liblinear“限于 one-versus-rest方案。
"newton-cg","lbfgs"和”seg"只支持 L2 正则,“liblinear"和”saga“只支持 L1正则。
"sag"和"saga"快速卷积只有在特征规模大致相同时才能保证快速收敛。 可以使用sklearn.preprocessing的缩放器对数据进行预处理。
'''
# Call the logistic regression model with a certain C parameter
lr = LogisticRegression(C=c_param, penalty='l1',solver='liblinear') # L1惩罚,L2惩罚
# Use the training data to fit the model. In this case, we use the portion of the fold to train the model
# with indices[0]. We then predict on the portion assigned as the 'test cross validation' with indices[1]
lr.fit(X_train_data.iloc[indices[0], :], Y_train_data.iloc[indices[0], :].values.ravel())
# Predict values using the test indices in the training data
Y_pred_undersample = lr.predict(X_train_data.iloc[indices[1], :].values)
#绘制confusion matrix
cnf_matrix = confusion_matrix( Y_train_data.iloc[indices[1], :].values,Y_pred_undersample)
np.set_printoptions(precision=2)
#显示出confusion matrix矩阵图
class_names = [0,1]
plt.figure()
MYtitle='Confusion matrix C_param_range='+str(c_param)
plot_confusion_matrix(cnf_matrix,classes=class_names,title=MYtitle)
plt.show()
# Calculate the recall score and append it to a list for recall scores representing the current 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('')
best_c=results_table.loc[results_table['Mean recall score'].astype('float64').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)
# print('')
# print("********原始数据直接测试*************")
# print('')
# #证明效果很不好
# best_c = printing_Kfold_scores(X_train,y_train)
def testfazhi():#设置阀值
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) # 原来时预测类别值,而此处是预测概率。方便后续比较
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)
plt.show()
testfazhi()