【问题标题】:Detecting anomalies in objects检测对象中的异常
【发布时间】:2018-05-29 11:17:19
【问题描述】:

假设我有一个 json 对象数组。

{ firstName: John, Children: ["Maria","Alfred"], married: false }
{ firstName: George, Children: ['**zoekerbergen alfonso the second**','Harvey'], married: false }

{ firstName: Hary, Children: ["Sam","Obama"], married: false }

模式是,通常孩子是一个小单词名称的数组。

然而,Zoekerbergen alfonso 第二个是异常的。

有没有办法学习对象的模式,然后检测异常,例如那些或其他的异常,例如一个有 1000 个孩子的人。

基本模式学习和检测各种异常。

谢谢。

【问题讨论】:

标签: javascript machine-learning


【解决方案1】:

对于 JSON 架构解决方案,您的规则可能过于复杂,但您可以轻松创建自己的规则集,如下所示:

var list = [
    {firstName: 'John', Children: ['Maria', 'Alfred'], married: false},
    {firstName: 'George', Children: ['**zoekerbergen alfonso the second**', 'Harvey'], married: false},
    {firstName: 'Hary', Children: ['Sam', 'Obama', 'Peter'], married: false}
];
// change with your max
var maxChildren = 2;

// add more rules if needed
var anomaliesRules = [
    function(row) {
        return row.Children !== null && row.Children.length > maxChildren;
    },
    function(row) {
        // you can define your own rules per item
        return row.Children.filter(function(child) { return !child.match(/^[A-Z][a-z]+$/); }).length > 0;
    }
];

var checkAllAnomalies = function(item) {
    for (var i = 0; i < anomaliesRules.length; i++) {
        if (anomaliesRules[i](item)) return true;
    }
    return false;
};

var anomalies = list.filter(checkAllAnomalies);
console.log(anomalies);

您最终会得到与您定义的至少一个异常匹配的所有项目。

【讨论】:

    【解决方案2】:

    您可以创建一个 JSON 模式来描述您的 JSON 数据(与 XML 的 XSD 相同)。它允许为字符串字段定义正则表达式模式等等。

    JSON Schema 是一个词汇表,允许您验证、注释和 操作 JSON 文档。

    请看这里https://github.com/json-schema-org/json-schema-spec

    【讨论】:

    • 是的,我想过这一点,但人们可能会在 json-schema 上出错,所以我想了解如何在 json 对象数组上应用异常检测,因为这些可能表明存在问题跨度>
    • 好吧,我相信它是最好的解决方案。在这里jsonschema.net 你可以为现有的 json 生成模式,然后进行所需的更改
    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2019-03-09
    • 2014-05-06
    • 2020-08-27
    相关资源
    最近更新 更多