Nav*_*a R 31 javascript crossfilter
简单来说有人能解释如何减少功能与它的参数reduceAdd,reduceSum,reduceRemove工程crossfilter?
Isi*_*dum 55
请记住,map reduce会按特定维度的键减少数据集.例如,让我们使用带有记录的crossfilter实例:
[
{ 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" },
]
Run Code Online (Sandbox Code Playgroud)
和尺寸名称,年龄,价值和性别.我们将使用reduce方法减少性别维度.
首先,我们定义reduceAdd,reduceRemove和reduceInitial回调方法.
reduceInitial返回具有缩小对象和初始值形式的对象.它不需要参数.
function reduceInitial() {
return {
worth: 0,
count: 0
};
}
Run Code Online (Sandbox Code Playgroud)
reduceAdd定义当记录被"过滤"为特定键的缩减对象时发生的情况.第一个参数是简化对象的瞬态实例.第二个对象是当前记录.该方法将返回增强的瞬态缩减对象.
function reduceAdd(p, v) {
p.worth = p.worth + v.worth;
p.count = p.count + 1;
return p;
}
Run Code Online (Sandbox Code Playgroud)
reduceRemove与reduceAdd(至少在这个例子中)相反.它采用相同的参数reduceAdd.这是必需的,因为在过滤记录时更新组减少,有时需要从先前计算的组减少中删除记录.
function reduceRemove(p, v) {
p.worth = p.worth - v.worth;
p.count = p.count - 1;
return p;
}
Run Code Online (Sandbox Code Playgroud)
调用reduce方法如下所示:
mycf.dimensions.gender.reduce(reduceAdd, reduceRemove, reduceInitial)
Run Code Online (Sandbox Code Playgroud)
要查看减少的值,请使用该all方法.要查看前n个值,请使用该top(n)方法.
mycf.dimensions.gender.reduce(reduceAdd, reduceRemove, reduceInitial).all()
Run Code Online (Sandbox Code Playgroud)
返回的数组将(应该)如下所示:
[
{ key: "m", value: { worth: 161000000000, count: 3 } },
{ key: "f", value: { worth: 35000000000, count: 2 } },
]
Run Code Online (Sandbox Code Playgroud)
减少数据集的目标是通过首先按公共密钥对记录进行分组,然后将这些分组的维度减少为每个密钥的单个值来派生新数据集.在这种情况下,我们按性别分组,并通过添加共享相同密钥的记录值来减少该分组的价值维度.
其他reduceX方法是reduce方法的便捷方法.
对于这个例子来说,这reduceSum是最合适的替代品.
mycf.dimensions.gender.reduceSum(function(d) {
return d.worth;
});
Run Code Online (Sandbox Code Playgroud)
调用all返回的分组将(应该)看起来像:
[
{ key: "m", value: 161000000000 },
{ key: "f", value: 35000000000 },
]
Run Code Online (Sandbox Code Playgroud)
reduceCount 将计算记录
mycf.dimensions.gender.reduceCount();
Run Code Online (Sandbox Code Playgroud)
调用all返回的分组将(应该)看起来像:
[
{ key: "m", value: 3 },
{ key: "f", value: 2 },
]
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 :)
资料来源:https://github.com/square/crossfilter/wiki/API-Reference
小智 6
http://blog.rusty.io/2012/09/17/crossfilter-tutorial/
var livingThings = crossfilter([
// Fact data.
{ name: “Rusty”, type: “human”, legs: 2 },
{ name: “Alex”, type: “human”, legs: 2 },
{ name: “Lassie”, type: “dog”, legs: 4 },
{ name: “Spot”, type: “dog”, legs: 4 },
{ name: “Polly”, type: “bird”, legs: 2 },
{ name: “Fiona”, type: “plant”, legs: 0 }
]);
Run Code Online (Sandbox Code Playgroud)
例如,我家里有多少生物?
为此,我们将调用groupAll便捷函数,它将所有记录选择到一个组中,然后调用该函数,该reduceCount函数创建记录计数.
// How many living things are in my house?
var n = livingThings.groupAll().reduceCount().value();
console.log("There are " + n + " living things in my house.") // 6
Run Code Online (Sandbox Code Playgroud)
现在让我们算一下我家里所有的腿.同样,我们将使用该groupAll函数来获取单个组中的所有记录,然后我们调用该
reduceSum函数.这将把价值加在一起.有什么价值?好吧,我们想要腿,所以让我们传递一个函数来提取并返回事实中的腿数.
// How many total legs are in my house?
var legs = livingThings.groupAll().reduceSum(function(fact) {
return fact.legs;
}).value()
console.log("There are " + legs + " legs in my house.")
Run Code Online (Sandbox Code Playgroud)
reduceCountfunction创建记录计数.
reduceSumfunction是这些记录的总和值.