1- from sklearn .neural_network import MLPClassifier
2-
3- X = [[0.0 , 0.0 ], [1.0 , 1.0 ], [1.0 , 0.0 ], [0.0 , 1.0 ]]
4- y = [0 , 1 , 0 , 0 ]
1+ """
2+ Multilayer Perceptron (MLP) Classifier Example
53
4+ A Multilayer Perceptron (MLP) is a type of feedforward artificial neural network
5+ that consists of at least three layers of nodes: an input layer, one or more hidden layers,
6+ and an output layer. Each node (except for the input nodes) is a neuron that uses a nonlinear
7+ activation function.
68
7- clf = MLPClassifier (
8- solver = "lbfgs" , alpha = 1e-5 , hidden_layer_sizes = (5 , 2 ), random_state = 1
9- )
9+ Mathematical Concept:
10+ ---------------------
11+ MLPs learn a function f(·): R^m → R^o by training on a dataset, where m is the number of input features
12+ and o is the number of output classes. The network adjusts its weights using backpropagation to minimize
13+ the difference between predicted and actual outputs.
1014
11- clf .fit (X , y )
15+ Practical Use Cases:
16+ --------------------
17+ - Handwritten digit recognition (e.g., MNIST dataset)
18+ - Binary and multiclass classification tasks
19+ - Predicting outcomes based on multiple features (e.g., medical diagnosis, spam detection)
1220
21+ References:
22+ -----------
23+ - https://en.wikipedia.org/wiki/Multilayer_perceptron
24+ - https://scikit-learn.org/stable/modules/neural_networks_supervised.html
25+ - https://medium.com/@aryanrusia8/multi-layer-perceptrons-explained-7cb9a6e318c3
1326
14- test = [[0.0 , 0.0 ], [0.0 , 1.0 ], [1.0 , 1.0 ]]
15- Y = clf .predict (test )
27+ Example:
28+ --------
29+ >>> X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]
30+ >>> y = [0, 1, 0, 0]
31+ >>> multilayer_perceptron_classifier(X, y, [[0.0, 0.0], [1.0, 1.0]])
32+ [0, 1]
33+ """
1634
35+ from typing import List , Sequence
36+ from sklearn .neural_network import MLPClassifier
1737
18- def wrapper (y ):
38+ def multilayer_perceptron_classifier (
39+ train_features : Sequence [Sequence [float ]],
40+ train_labels : Sequence [int ],
41+ test_features : Sequence [Sequence [float ]],
42+ ) -> List [int ]:
1943 """
20- >>> [int(x) for x in wrapper(Y)]
21- [0, 0, 1]
44+ Train a Multilayer Perceptron classifier and predict labels for test data.
45+
46+ Args:
47+ train_features: Training data features, shape (n_samples, n_features).
48+ train_labels: Training data labels, shape (n_samples,).
49+ test_features: Test data features to predict, shape (m_samples, n_features).
50+
51+ Returns:
52+ List of predicted labels for the test data.
53+
54+ Raises:
55+ ValueError: If the number of training samples and labels do not match.
56+
57+ Example:
58+ >>> X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]]
59+ >>> y = [0, 1, 0, 0]
60+ >>> multilayer_perceptron_classifier(X, y, [[0.0, 0.0], [1.0, 1.0]])
61+ [0, 1]
2262 """
23- return list (y )
63+ if len (train_features ) != len (train_labels ):
64+ raise ValueError ("Number of training samples and labels must match." )
2465
66+ clf = MLPClassifier (
67+ solver = "lbfgs" , alpha = 1e-5 , hidden_layer_sizes = (5 , 2 ), random_state = 1
68+ )
69+ clf .fit (train_features , train_labels )
70+ predictions = clf .predict (test_features )
71+ return list (predictions )
2572
2673if __name__ == "__main__" :
2774 import doctest
28-
29- doctest .testmod ()
75+ doctest .testmod ()
0 commit comments