信用卡欺诈案例
数据集下载地址
https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv
参考不平衡数据的分类
文章目录
- 只进行特征衍生,未进行数据标准化、上才样处理数据不平衡问题,得到的准确率和召回率居然很高
- 如果不处理数据不平衡问题,进行过采样,召回率只有0.64了结果如下
- 使用上采样处理数据不平衡问题,数据标准化处理,得到的准确率反而没那么高了
- 交叉验证,接着以上代码
- 测评数据的评估
- 混淆矩阵和召回率
只进行特征衍生,未进行数据标准化、上才样处理数据不平衡问题,得到的准确率和召回率居然很高
import pandas as pd
pd.set_option('display.float_format',lambda x: '%.2f' %x)
data = pd.read_csv('C:/Users/Administrator/Downloads/creditcard.csv')
data.head()
如果不处理数据不平衡问题,进行过采样,召回率只有0.64了结果如下
import pandas as pd
pd.set_option('display.float_format',lambda x: '%.2f' %x)
data = pd.read_csv('C:/Users/Administrator/Downloads/creditcard.csv')
data['Hour'] = data['Time'].apply(lambda x : divmod(x,3600)[0])
feature = list(data.columns)
feature.remove('Class')
feature.remove('Time')
X = data[feature]
y = data['Class']
display(X.head(),y.head())
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X,y)
y_ = model.predict(X)
from sklearn.metrics import auc,roc_auc_score,roc_curve,recall_score,accuracy_score,classification_report,confusion_matrix
print('LogisticRegression准确率是:',accuracy_score(y,y_))
cm = confusion_matrix(y,y_)
recall = cm[1,1]/(cm[1,1]+cm[1,0])
print('LogisticRegression召回率率是:',recall)
proba_ = model.predict_proba(X)[:,1] #表示获取类别1的样本阳性,行用卡盗刷
fpr,tpr,thresholds = roc_curve(y,proba_)
roc_auc = auc(fpr,tpr) #曲线下的面积
import matplotlib.pyplot as plt
plt.title=('Receiver Operating Characteristic')
plt.plot(fpr,tpr,'b',label='AUC = %0.5f'% roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.0])
plt.ylim([-0.1,1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
使用上采样处理数据不平衡问题,数据标准化处理,得到的准确率反而没那么高了
import pandas as pd
pd.set_option('display.float_format',lambda x: '%.2f' %x)
data = pd.read_csv('C:/Users/Administrator/Downloads/creditcard.csv')
data['Hour'] = data['Time'].apply(lambda x : divmod(x,3600)[0]) #特征衍生
#标准化
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
col = ['Amount','Hour']
data[col] = sc.fit_transform(data[col])
feature = list(data.columns)
feature.remove('Class') #剔除标签列
feature.remove('Time') #特征清洗
X = data[feature] #特征列
y = data['Class'] #目标值列
display(X.head(),y.head())
#过采样或者称上才采样,使用 最近邻插值(Nearest Neighbor Interpolation):直接使用最接近的像素值作为新的像素值
from imblearn.over_sampling import SMOTE
smote = SMOTE()
X,y = smote.fit_resample(X,y)
y.value_counts()
#模型训练
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X,y)
y_ = model.predict(X)
from sklearn.metrics import auc,roc_auc_score,roc_curve,recall_score,accuracy_score,classification_report,confusion_matrix
print('LogisticRegression准确率是:',accuracy_score(y,y_))
cm = confusion_matrix(y,y_) #获得混淆矩阵
recall = cm[1,1]/(cm[1,1]+cm[1,0])
print('LogisticRegression召回率率是:',recall)
proba_ = model.predict_proba(X)[:,1] #表示获取类别1的样本阳性,行用卡盗刷
fpr,tpr,thresholds = roc_curve(y,proba_)
roc_auc = auc(fpr,tpr) #曲线下的面积
import matplotlib.pyplot as plt
plt.title=('Receiver Operating Characteristic')
plt.plot(fpr,tpr,'b',label='AUC = %0.5f'% roc_auc)
plt.legend(loc='lower right')
plt.plot([0,1],[0,1],'r--')
plt.xlim([-0.1,1.0])
plt.ylim([-0.1,1.0])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
交叉验证,接着以上代码
%%time
from sklearn.model_selection import GridSearchCV, train_test_split
#交叉验证, 筛选合适的参数
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.2)
#构建参数组合
param_grid = {'C': [0.01,0.1,1,10,100,1000,], 'penalty': ['l2']}
grid_search = GridSearchCV(LogisticRegression(),param_grid,cv=10)
grid_search.fit(X_train,y_train) #使用训练集学习散发
报以下错误
将l1去掉,重跑一次,运行无报错,结果如下
#查看最佳参数
results = pd.DataFrame(grid_search.cv_results_)
display(results)
print("Best parammeters:{}".format(grid_search.best_params_))
print("Best cross-validation score: {:.5f}".format(grid_search.best_score_))
测评数据的评估
混淆矩阵和召回率
def plot_confusion_matrix(cm,classes,title, cmap=plt.cm.Blues):
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
cnf_matrix = confusion_matrix(y_test,y_pred) #获得混淆矩阵
recall1 = cnf_matrix[1,1]/(cnf_matrix[1,1]+cnf_matrix[1,0])
print('LogisticRegression召回率率是:',recall1)
#绘制模型优化后的混淆矩阵
class_names=[0,1]
plt.figure()
plot_confusion_matrix(cnf_matrix
, classes=class_names
, title = 'Confusion matrix'
)