初衷是因为引用卡欺诈问题相对与其他机器学习问题略有不同,因为二分类数据量差距过大,导致以往的评价方法对其不适用,如下图的284807 笔交易中只有492笔是欺诈行为,如果用以为的准确率评价几乎都在99%以上,但是这并不能说明模型好,因为即使漏掉1个欺诈交易都是损失很大的,所以这篇里引入了召回率和精确率,进行综合评价,详细步骤如下:
model = svm.SVC(kernel=‘rbf’, C=1.0, gamma=‘auto’)
kernel代表核函数的选择,它有四种选择,只不过默认是 rbf,即高斯核函数。linear:线性核函数poly:多项式核函数rbf:高斯核函数(默认)sigmoid:sigmoid 核函数这四种函数代表不同的映射方式。
此外引用一位知乎题主的回答,我觉得比较实用:
作者:Jason Gu
链接:
- Linear核:主要用于线性可分的情形。参数少,速度快,对于一般数据,分类效果已经很理想了。
- RBF核:主要用于线性不可分的情形。参数多,分类结果非常依赖于参数。有很多人是通过训练数据的交叉验证来寻找合适的参数,不过这个过程比较耗时。我个人的体会是:使用libsvm,默认参数,RBF核比Linear核效果稍差。通过进行大量参数的尝试,一般能找到比linear核更好的效果。至于到底该采用哪种核,要根据具体问题,有的数据是线性可分的,有的不可分,需要多尝试不同核不同参数。如果特征的提取的好,包含的信息量足够大,很多问题都是线性可分的。当然,如果有足够的时间去寻找RBF核参数,应该能达到更好的效果。
此外,sigmoid一般用于处理神经网络,pold虽然可以处理非线性问题,但是训练时间比较长。
数据源
链接: 提取码:58gp
说明:Class=0 为正常(非欺诈),Class=1 代表欺诈。
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
del_selection import train_test_split
from sklearn import svm
ics import confusion_matrix, precision_recall_curve
import warnings
import itertools
warnings.filterwarnings('ignore')# 混淆矩阵可视化
def plot_confusion_matrix(cm, classes, normalize = False, title = 'Confusion matrix"', cmap = Blues) :plt.figure()plt.imshow(cm, interpolation = 'nearest', cmap = cmap)plt.title(lorbar()tick_marks = np.arange(len(classes))icks(tick_marks, classes, rotation = icks(tick_marks, classes)thresh = cm.max() / 2.for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])) :(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')plt.show()# 显示模型评估结果
def show_metrics():tp = cm[1,1]fn = cm[1,0]fp = cm[0,1]tn = cm[0,0]print('精确率: {:.3f}'.format(tp/(tp+fp)))print('召回率: {:.3f}'.format(tp/(tp+fn)))print('F1值: {:.3f}'.format(2*(((tp/(tp+fp))*(tp/(tp+fn)))/((tp/(tp+fp))+(tp/(tp+fn))))))# 绘制精确率-召回率曲线
def plot_precision_recall():plt.step(recall, precision, color = 'b', alpha = 0.2, where = 'post')plt.fill_between(recall, precision, step ='post', alpha = 0.2, color = 'b')plt.plot(recall, precision, linewidth=2)plt.xlim([0.0,1])plt.ylim([0.0,1.05])plt.xlabel('召回率')plt.ylabel('精确率')plt.title('精确率-召回率 曲线')plt.show();data = pd.read_csv('./creditcard.csv')
data['Amount_Norm'] = StandardScaler().fit_transform(data['Amount'].shape(-1,1))
y = np.array(list())
data = data.drop(['Time','Amount','Class'],axis=1)
X = np.array(data.as_matrix())#DataFrame的形式,这个时候要记得转换成数组
train_x, test_x, train_y, test_y = train_test_split (X, y, test_size = 0.1, random_state = 33)
#创建SVM模型
model = svm.LinearSVC()
model.fit(train_x,train_y)
prediction=model.predict(test_x)
cm = confusion_matrix(test_y, prediction)
show_metrics()
# 设置plt正确显示中文
Params['font.sans-serif'] = ['SimHei']
# 绘制类别分布class_names = [0,1]
# 显示混淆矩阵
plot_confusion_matrix(cm, classes = class_names, title = '逻辑回归 混淆矩阵')score_y = model.decision_function(test_x)
precision, recall, thresholds = precision_recall_curve(test_y, score_y)
plot_precision_recall()
结果:
精确率: 0.843
召回率: 0.717
F1值: 0.775
信用卡欺诈问题总体来说是一个二分类问题,其特点在于类别数据比差距过大,评价指标不能仅使用metrics.accuracy_score(prediction,test_y))。建模实现分类的方法有很多,以及各个模型下还有参数调整情况,可以尝试使用弱分类器组成Adaboost、还有将随机森林与GridSearchCV结合找出最优解,可以下次写一下实现方法,逻辑回归也是一个比较实用的方法但是经测试训练时间较快,但是效果略差于SVM。
本文发布于:2024-02-01 21:27:12,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170679403239521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |