Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$sigmoid
New from version 8.0.1.
The $sigmoid operator in Amazon DocumentDB applies the logistic sigmoid function, 1 / (1 + e^(-x)), to a numeric input. The function maps any real number to a value between 0 and 1, producing an S-shaped curve. This is useful when you need to normalize values into the 0–1 range, such as converting raw scores into probabilities or relevance scores.
Parameters
Example (MongoDB Shell)
The following example shows how to use the $sigmoid operator to convert logit values into probabilities.
Create sample documents
db.predictions.insertMany([
{_id: 1, logit: -2},
{_id: 2, logit: 0},
{_id: 3, logit: 2}
]);
Query example
db.predictions.aggregate([
{ $project: { probability: { $sigmoid: "$logit" } } }
]);
Output
[
{_id: 1, probability: 0.11920292202211755},
{_id: 2, probability: 0.5},
{_id: 3, probability: 0.8807970779778823}
]
Code examples
To view a code example for using the $sigmoid operator, choose the tab for the language that you want to use:
- 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()