本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$sigmoid
8.0.1 版的新功能。
Amazon DocumentDB 中的$sigmoid運算子會將邏輯 Sigmoid 函數 1 / (1 + e^(-x))套用至數值輸入。函數會將任何實數映射到介於 0 和 1 之間的值,產生 S 形曲線。當您需要將值標準化為 0–1 範圍時,例如將原始分數轉換為機率或相關性分數時,這會很有用。
參數
範例 (MongoDB Shell)
下列範例示範如何使用 $sigmoid運算子將 logit 值轉換為機率。
建立範例文件
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()