写在前面

准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正。

如果有朋友对此感兴趣,可以加入我:https://github.com/feiyun0112/machinelearning-samples.zh-cn

垃圾短信检测

ML.NET 版本 API 类型 状态 应用程序类型 数据类型 场景 机器学习任务 算法
v0.7 动态API 可能需要更新项目结构以匹配模板 控制台应用程序 .tsv 文件 垃圾信息检测 二元分类 SDCA(线性学习器),还展示了CustomMapping评估器,它可以将自定义代码添加到ML.NET管道

在这个示例中,您将看到如何使用ML.NET来预测短信是否是垃圾信息。在机器学习领域中,这种类型的预测被称为二元分类

问题

我们的目标是预测一个短信是否是垃圾信息(一个不相关的/不想要的消息)。我们将使用UCI的SMS Spam Collection Data Set,其中包含近6000条被分类为“垃圾信息”或“ham”(不是垃圾信息)的消息。我们将使用这个数据集来训练一个模型,该模型可以接收新消息并预测它们是否是垃圾信息。

这是一个二元分类的示例,因为我们将短信分类为两个类别。

解决方案

要解决这个问题,首先我们将建立一个评估器来定义我们想要使用的机器学习管道。 然后,我们将在现有数据上训练这个评估器,评估其有多好,最后我们将使用该模型来预测一些示例消息是否是垃圾信息。

1. 建立评估器

为了建立评估器,我们将:

  • 定义如何读取从 https://archive.ics.uci.edu/ml/datasets/SMS+Spam+Collection 下载的垃圾信息数据集。

  • 应用多个数据转换:

    • 将标签(“spam”或“ham”)转换为布尔值(“true”表示垃圾信息),这样我们就可以在二元分类器中使用它。
    • 将短信转换为数字向量,以便机器学习训练器可以使用它
  • 添加一个训练器(如StochasticDualCoordinateAscent)。

初始代码类似以下内容:

// Set up the MLContext, which is a catalog of components in ML.NET.
var mlContext = new MLContext(); // Create the reader and define which columns from the file should be read.
var reader = new TextLoader(mlContext, new TextLoader.Arguments()
{
Separator = "tab",
HasHeader = true,
Column = new[]
{
new TextLoader.Column("Label", DataKind.Text, 0),
new TextLoader.Column("Message", DataKind.Text, 1)
}
}); var data = reader.Read(new MultiFileSource(TrainDataPath)); // Create the estimator which converts the text label to boolean, featurizes the text, and adds a linear trainer.
var estimator = mlContext.Transforms.CustomMapping<MyInput, MyOutput>(MyLambda.MyAction, "MyLambda")
.Append(mlContext.Transforms.Text.FeaturizeText("Message", "Features"))
.Append(mlContext.BinaryClassification.Trainers.StochasticDualCoordinateAscent());

2. 评估模型

对于这个数据集,我们将使用交叉验证来评估我们的模型。将数据集划分成5个不相交的子集,训练5个模型(每个模型使用其中4个子集),并在训练中没有使用的数据子集上测试模型。

var cvResults = mlContext.BinaryClassification.CrossValidate(data, estimator, numFolds: 5);
var aucs = cvResults.Select(r => r.metrics.Auc);
Console.WriteLine("The AUC is {0}", aucs.Average());

请注意,通常我们在训练后评估模型。 但是,交叉验证包括模型训练部分,因此我们不需要先执行Fit()。 但是,我们稍后将在完整数据集上训练模型以利用其他数据。

3. 训练模型

为了训练模型,我们将调用评估器的Fit()方法,同时提供完整的训练数据。

var model = estimator.Fit(data);

4. 使用模型

训练完模型后,您可以使用Predict()API来预测新文本是否垃圾信息。 在这种情况下,我们更改模型的阈值以获得更好的预测。 我们这样做是因为我们的数据有偏差,大多数消息都不是垃圾信息。

// The dataset we have is skewed, as there are many more non-spam messages than spam messages.
// While our model is relatively good at detecting the difference, this skewness leads it to always
// say the message is not spam. We deal with this by lowering the threshold of the predictor. In reality,
// it is useful to look at the precision-recall curve to identify the best possible threshold.
var inPipe = new TransformerChain<ITransformer>(model.Take(model.Count() - 1).ToArray());
var lastTransformer = new BinaryPredictionTransformer<IPredictorProducing<float>>(mlContext, model.LastTransformer.Model, inPipe.GetOutputSchema(data.Schema), model.LastTransformer.FeatureColumn, threshold: 0.15f, thresholdColumn: DefaultColumnNames.Probability); ITransformer[] parts = model.ToArray();
parts[parts.Length - 1] = lastTransformer;
var newModel = new TransformerChain<ITransformer>(parts); // Create a PredictionFunction from our model
var predictor = newModel.MakePredictionFunction<SpamInput, SpamPrediction>(mlContext); var input = new SpamInput { Message = "free medicine winner! congratulations" };
Console.WriteLine("The message '{0}' is {1}", input.Message, predictor.Predict(input).isSpam ? "spam" : "not spam");

ML.NET 示例:二元分类之垃圾短信检测的更多相关文章

  1. python数据挖掘第三篇-垃圾短信文本分类

    数据挖掘第三篇-文本分类 文本分类总体上包括8个步骤.数据探索分析->数据抽取->文本预处理->分词->去除停用词->文本向量化表示->分类器->模型评估.重 ...

  2. 新出台的治理iMessage垃圾短信的规则

    工信部拟制定<通信短信息服务管理规定>,为治理垃圾短信提供执法根据.当中,对于苹果iMessage垃圾信息泛滥现象,工信部也将跟踪研究技术监測和防范手段.这意味着长期以来处于监管" ...

  3. 使用Python 2.7实现的垃圾短信识别器

    最近参加比赛,写了一个垃圾短信识别器,在这里做一下记录. 官方提供的数据是csv文件,其中训练集有80万条数据,测试集有20万条数据,训练集的格式为:行号 标记(0为普通短信,1为垃圾短信) 短信内容 ...

  4. 单线程与多线程的简单示例(以Windows服务发短信为示例)

    单线程示例: public delegate void SM(); SM sm = new SM(() =>    {                    while (true)       ...

  5. XSS之偷梁换柱--盲打垃圾短信平台

    https://www.t00ls.net/thread-49742-1-1.html

  6. R 基于朴素贝叶斯模型实现手机垃圾短信过滤

    # 读取数数据, 查看数据结构 df_raw <- read.csv("sms_spam.csv", stringsAsFactors=F) str(df_raw) leng ...

  7. ML.NET 示例:目录

    ML.NET 示例中文版:https://github.com/feiyun0112/machinelearning-samples.zh-cn 英文原版请访问:https://github.com/ ...

  8. 腾讯云短信 nodejs 接入, 通过验证码修改手机示例

    腾讯云短信 nodejs 接入, 通过验证码修改手机示例 参考:腾讯云短信文档国内短信快速入门qcloudsms Node.js SDK文档中心>短信>错误码 nodejs sdk 使用示 ...

  9. atitit.短信 验证码  破解  v3 p34  识别 绕过 系统方案规划----业务相关方案 手机验证码  .doc

    atitit.短信 验证码  破解  v3 p34  识别 绕过 系统方案规划----业务相关方案 手机验证码  .doc 1. 手机短信验证码 vs 图片验证码 安全性(破解成本)确实要高一些1 1 ...

随机推荐

  1. Django之form总结

    复习Django项目结构: 主要的文件:manage.py,url.py,views.py,settings.py,models.py manage.py:项目管理文件,一般不做修改. url.py: ...

  2. multipart/form-data文件上传

    form表单的enctype属性:规定了form表单数据在发送到服务器时候的编码方式 application/x-www-form-urlencoded:默认编码方式 multipart/form-d ...

  3. webrtc学习: 部署stun和turn服务器

    webrtc的P2P穿透部分是由libjingle实现的. 步骤顺序大概是这样的: 1. 尝试直连. 2. 通过stun服务器进行穿透 3. 无法穿透则通过turn服务器中转. stun 服务器比较简 ...

  4. 图解get方法与filter方法

    在django的数据库查找方法中,get与filter方法是查找单目数据,用图记录了这两个方法查找数据各自返回的是什么?

  5. axure元件库导入后重启程序元件库消失问题

    把元件库文件放在我的文档里的 \Documents\Axure\元件库 就可以了,重新启动程序不会消失

  6. MySQL使用索引的场景分析、不能使用索引的场景分析

    一.MySQL中能够使用索引的典型场景 1.匹配全值.对索引中的列都有等值匹配的条件.即使是在and中,and前后的列都有索引并进行等值匹配. 2.匹配值的范围查询,对索引的值能够进行范围查找. 3. ...

  7. 06LaTeX学习系列之---TeXstudio的使用

    目录 目录 前言 (一)TeXstudio的认识 1.TeXstudio的安装 2.TeXstudio的优点 3.Texstudio的界面 (二)TeXstudio的编译与查看 (三)TeXstudi ...

  8. January 12th, 2018 Week 02nd Friday

    Nothing behind me, everything ahead of me, as is ever so on the road. 我的身后空空荡荡,整个世界都在前方,这就是在路上. That ...

  9. 17秋 软件工程 Alpha展示博客

    成员简介 姓名 个人简介 博客地址 郑世强 郑世强,计算机三班,了解java web端和Android端编程,使用过Spring MVC和Spring Boot开发商业程序,Android端学习了rx ...

  10. CSS3 animation动画,循环间的延时执行时间

    如下代码,其中的delay值为3s,但是animation按现在的规则,这个delay是指动画开始前的延时,在动画循环执行间,这个delay是不生效的. .item{ webkit-animation ...