keras搭建基于自动编码器的异常检测技术进行欺诈识别(二)

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
简介: keras搭建基于自动编码器的异常检测技术进行欺诈识别(二)

自动编码器将我们的数据编码到一个子空间,并且在对数据进行归一化时将其解码为相应的特征。我们希望自动编码器能够学习到在归一化转换时的特征,并且在应用时这个输入和输出是类似的。而对于异常情况,由于它是欺诈数据,所以输入和输出将会明显不同。

这种方法的好处是它允许使用无监督的学习方式,毕竟在我们通常所使用的数据中,大部分的数据均为正常交易数据。并且数据的标签通常是难以获得的,而且在某些情况下完全没法使用,例如手动对数据进行标记往往存在人为认识偏差等问题。从而,在对模型进行训练的过程中,我们只使用没有标签的正常交易数据。

接下来,让我们下载数据并训练自动编码器:

df = pd.read_csv('creditcard.csv')
x = df[df.columns[1:30]].to_numpy()
y = df[df.columns[30]].to_numpy()
# prepare data
df = pd.concat([pd.DataFrame(x), pd.DataFrame({'anomaly': y})], axis=1)
normal_events = df[df['anomaly'] == 0]
abnormal_events = df[df['anomaly'] == 1]
normal_events = normal_events.loc[:, normal_events.columns != 'anomaly']
abnormal_events = abnormal_events.loc[:, abnormal_events.columns != 'anomaly']
# scaling
scaler = preprocessing.MinMaxScaler()
scaler.fit(df.drop('anomaly', 1))
scaled_data = scaler.transform(normal_events)
# 80% percent of dataset is designated to training
train_data, test_data = model_selection.train_test_split(scaled_data, test_size=0.2)
n_features = x.shape[1]
# model
encoder = models.Sequential(name='encoder')
encoder.add(layer=layers.Dense(units=20, activation=activations.relu, input_shape=[n_features]))
encoder.add(layers.Dropout(0.1))
encoder.add(layer=layers.Dense(units=10, activation=activations.relu))
encoder.add(layer=layers.Dense(units=5, activation=activations.relu))
decoder = models.Sequential(name='decoder')
decoder.add(layer=layers.Dense(units=10, activation=activations.relu, input_shape=[5]))
decoder.add(layer=layers.Dense(units=20, activation=activations.relu))
decoder.add(layers.Dropout(0.1))
decoder.add(layer=layers.Dense(units=n_features, activation=activations.sigmoid))
autoencoder = models.Sequential([encoder, decoder])
autoencoder.compile(
loss=losses.MSE,
optimizer=optimizers.Adam(),
metrics=[metrics.mean_squared_error])
# train model
es = EarlyStopping(monitor='val_loss', min_delta=0.00001, patience=20, restore_best_weights=True)
history = autoencoder.fit(x=train_data, y=train_data, epochs=100, verbose=1, validation_data=[test_data, test_data], callbacks=[es])
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()


观察下图可知,该模型的误差大约为8.5641e-04,而误差最小时约为5.4856e-04。

image.png

使用该模型,我们能够计算出正常交易时的均方根误差,并且还能知道当需要均方根误差值为95%时,阈值应该设置为多少。

train_predicted_x = autoencoder.predict(x=train_data)
train_events_mse = losses.mean_squared_error(train_data, train_predicted_x)
cut_off = np.percentile(train_events_mse, 95)

我们设置的阈值为0.002,如果均方根误差大于0.002时,我们就把这次的交易视为异常交易,即有欺诈行为出现。让我们选取100个欺诈数据和100个正常数据作为样本,结合阈值能够绘制如下图:

plot_samples = 100
# normal event
real_x = test_data[:plot_samples].reshape(plot_samples, n_features)
predicted_x = autoencoder.predict(x=real_x)
normal_events_mse = losses.mean_squared_error(real_x, predicted_x)
normal_events_df = pd.DataFrame({
'mse': normal_events_mse,
'n': np.arange(0, plot_samples),
'anomaly': np.zeros(plot_samples)})
# abnormal event
abnormal_x = scaler.transform(abnormal_events)[:plot_samples].reshape(plot_samples, n_features)
predicted_x = autoencoder.predict(x=abnormal_x)
abnormal_events_mse = losses.mean_squared_error(abnormal_x, predicted_x)
abnormal_events_df = pd.DataFrame({
'mse': abnormal_events_mse,
'n': np.arange(0, plot_samples),
'anomaly': np.ones(plot_samples)})
mse_df = pd.concat([normal_events_df, abnormal_events_df])
plot = sns.lineplot(x=mse_df.n, y=mse_df.mse, hue=mse_df.anomaly)
line = lines.Line2D(
xdata=np.arange(0, plot_samples),
ydata=np.full(plot_samples, cut_off),
color='#CC2B5E',
linewidth=1.5,
linestyle='dashed')
plot.add_artist(line)
plt.title('Threshlold: {threshold}'.format(threshold=cut_off))
plt.show()

image.png

由上图可知,与正常交易数据相比,绝大部分欺诈数据均有较高的均方根误差,从而这个方法对欺诈数据的识别似乎非常奏效。

虽然我们放弃了5%的正常交易,但仍然存在低于阈值的欺诈交易。这或许可以通过使用更好的特征提取方法来进行改进,因为一些欺诈数据与正常交易数据具有非常相似的特征。例如,对于信用卡欺诈而言,如果交易是在不同国家发生的,那么比较有价值的特征是:前一小时、前一天、前一周的交易数量。

下一步的工作

1. 对超参数进行优化。

2. 使用一些数据分析方法来更好的理解数据的特征。

3.将上述方法与其他机器学习的方法相比较,例如:支持向量机或k-means聚类等等。

本文的完整代码均能在Github上进行获取。

https://github.com/bgokden/anomaly-detection-with-autoencoders


引用文献

1. Andrea Dal Pozzolo, Olivier Caelen, Reid A. Johnson and GianlucaBontempi. Calibrating Probability with Undersampling for UnbalancedClassification. In Symposium on Computational Intelligence and DataMining (CIDM), IEEE, 2015

2. Dal Pozzolo, Andrea; Caelen, Olivier; Le Borgne, Yann-Ael;Waterschoot, Serge; Bontempi, Gianluca. Learned lessons in credit cardfraud detection from a practitioner perspective, Expert systems withapplications,41,10,4915--4928,2014, Pergamon

3. Dal Pozzolo, Andrea; Boracchi, Giacomo; Caelen, Olivier; Alippi,Cesare; Bontempi, Gianluca. Credit card fraud detection: a realisticmodeling and a novel learning strategy, IEEE transactions on neuralnetworks and learning systems,29,8,3784--3797,2018,IEEE

4. Dal Pozzolo, Andrea Adaptive Machine learning for credit card frauddetection ULB MLG PhD thesis (supervised by G. Bontempi)

5. Carcillo, Fabrizio; Dal Pozzolo, Andrea; Le Borgne, Yann-Aël;Caelen, Olivier; Mazzer, Yannis; Bontempi, Gianluca. Scarff: a scalableframework for streaming credit card fraud detection with Spark,Information fusion,41, 182--194,2018,Elsevier

6. Carcillo, Fabrizio; Le Borgne, Yann-Aël; Caelen, Olivier; Bontempi,Gianluca. Streaming active learning strategies for real-life credit cardfraud detection: assessment and visualization, International Journal ofData Science and Analytics, 5,4,285--300,2018,Springer InternationalPublishing

7.Bertrand Lebichot, Yann-Aël Le Borgne, Liyun He, Frederic Oblé,Gianluca Bontempi Deep-Learning Domain Adaptation Techniques for CreditCards Fraud Detection, INNSBDDL 2019: Recent Advances in Big Data andDeep Learning, pp 78--88, 2019

8. Fabrizio Carcillo, Yann-Aël Le Borgne, Olivier Caelen, FredericOblé, Gianluca Bontempi Combining Unsupervised and Supervised Learningin Credit Card Fraud Detection Information Sciences, 2019

相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
目录
相关文章
|
5月前
|
机器学习/深度学习 人工智能 安全
论文介绍:从黑盒生产语言模型中提取信息的模型窃取攻击
【2月更文挑战第22天】论文介绍:从黑盒生产语言模型中提取信息的模型窃取攻击
110 6
论文介绍:从黑盒生产语言模型中提取信息的模型窃取攻击
|
5月前
|
机器学习/深度学习 存储 监控
数据分享|Python卷积神经网络CNN身份识别图像处理在疫情防控下口罩识别、人脸识别
数据分享|Python卷积神经网络CNN身份识别图像处理在疫情防控下口罩识别、人脸识别
|
9天前
|
机器学习/深度学习 TensorFlow 算法框架/工具
使用Python实现深度学习模型:智能质量检测与控制
使用Python实现深度学习模型:智能质量检测与控制 【10月更文挑战第8天】
102 62
使用Python实现深度学习模型:智能质量检测与控制
|
2月前
|
机器学习/深度学习 人工智能 大数据
基于联邦学习的数据隐私保护机制在智能模型训练中的应用
【8月更文第15天】随着大数据和人工智能的发展,数据隐私保护成为了亟待解决的问题。传统的集中式机器学习方法需要将数据收集到一个中心服务器进行处理,这不仅增加了数据泄露的风险,还可能触犯相关的法律法规。联邦学习(Federated Learning, FL)作为一种新兴的分布式机器学习框架,允许终端设备直接在本地数据上训练模型,并仅将更新后的模型参数发送给中心服务器汇总,从而在不暴露原始数据的情况下实现模型训练。
104 0
|
6天前
|
机器学习/深度学习 SQL 数据采集
基于tensorflow、CNN网络识别花卉的种类(图像识别)
基于tensorflow、CNN网络识别花卉的种类(图像识别)
11 1
|
1天前
|
数据采集 机器学习/深度学习 TensorFlow
声纹识别实战:从数据采集到模型训练
【10月更文挑战第16天】声纹识别技术通过分析个人的语音特征来验证其身份,具有无接触、便捷的特点。本文将带你从零开始,一步步完成声纹识别系统的构建,包括数据采集、音频预处理、特征提取、模型训练及评估等关键步骤。我们将使用Python语言和相关的科学计算库来进行实践。
4 0
|
29天前
|
机器学习/深度学习 数据采集 网络安全
使用Python实现深度学习模型:智能网络安全威胁检测
使用Python实现深度学习模型:智能网络安全威胁检测
108 5
|
1月前
|
机器学习/深度学习 数据采集 存储
使用Python实现深度学习模型:智能保险风险评估
使用Python实现深度学习模型:智能保险风险评估
73 12
|
机器学习/深度学习 算法 数据挖掘
ML |机器学习模型如何检测和预防过拟合?
ML |机器学习模型如何检测和预防过拟合?
169 0
|
5月前
|
机器学习/深度学习 人工智能 测试技术
基于keras平台CNN神经网络模型的服装识别分析
基于keras平台CNN神经网络模型的服装识别分析