View a markdown version of this page

$sigmoid - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$sigmoid

8.0.1 版中的新增内容。

Amazon DocumentDB 中的$sigmoid运算符将逻辑 sigmoid 函数应用于数字1 / (1 + e^(-x))输入。该函数将任何实数映射到 0 到 1 之间的值,从而生成 S-shaped 曲线。当您需要将值归一化为 0—1 范围时,例如将原始分数转换为概率或相关性分数时,这很有用。

参数

  • expression:解析为数值的表达式。

示例(MongoDB Shell)

以下示例说明如何使用$sigmoid运算符将对数值转换为概率。

创建示例文档

db.predictions.insertMany([ {_id: 1, logit: -2}, {_id: 2, logit: 0}, {_id: 3, logit: 2} ]);

查询示例

db.predictions.aggregate([ { $project: { probability: { $sigmoid: "$logit" } } } ]);

输出

[ {_id: 1, probability: 0.11920292202211755}, {_id: 2, probability: 0.5}, {_id: 3, probability: 0.8807970779778823} ]

代码示例

要查看使用$sigmoid运算符的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('predictions'); const result = await collection.aggregate([ { $project: { probability: { $sigmoid: "$logit" } } } ]).toArray(); console.log(result); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] collection = db['predictions'] result = list(collection.aggregate([ {'$project': {'probability': {'$sigmoid': '$logit'}}} ])) print(result) finally: client.close() example()