From 9db13124e8b146e15da689e1d2b8cca88113460a Mon Sep 17 00:00:00 2001 From: Dibbu-cell Date: Thu, 2 Oct 2025 15:23:39 +0530 Subject: [PATCH 1/2] machine_learning\multilayer_perceptron_classifier --- .../multilayer_perceptron_classifier.py | 78 +++++++++++++++---- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/machine_learning/multilayer_perceptron_classifier.py b/machine_learning/multilayer_perceptron_classifier.py index 40f998c7dfa2..0b44659284c2 100644 --- a/machine_learning/multilayer_perceptron_classifier.py +++ b/machine_learning/multilayer_perceptron_classifier.py @@ -1,29 +1,75 @@ -from sklearn.neural_network import MLPClassifier - -X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]] -y = [0, 1, 0, 0] +""" +Multilayer Perceptron (MLP) Classifier Example +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, +and an output layer. Each node (except for the input nodes) is a neuron that uses a nonlinear +activation function. -clf = MLPClassifier( - solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1 -) +Mathematical Concept: +--------------------- +MLPs learn a function f(·): R^m → R^o by training on a dataset, where m is the number of input features +and o is the number of output classes. The network adjusts its weights using backpropagation to minimize +the difference between predicted and actual outputs. -clf.fit(X, y) +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) +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 -test = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]] -Y = clf.predict(test) +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] +""" +from typing import List, Sequence +from sklearn.neural_network import MLPClassifier -def wrapper(y): +def multilayer_perceptron_classifier( + train_features: Sequence[Sequence[float]], + train_labels: Sequence[int], + test_features: Sequence[Sequence[float]], +) -> List[int]: """ - >>> [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__": import doctest - - doctest.testmod() + doctest.testmod() \ No newline at end of file From 51af916196808bf15c14ce1c742fb3ba3dd21e0d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Oct 2025 10:05:35 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/multilayer_perceptron_classifier.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine_learning/multilayer_perceptron_classifier.py b/machine_learning/multilayer_perceptron_classifier.py index 0b44659284c2..d19607a5bf2f 100644 --- a/machine_learning/multilayer_perceptron_classifier.py +++ b/machine_learning/multilayer_perceptron_classifier.py @@ -35,6 +35,7 @@ from typing import List, Sequence from sklearn.neural_network import MLPClassifier + def multilayer_perceptron_classifier( train_features: Sequence[Sequence[float]], train_labels: Sequence[int], @@ -70,6 +71,8 @@ def multilayer_perceptron_classifier( predictions = clf.predict(test_features) return list(predictions) + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod()