python - 使用 MongoDB 聚合框架计算一阶导数

标签 python mongodb mapreduce pymongo aggregation-framework

是否可以使用聚合框架计算一阶导数?

例如,我有数据:

{time_series : [10,20,40,70,110]}

我正在尝试获得如下输出:

{derivative : [10,20,30,40]}

最佳答案

db.collection.aggregate(
    [
      {
        "$addFields": {
          "indexes": {
            "$range": [
              0,
              {
                "$size": "$time_series"
              }
            ]
          },
          "reversedSeries": {
            "$reverseArray": "$time_series"
          }
        }
      },
      {
        "$project": {
          "derivatives": {
            "$reverseArray": {
              "$slice": [
                {
                  "$map": {
                    "input": {
                      "$zip": {
                        "inputs": [
                          "$reversedSeries",
                          "$indexes"
                        ]
                      }
                    },
                    "in": {
                      "$subtract": [
                        {
                          "$arrayElemAt": [
                            "$$this",
                            0
                          ]
                        },
                        {
                          "$arrayElemAt": [
                            "$reversedSeries",
                            {
                              "$add": [
                                {
                                  "$arrayElemAt": [
                                    "$$this",
                                    1
                                  ]
                                },
                                1
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  }
                },
                {
                  "$subtract": [
                    {
                      "$size": "$time_series"
                    },
                    1
                  ]
                }
              ]
            }
          },
          "time_series": 1
        }
      }
    ]
)

我们可以在 3.4+ 版本中使用上述管道来执行此操作。 在管道中,我们使用 $addFields 流水线阶段。运算符添加“time_series”的元素索引的数组来做文档,我们还反转了时间序列数组并将其添加到文档中,分别使用 $range $reverseArray 运营商

我们在这里反转了数组,因为位置 p 的元素数组中的元素总是大于位置 p+1 的元素这意味着 [p] - [p+1] < 0我们不想使用 $multiply 在这里。(请参阅版本 3.2 的管道)

接下来我们$zipped具有索引数组的时间序列数据并应用了 substract 使用 $map 对结果数组进行表达式运算符。

然后我们 $slice丢弃null/None的结果数组中的值并重新反转结果。


在 3.2 中,我们可以使用 $unwind 运算符来展开我们的数组,并通过将文档指定为操作数而不是以$为前缀的传统“路径”来包含数组中每个元素的索引。

接下来,我们需要 $group 我们的文件并使用 $push 累加器运算符返回一个子文档数组,如下所示:

{
    "_id" : ObjectId("57c11ddbe860bd0b5df6bc64"),
    "time_series" : [
        { "value" : 10, "index" : NumberLong(0) },
        { "value" : 20, "index" : NumberLong(1) },
        { "value" : 40, "index" : NumberLong(2) },
        { "value" : 70, "index" : NumberLong(3) },
        { "value" : 110, "index" : NumberLong(4) }
    ]
}

终于来了 $project 阶段。在这个阶段,我们需要使用 $map 运算符将一系列表达式应用于 $group 中新计算的数组中的每个元素阶段。

这是 $map 内部发生的事情(参见 $map 作为 for 循环)in 表达式:

对于每个子文档,我们使用 $let value 字段分配给一个变量。变量运算符。然后我们从数组中下一个元素的“value”字段的值中减去它的值。

由于数组中的下一个元素是当前索引处的元素加一,所以我们需要 $arrayElemAt 的帮助。运算符和一个简单的 $add 当前元素的索引和 1 .

$subtract 表达式返回一个负值,所以我们需要将该值乘以 -1使用 $multiply 运算符。

我们还需要 $filter 结果数组,因为它的最后一个元素是 Nonenull .原因是当当前元素是最后一个元素时,$subtract返回 None因为下一个元素的索引等于数组的大小。

db.collection.aggregate([
  {
    "$unwind": {
      "path": "$time_series",
      "includeArrayIndex": "index"
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "time_series": {
        "$push": {
          "value": "$time_series",
          "index": "$index"
        }
      }
    }
  },
  {
    "$project": {
      "time_series": {
        "$filter": {
          "input": {
            "$map": {
              "input": "$time_series",
              "as": "el",
              "in": {
                "$multiply": [
                  {
                    "$subtract": [
                      "$$el.value",
                      {
                        "$let": {
                          "vars": {
                            "nextElement": {
                              "$arrayElemAt": [
                                "$time_series",
                                {
                                  "$add": [
                                    "$$el.index",
                                    1
                                  ]
                                }
                              ]
                            }
                          },
                          "in": "$$nextElement.value"
                        }
                      }
                    ]
                  },
                  -1
                ]
              }
            }
          },
          "as": "item",
          "cond": {
            "$gte": [
              "$$item",
              0
            ]
          }
        }
      }
    }
  }
])

另一个我认为效率较低的选项是使用 map_reduce 对我们的集合执行 map/reduce 操作。方法。

>>> import pymongo
>>> from bson.code import Code
>>> client = pymongo.MongoClient()
>>> db = client.test
>>> collection = db.collection
>>> mapper = Code("""
...               function() {
...                 var derivatives = [];
...                 for (var index=1; index<this.time_series.length; index++) {
...                   derivatives.push(this.time_series[index] - this.time_series[index-1]);
...                 }
...                 emit(this._id, derivatives);
...               }
...               """)
>>> reducer = Code("""
...                function(key, value) {}
...                """)
>>> for res in collection.map_reduce(mapper, reducer, out={'inline': 1})['results']:
...     print(res)  # or do something with the document.
... 
{'value': [10.0, 20.0, 30.0, 40.0], '_id': ObjectId('57c11ddbe860bd0b5df6bc64')}

您还可以检索所有文档并使用 numpy.diff像这样返回导数:

import numpy as np


for document in collection.find({}, {'time_series': 1}):
    result = np.diff(document['time_series']) 

关于python - 使用 MongoDB 聚合框架计算一阶导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38957649/

相关文章:

python - Python 中的 MD5 哈希函数更改数据

python - OpenCV 绘制非匹配点

python - 嵌套 python 列表推导式以构建列表列表

java - Spring Data mongoTemplate.save() 行为

mongodb - 所有者 1 的 NoSQL 设计 :M PC and PC 1:M Parts

hadoop - 如何定期在Hadoop上分析数据

python - 从 Pyspark LDA 模型中提取文档主题矩阵

hadoop - hadoop mapreduce.partition.keypartitioner.options不起作用

hadoop - 如何配置 Avro MapReduce 作业以将结果输出到单个文件中?

node.js - 无法在 GCP 云运行上使用 MongoDB 并将其连接到 NodeJS