Skip to content
Closed
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
75 changes: 62 additions & 13 deletions machine_learning/multilayer_perceptron_classifier.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
from sklearn.neural_network import MLPClassifier
"""
Multilayer Perceptron (MLP) Classifier Example

X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]
y = [0, 1, 0, 0]
A Multilayer Perceptron (MLP) is a type of feedforward artificial neural network
that consists of at least three layers of nodes: an input layer, one or more hidden layers,

Check failure on line 5 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/multilayer_perceptron_classifier.py:5:89: E501 Line too long (91 > 88)
and an output layer. Each node (except for the input nodes) is a neuron that uses a nonlinear

Check failure on line 6 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/multilayer_perceptron_classifier.py:6:89: E501 Line too long (93 > 88)
activation function.

Mathematical Concept:
---------------------
MLPs learn a function f(·): R^m → R^o by training on a dataset, where m is the number of input features

Check failure on line 11 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/multilayer_perceptron_classifier.py:11:89: E501 Line too long (103 > 88)
and o is the number of output classes. The network adjusts its weights using backpropagation to minimize

Check failure on line 12 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/multilayer_perceptron_classifier.py:12:89: E501 Line too long (104 > 88)
the difference between predicted and actual outputs.

clf = MLPClassifier(
solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1
)
Practical Use Cases:
--------------------
- Handwritten digit recognition (e.g., MNIST dataset)
- Binary and multiclass classification tasks
- Predicting outcomes based on multiple features (e.g., medical diagnosis, spam detection)

Check failure on line 19 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

machine_learning/multilayer_perceptron_classifier.py:19:89: E501 Line too long (90 > 88)

clf.fit(X, y)
References:
-----------
- https://en.wikipedia.org/wiki/Multilayer_perceptron
- https://scikit-learn.org/stable/modules/neural_networks_supervised.html
- https://medium.com/@aryanrusia8/multi-layer-perceptrons-explained-7cb9a6e318c3

Example:
--------
>>> X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]
>>> y = [0, 1, 0, 0]
>>> multilayer_perceptron_classifier(X, y, [[0.0, 0.0], [1.0, 1.0]])
[0, 1]
"""

test = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
Y = clf.predict(test)
from typing import List, Sequence

Check failure on line 35 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

machine_learning/multilayer_perceptron_classifier.py:35:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 35 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

machine_learning/multilayer_perceptron_classifier.py:35:1: UP035 Import from `collections.abc` instead: `Sequence`
from sklearn.neural_network import MLPClassifier

Check failure on line 36 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

machine_learning/multilayer_perceptron_classifier.py:35:1: I001 Import block is un-sorted or un-formatted


def wrapper(y):
def multilayer_perceptron_classifier(
train_features: Sequence[Sequence[float]],
train_labels: Sequence[int],
test_features: Sequence[Sequence[float]],
) -> List[int]:

Check failure on line 43 in machine_learning/multilayer_perceptron_classifier.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

machine_learning/multilayer_perceptron_classifier.py:43:6: UP006 Use `list` instead of `List` for type annotation
"""
>>> [int(x) for x in wrapper(Y)]
[0, 0, 1]
Train a Multilayer Perceptron classifier and predict labels for test data.

Args:
train_features: Training data features, shape (n_samples, n_features).
train_labels: Training data labels, shape (n_samples,).
test_features: Test data features to predict, shape (m_samples, n_features).

Returns:
List of predicted labels for the test data.

Raises:
ValueError: If the number of training samples and labels do not match.

Example:
>>> X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]
>>> y = [0, 1, 0, 0]
>>> multilayer_perceptron_classifier(X, y, [[0.0, 0.0], [1.0, 1.0]])
[0, 1]
"""
return list(y)
if len(train_features) != len(train_labels):
raise ValueError("Number of training samples and labels must match.")

clf = MLPClassifier(
solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1
)
clf.fit(train_features, train_labels)
predictions = clf.predict(test_features)
return list(predictions)


if __name__ == "__main__":
Expand Down
Loading