交叉过滤器中的reduceAdd,reduceSum,reduceRemove函数是什么?应该如何使用它们? [英] What are the reduceAdd, reduceSum , reduceRemove functions in crossfilter? How should they be used?

查看:83
本文介绍了交叉过滤器中的reduceAdd,reduceSum,reduceRemove函数是什么?应该如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以简单地用参数 reduceAdd reduceSum reduceRemove 在 crossfilter 中工作吗?

Can someone explain in simple terms how reduce function with its arguments reduceAdd, reduceSum, reduceRemove works in crossfilter?

推荐答案

请记住,map reduce通过特定维的键来减少数据集。例如,让我们使用带有记录的交叉过滤器实例:

Remember that map reduce reduces a dataset by keys of a particular dimension. For example lets use a crossfilter instance with records:

[
    { name: "Gates",      age: 57,   worth: 72000000000, gender: "m" },
    { name: "Buffet",     age: 59,   worth: 58000000000, gender: "m" },
    { name: "Winfrey",    age: 83,   worth:  2900000000, gender: "f"   },
    { name: "Bloomberg",  age: 71,   worth: 31000000000, gender: "m"  },
    { name: "Walton",     age: 64,   worth: 33000000000, gender: "f"  },
]

维度名称,年龄,价值和性别。我们将使用reduce方法减少性别维度。

and dimensions name, age, worth, and gender. We will reduce the gender dimension using the reduce method.

首先,我们定义reduceAdd,reduceRemove和reduceInitial回调方法。

First we define the reduceAdd, reduceRemove, and reduceInitial callback methods.

reduceInitial 返回一个对象,其形式为简化对象和初始值。它不带参数。

reduceInitial returns an object with the form of the reduced object and the initial values. It takes no parameters.

function reduceInitial() {
    return {
        worth: 0,
        count: 0
    };
}

reduceAdd 定义了什么当记录被过滤到特定键的简化对象中时发生。第一个参数是简化对象的瞬态实例。第二个对象是当前记录。该方法将返回增强的瞬态缩减对象。

reduceAdd defines what happens when a record is being 'filtered into' the reduced object for a particular key. The first parameter is a transient instance of the reduced object. The second object is the current record. The method will return the augmented transient reduced object.

function reduceAdd(p, v) {
    p.worth = p.worth + v.worth;
    p.count = p.count + 1;
    return p;
}

redmove reduceAdd 相反(至少在此示例中)。它采用与 reduceAdd 相同的参数。之所以需要这样做,是因为随着记录的过滤,组归约会更新,并且有时需要从先前计算的组归约中删除记录。

reduceRemove does the opposite of reduceAdd (at least in this example). It takes the same parameters as reduceAdd. It is needed because group reduces are updated as records are filtered and sometimes records need to be removed from a previously computed group reduction.

function reduceRemove(p, v) {
    p.worth = p.worth - v.worth;
    p.count = p.count - 1;
    return p;
}

调用reduce方法看起来像这样:

Invoking the reduce method would look like this:

mycf.dimensions.gender.reduce(reduceAdd, reduceRemove, reduceInitial)

要查看减少的值,请使用 all 方法。要查看前n个值,请使用 top(n)方法。

To take a peek at the reduced values, use the all method. To see the top n values use the top(n) method.

mycf.dimensions.gender.reduce(reduceAdd, reduceRemove, reduceInitial).all()

返回的数组应该(应该)如下:

The returned array would (should) look like:

[
    { key: "m", value: { worth: 161000000000, count: 3 } },
    { key: "f", value: { worth:  35000000000, count: 2 } },
]

减少数据集的目的是通过首先按公用键对记录进行分组,然后将这些分组的维数减小为每个键的一个值来派生新数据集。在这种情况下,我们按性别分组并通过添加共享相同键的记录的值来减少该分组的价值维度。

The goals of reducing a dataset is to derive a new dataset by first grouping records by common keys, then reducing a dimension those groupings into a single value for each key. In this case we grouped by gender and reduced the worth dimension of that grouping by adding the values of records that shared the same key.

其他reduceX方法是用于

The other reduceX methods are convience methods for the reduce method.

在此示例中, reduceSum 是最合适的替代方法。

For this example reduceSum would be the most appropriate replacement.

mycf.dimensions.gender.reduceSum(function(d) {
    return d.worth;
});

在返回的分组上调用 all 将(应该):

Invoking all on the returned grouping would (should) look like:

[
    { key: "m", value: 161000000000 },
    { key: "f", value: 35000000000 },
]

reduceCount 将对记录进行计数

mycf.dimensions.gender.reduceCount();

在返回的分组上调用 all 将(应该):

Invoking all on the returned grouping would (should) look like:

[
    { key: "m", value: 3 },
    { key: "f", value: 2 },
]

希望这会有所帮助: )

Hope this helps :)

来源: https://github.com/ square / crossfilter / wiki / API参考

这篇关于交叉过滤器中的reduceAdd,reduceSum,reduceRemove函数是什么?应该如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆