Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions machine_learning/sigmoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Sigmoid activation function for binary classification models.

The sigmoid function maps a real-valued input to the interval (0, 1).

>>> sigmoid(0)
0.5
>>> round(sigmoid(2), 4)
0.8808
>>> round(sigmoid(-2), 4)
0.1192
>>> sigmoid(1000)
1.0
>>> sigmoid(-1000)
0.0
"""

import math


def sigmoid(value: float) -> float:
"""Return the numerically stable sigmoid of *value*."""
if value >= 0:
return 1 / (1 + math.exp(-value))
exp_value = math.exp(value)
return exp_value / (1 + exp_value)


if __name__ == "__main__":
import doctest

doctest.testmod()
37 changes: 37 additions & 0 deletions machine_learning/softmax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Softmax activation function for multiclass classification.

Softmax converts a sequence of scores into probabilities that sum to one.

>>> probabilities = softmax([2.0, 1.0, 0.1])
>>> [round(value, 4) for value in probabilities]
[0.659, 0.2424, 0.0986]
>>> sum(softmax([1.0, 2.0, 3.0]))
1.0
>>> softmax([])
Traceback (most recent call last):
...
ValueError: scores must not be empty
"""

import math


def softmax(scores: list[float]) -> list[float]:
"""Return softmax probabilities for a non-empty list of scores.

Subtracting the maximum score before exponentiation improves numerical
stability and prevents overflow for large input values.
"""
if not scores:
raise ValueError("scores must not be empty")

max_score = max(scores)
exponentials = [math.exp(score - max_score) for score in scores]
total = sum(exponentials)
return [value / total for value in exponentials]


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading