ML.NET Cookbook:(18)如何在文本数据上训练模型?

一般来说,所有的ML.NET学习器都希望这些特征是一个浮点向量。因此,如果您的一些数据不是一个float,您需要将其转换为float。

如果我们想学习文本数据,我们需要从文本中“提取特征”。NLP(自然语言处理)的整个研究领域都在处理这个问题。在ML.NET中,我们提供了一些文本特征提取的基本机制:

  • 文本规范化(删除标点符号、变音符号、切换为小写等)

  • 基于分隔符的标记化。

  • 停用词删除。

  • Ngram和skip-gram提取。

  • TF-IDF重缩放。

  • 词袋转换。

NET提供了一个名为TextFeaturizer的“一站式”操作,它将上述步骤作为一个大的“文本特征化”来运行。我们已经在文本数据集上对它进行了广泛的测试,我们确信它的性能相当好,而不需要深入研究操作。

但是,我们还提供了一些基本操作,让您可以自定义NLP处理。下面是我们使用它们的例子。

维基百科 detox 数据集:

Sentiment   SentimentText
1 Stop trolling, zapatancas, calling me a liar merely demonstartes that you arer Zapatancas. You may choose to chase every legitimate editor from this site and ignore me but I am an editor with a record that isnt 99% trolling and therefore my wishes are not to be completely ignored by a sockpuppet like yourself. The consensus is overwhelmingly against you and your trollin g lover Zapatancas,  
1 ::::: Why are you threatening me? I'm not being disruptive, its you who is being disruptive.   
0 " *::Your POV and propaganda pushing is dully noted. However listing interesting facts in a netral and unacusitory tone is not POV. You seem to be confusing Censorship with POV monitoring. I see nothing POV expressed in the listing of intersting facts. If you want to contribute more facts or edit wording of the cited fact to make them sound more netral then go ahead. No need to CENSOR interesting factual information. "
0 ::::::::This is a gross exaggeration. Nobody is setting a kangaroo court. There was a simple addition concerning the airline. It is the only one disputed here.   
// 创建加载器:定义数据列以及它们在文本文件中的位置。
var loader = mlContext.Data.CreateTextLoader(new[] 
    {
        new TextLoader.Column("IsToxic", DataKind.Boolean, 0),
        new TextLoader.Column("Message", DataKind.String, 1),
    },
    hasHeader: true
);

// 加载数据。
var data = loader.Load(dataPath);

// 检查从文件中读取的消息文本。
var messageTexts = data.GetColumn<string>(data.Schema["Message"]).Take(20).ToArray();

// 应用ML.NET支持的各种文本操作。
var pipeline =
    // 一站式运行全部文本特征化。
    mlContext.Transforms.Text.FeaturizeText("TextFeatures", "Message")

    // 为以后的转换规范化消息文本
    .Append(mlContext.Transforms.Text.NormalizeText("NormalizedMessage", "Message"))

    // NLP管道1:词袋。
    .Append(mlContext.Transforms.Text.ProduceWordBags("BagOfWords", "NormalizedMessage"))

    // NLP管道2:bag of bigrams,使用散列而不是字典索引。
    .Append(mlContext.Transforms.Text.ProduceHashedWordBags("BagOfBigrams","NormalizedMessage", 
                ngramLength: 2, useAllLengths: false))

    // NLP管道3:具有TF-IDF加权的三字符序列包。
    .Append(mlContext.Transforms.Text.TokenizeIntoCharactersAsKeys("MessageChars", "Message"))
    .Append(mlContext.Transforms.Text.ProduceNgrams("BagOfTrichar", "MessageChars", 
                ngramLength: 3, weighting: NgramExtractingEstimator.WeightingCriteria.TfIdf))

    // NLP管道4:词嵌入。
    .Append(mlContext.Transforms.Text.TokenizeIntoWords("TokenizedMessage", "NormalizedMessage"))
    .Append(mlContext.Transforms.Text.ApplyWordEmbedding("Embeddings", "TokenizedMessage",
                WordEmbeddingEstimator.PretrainedModelKind.SentimentSpecificWordEmbedding));

// 让我们训练管道,然后将其应用于相同的数据。
// 请注意,即使在70KB的小数据集上,上面的管道也可能需要一分钟才能完成训练。
var transformedData = pipeline.Fit(data).Transform(data);

// 检查结果数据集的某些列。
var embeddings = transformedData.GetColumn<float[]>(mlContext, "Embeddings").Take(10).ToArray();
var unigrams = transformedData.GetColumn<float[]>(mlContext, "BagOfWords").Take(10).ToArray();

欢迎关注我的个人公众号”My IO“

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值