我试图$and用MongoDB 否定一个条款,我收到了一条MongoError: invalid operator: $and消息.基本上我想要实现的目标如下:
query = { $not: { $and: [{institution_type:'A'}, {type:'C'}] } }
Run Code Online (Sandbox Code Playgroud)
这是否可以在mongo查询中表达?
这是一个示例集合:
{ "institution_type" : "A", "type" : "C" }
{ "institution_type" : "A", "type" : "D" }
{ "institution_type" : "B", "type" : "C" }
{ "institution_type" : "B", "type" : "D" }
Run Code Online (Sandbox Code Playgroud)
我想要回来的是以下内容:
{ "institution_type" : "A", "type" : "D" }
{ "institution_type" : "B", "type" : "C" }
{ "institution_type" : "B", "type" : "D" }
Run Code Online (Sandbox Code Playgroud) 我非常熟悉使用Reservoir Sampling在一次通过数据的过程中从一组未确定的长度中采样.在我看来,这种方法的一个限制是它仍然需要在返回任何结果之前传递整个数据集.从概念上讲,这是有道理的,因为必须允许整个序列中的项目有机会替换先前遇到的项目以获得统一的样本.
有没有办法在整个序列评估之前能够产生一些随机结果?我正在考虑那种适合python的伟大的itertools库的懒惰方法.也许这可以在一些给定的容错范围内完成?我很感激有关这个想法的任何反馈!
为了澄清这个问题,这个图总结了我对不同采样技术的内存与流媒体权衡的理解.我想要的是属于Stream Sampling的类别,我们事先并不知道人口的长度.

显然,由于我们很可能将样本偏向人口的开头,因此不知道先验长度并且仍然得到统一样本存在看似矛盾.有没有办法量化这种偏见?是否需要权衡利弊?有没有人有一个聪明的算法来解决这个问题?
假设,为了这个问题,我希望能够在Javascript中创建一个函数,将一个数组的所有元素追加到另一个数组.实现此目的的一种方法是,如果您有权访问目标数组,则说:
var destination = [1,2,3];
var source = [4,5];
Array.prototype.push.apply(destination, source);
console.log(destination); // [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
现在,由于Array.prototype.push.apply非常丑陋,我想将它别名为更好的东西,例如:
var pushAll = Array.prototype.push.apply;
Run Code Online (Sandbox Code Playgroud)
我应该能够使用两个参数调用,上下文(目标)和参数数组(源).但是,当我尝试使用别名时,会发生以下情况:
pushAll(destination, [6,7]);
TypeError: Function.prototype.apply was called on [object global], which
is a object and not a function
Run Code Online (Sandbox Code Playgroud)
所以很明显这个apply函数没有绑定push,这让我尝试了这个:
var pushAll = Function.prototype.apply.bind(Array.prototype.push);
pushAll(destination, [6,7]);
console.log(destination); // [1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)
这显然很好.我的问题是,为什么我必须绑定push方法才能应用?不应该绑定Array.prototype.push.apply吗?为什么以不同的名称调用它会导致在未绑定的上下文中调用它?
假设我有两个三维矩阵,就像这样(取自这个matlab示例http://www.mathworks.com/help/matlab/ref/dot.html):
A = cat(3,[1 1;1 1],[2 3;4 5],[6 7;8 9])
B = cat(3,[2 2;2 2],[10 11;12 13],[14 15; 16 17])
Run Code Online (Sandbox Code Playgroud)
如果我想沿第三维采取成对点积,我可以在matlab中这样做:
C = dot(A,B,3)
Run Code Online (Sandbox Code Playgroud)
哪会得到结果:
C =
106 140
178 220
Run Code Online (Sandbox Code Playgroud)
numpy中的等效操作是什么,最好是矢量化选项,以避免必须在整个数组中编写一个double for循环.我似乎无法理解应该做什么np.tensordot或np.inner应该做什么,但它们可能是选择.