ML.NET 示例:二元分类之信用卡欺诈检测
写在前面
准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正。
如果有朋友对此感兴趣,可以加入我:https://github.com/feiyun0112/machinelearning-samples.zh-cn
基于二元分类和PCA的信用卡欺诈检测
| ML.NET 版本 | API 类型 | 状态 | 应用程序类型 | 数据类型 | 场景 | 机器学习任务 | 算法 |
|---|---|---|---|---|---|---|---|
| v0.7 | 动态API | 更新至0.7 | 两个控制台应用程序 | .csv 文件 | 欺诈检测 | 二元分类 | FastTree 二元分类 |
在这个介绍性示例中,您将看到如何使用ML.NET来预测信用卡欺诈。在机器学习领域中,这种类型的预测被称为二元分类。
API版本:基于动态和评估器的API
请务必注意,此示例使用动态API和评估器。
问题
这个问题的核心是预测信用卡交易(及其相关信息/变量)是否是欺诈。
交易的输入信息仅包含PCA转换后的数值输入变量。遗憾的是,基于隐私原因,原始特征和附加的背景信息无法得到,但您建立模型的方式不会改变。
特征V1, V2, ... V28是用PCA获得的主成分,未经PCA转换的特征是“Time”和“Amount”。
“Time”特征包含每个交易和数据集中的第一个交易之间经过的秒数。“Amount”特征是交易金额,该特征可用于依赖于示例的代价敏感学习。特征“Class”是响应变量,如果存在欺诈取值为1,否则为0。
数据集非常不平衡,正类(欺诈)数据占所有交易的0.172%。
使用这些数据集,您可以建立一个模型,当预测该模型时,它将分析交易的输入变量并预测欺诈值为false或true。
数据集
训练和测试数据基于公共数据集dataset available at Kaggle,其最初来自于Worldline和ULB(Université Libre de Bruxelles)的机器学习小组(ttp://mlg.ulb.ac.be)在研究合作期间收集和分析的数据集。
这些数据集包含2013年9月由欧洲持卡人通过信用卡进行的交易。 这个数据集显示了两天内发生的交易,在284,807笔交易中有492个欺诈。
作者:Andrea Dal Pozzolo、Olivier Caelen、Reid A. Johnson和Gianluca Bontempi。基于欠采样的不平衡分类概率。2015在计算智能和数据挖掘(CIDM)学术研讨会上的发言
有关相关主题的当前和过去项目的更多详细信息,请访问 http://mlg.ulb.ac.be/BruFence 和 http://mlg.ulb.ac.be/ARTML
机器学习任务 - 二元分类
二元或二项式分类是根据分类规则将给定集合中的元素分成两组(预测每个元素属于哪个组)的任务。需要决定某项是否具有某种定性属性、某些特定特征的上下文
解决方案
要解决这个问题,首先需要建立一个机器学习模型。 然后,您可以在现有训练数据上训练模型,评估其准确性有多好,最后使用该模型(在另一个应用程序中部署建立的模型)来预测信用卡交易样本是否存在欺诈。

1. 建立模型
建立一个模型包括:
定义映射到数据集的数据架构,以便使用DataReader读取
拆分训练和测试数据
创建一个评估器,并使用ConcatEstimator()转换数据,并通过均值方差进行标准化。
选择一个训练/学习算法(FastTree)来训练模型。
初始代码类似以下内容:
// Create a common ML.NET context.
// Seed set to any number so you have a deterministic environment for repeateable results
MLContext mlContext = new MLContext(seed:1);
[...]
TextLoader.Column[] columns = new[] {
// A boolean column depicting the 'label'.
new TextLoader.Column("Label", DataKind.BL, 30),
// 29 Features V1..V28 + Amount
new TextLoader.Column("V1", DataKind.R4, 1 ),
new TextLoader.Column("V2", DataKind.R4, 2 ),
new TextLoader.Column("V3", DataKind.R4, 3 ),
new TextLoader.Column("V4", DataKind.R4, 4 ),
new TextLoader.Column("V5", DataKind.R4, 5 ),
new TextLoader.Column("V6", DataKind.R4, 6 ),
new TextLoader.Column("V7", DataKind.R4, 7 ),
new TextLoader.Column("V8", DataKind.R4, 8 ),
new TextLoader.Column("V9", DataKind.R4, 9 ),
new TextLoader.Column("V10", DataKind.R4, 10 ),
new TextLoader.Column("V11", DataKind.R4, 11 ),
new TextLoader.Column("V12", DataKind.R4, 12 ),
new TextLoader.Column("V13", DataKind.R4, 13 ),
new TextLoader.Column("V14", DataKind.R4, 14 ),
new TextLoader.Column("V15", DataKind.R4, 15 ),
new TextLoader.Column("V16", DataKind.R4, 16 ),
new TextLoader.Column("V17", DataKind.R4, 17 ),
new TextLoader.Column("V18", DataKind.R4, 18 ),
new TextLoader.Column("V19", DataKind.R4, 19 ),
new TextLoader.Column("V20", DataKind.R4, 20 ),
new TextLoader.Column("V21", DataKind.R4, 21 ),
new TextLoader.Column("V22", DataKind.R4, 22 ),
new TextLoader.Column("V23", DataKind.R4, 23 ),
new TextLoader.Column("V24", DataKind.R4, 24 ),
new TextLoader.Column("V25", DataKind.R4, 25 ),
new TextLoader.Column("V26", DataKind.R4, 26 ),
new TextLoader.Column("V27", DataKind.R4, 27 ),
new TextLoader.Column("V28", DataKind.R4, 28 ),
new TextLoader.Column("Amount", DataKind.R4, 29 )
};
TextLoader.Arguments txtLoaderArgs = new TextLoader.Arguments
{
Column = columns,
// First line of the file is a header, not a data row.
HasHeader = true,
Separator = ","
};
[...]
var classification = new BinaryClassificationContext(env);
(trainData, testData) = classification.TrainTestSplit(data, testFraction: 0.2);
[...]
//Get all the column names for the Features (All except the Label and the StratificationColumn)
var featureColumnNames = _trainData.Schema.GetColumns()
.Select(tuple => tuple.column.Name) // Get the column names
.Where(name => name != "Label") // Do not include the Label column
.Where(name => name != "StratificationColumn") //Do not include the StratificationColumn
.ToArray();
var pipeline = _mlContext.Transforms.Concatenate("Features", featureColumnNames)
.Append(_mlContext.Transforms.Normalize(inputName: "Features", outputName: "FeaturesNormalizedByMeanVar", mode: NormalizerMode.MeanVariance))
.Append(_mlContext.BinaryClassification.Trainers.FastTree(labelColumn: "Label",
featureColumn: "Features",
numLeaves: 20,
numTrees: 100,
minDatapointsInLeaves: 10,
learningRate: 0.2));
2. 训练模型
训练模型是在训练数据(具有已知欺诈值)上运行所选算法以调整模型参数的过程。它是在评估器对象的 Fit() 方法中实现。
为了执行训练,您需要在DataView对象中提供了训练数据集(trainData.csv)后调用 Fit() 方法。
var model = pipeline.Fit(_trainData);
3. 评估模型
我们需要这一步骤来判定我们的模型对新数据的准确性。 为此,上一步中的模型再次针对另一个未在训练中使用的数据集(testData.csv)运行。
Evaluate()比较测试数据集的预测值,并生成各种指标,例如准确性,您可以对其进行浏览。
var metrics = _context.Evaluate(model.Transform(_testData), "Label");
4. 使用模型
训练完模型后,您可以使用Predict()API来预测交易是否存在欺诈。
[...]
ITransformer model;
using (var file = File.OpenRead(_modelfile))
{
model = mlContext.Model.Load(file);
}
var predictionFunc = model.MakePredictionFunction<TransactionObservation, TransactionFraudPrediction>(mlContext);
[...]
dataTest.AsEnumerable<TransactionObservation>(mlContext, reuseRowObject: false)
.Where(x => x.Label == true)
.Take(numberOfTransactions)
.Select(testData => testData)
.ToList()
.ForEach(testData =>
{
Console.WriteLine($"--- Transaction ---");
testData.PrintToConsole();
predictionFunc.Predict(testData).PrintToConsole();
Console.WriteLine($"-------------------");
});
[...]
dataTest.AsEnumerable<TransactionObservation>(mlContext, reuseRowObject: false)
.Where(x => x.Label == false)
.Take(numberOfTransactions)
.ToList()
.ForEach(testData =>
{
Console.WriteLine($"--- Transaction ---");
testData.PrintToConsole();
predictionFunc.Predict(testData).PrintToConsole();
Console.WriteLine($"-------------------");
});
ML.NET 示例:二元分类之信用卡欺诈检测的更多相关文章
- 机器学习_线性回归和逻辑回归_案例实战:Python实现逻辑回归与梯度下降策略_项目实战:使用逻辑回归判断信用卡欺诈检测
线性回归: 注:为偏置项,这一项的x的值假设为[1,1,1,1,1....] 注:为使似然函数越大,则需要最小二乘法函数越小越好 线性回归中为什么选用平方和作为误差函数?假设模型结果与测量值 误差满足 ...
- 【原创 Hadoop&Spark 动手实践 12】Spark MLLib 基础、应用与信用卡欺诈检测系统动手实践
[原创 Hadoop&Spark 动手实践 12]Spark MLLib 基础.应用与信用卡欺诈检测系统动手实践
- ML.NET 示例:目录
ML.NET 示例中文版:https://github.com/feiyun0112/machinelearning-samples.zh-cn 英文原版请访问:https://github.com/ ...
- kaggle信用卡欺诈看异常检测算法——无监督的方法包括: 基于统计的技术,如BACON *离群检测 多变量异常值检测 基于聚类的技术;监督方法: 神经网络 SVM 逻辑回归
使用google翻译自:https://software.seek.intel.com/dealing-with-outliers 数据分析中的一项具有挑战性但非常重要的任务是处理异常值.我们通常将异 ...
- 100天搞定机器学习|Day56 随机森林工作原理及调参实战(信用卡欺诈预测)
本文是对100天搞定机器学习|Day33-34 随机森林的补充 前文对随机森林的概念.工作原理.使用方法做了简单介绍,并提供了分类和回归的实例. 本期我们重点讲一下: 1.集成学习.Bagging和随 ...
- ML.NET 示例:二元分类之用户评论的情绪分析
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- ML.NET 示例:二元分类之垃圾短信检测
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- ML.NET 示例:多类分类之鸢尾花分类
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- ML.NET教程之情感分析(二元分类问题)
机器学习的工作流程分为以下几个步骤: 理解问题 准备数据 加载数据 提取特征 构建与训练 训练模型 评估模型 运行 使用模型 理解问题 本教程需要解决的问题是根据网站内评论的意见采取合适的行动. 可用 ...
随机推荐
- LeetCode题解Maximum Binary Tree
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...
- 洗礼灵魂,修炼python(27)--异常处理(1)—>了解异常
python学到这,其实你应该是在入门到进阶的中间阶段了,但是还没有到进阶的阶段的,这是肯定的,因为进阶得可以从实际问题中解决问题的,比如写一个自动化的爬虫程序啊,对一件事物作大数据归纳分析,开发一个 ...
- python 之路初(一):pycharm 安装 和 环境配置 和 中文乱码问题
从健身和学习中我一体会到坚持的力量.想写写东西的想法已经好久了,就是不知道怎么开始.最近生活开始给我各种攻击和磨练,我从声嘶力竭到沉默到默默坚持自己,改变自己并总结告诉自己:少说多看,看破不说破,宁愿 ...
- mysql启动失败又一例
搭的wordpress报错: 后台用的mysql,之前也崩过,原因是虚拟内存耗尽,通过增加swap空间最终让数据重新启动. 但仅过一晚上,数据库再次崩溃.看来要查一查是什么程序耗尽资源. 执行top, ...
- nodejs中async使用
waterfall , parallel , series , eachSeries //var async = require('async'); /*** *① * 串行有关联 执行每个函数 ...
- 关于var与function的解析顺序问题
先给几段代码,看看你能知道运行结果不 function example1() { var f = function() {return 1;}; return f; var f = function( ...
- luogu P4146 序列终结者
嘟嘟嘟 这是一道splay基础题. 最坑的一点是,因为有些节点可能没有左儿子或右儿子,所以必须把t[0].Max赋成-INF! 因为这个调了半天,看来回头复习复习splay是对的-- #include ...
- Jedis关于Set的API Demo
package com.daxin.jedis_datastructure; import java.util.Set; import org.junit.After; import org.juni ...
- 转载 【.NET基础】--委托、事件、线程(1) https://www.cnblogs.com/chengzish/p/4559268.html
[.NET基础]--委托.事件.线程(1) 1,委托 是存放方法的指针的清单,也就是装方法的容器 A, 新建winform项目[01委托],项目中添加dg_SayHi.cs 委托类 用于存储方法 ...
- oracle 索引扫描类型的分类与构造
1. INDEX RANGE SCAN--请记住这个INDEX RANGE SCAN扫描方式drop table t purge;create table t as select * from dba ...