|
1 | 1 | """ |
2 | | -Multilayer Perceptron (MLP) Classifier Example |
| 2 | +Multilayer Perceptron (MLP) Classifier |
3 | 3 |
|
4 | 4 | A Multilayer Perceptron (MLP) is a type of feedforward artificial neural network |
5 | 5 | that consists of at least three layers of nodes: an input layer, one or more hidden |
|
20 | 20 | - Predicting outcomes based on multiple features |
21 | 21 | (e.g., medical diagnosis, spam detection) |
22 | 22 |
|
| 23 | +Advantages: |
| 24 | +----------- |
| 25 | +- Can learn non-linear decision boundaries |
| 26 | +- Works well with complex pattern recognition |
| 27 | +- Flexible architecture for various problem types |
| 28 | +
|
| 29 | +Limitations: |
| 30 | +------------ |
| 31 | +- Requires careful tuning of hyperparameters |
| 32 | +- Sensitive to feature scaling |
| 33 | +- Can overfit on small datasets |
| 34 | +
|
| 35 | +Time Complexity: O(n_samples * n_features * n_layers * n_epochs) |
| 36 | +Space Complexity: O(n_features * n_hidden_units + n_hidden_units * n_classes) |
| 37 | +
|
23 | 38 | References: |
24 | 39 | ----------- |
25 | 40 | - https://en.wikipedia.org/wiki/Multilayer_perceptron |
@@ -62,13 +77,17 @@ def multilayer_perceptron_classifier( |
62 | 77 | >>> X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]] |
63 | 78 | >>> y = [0, 1, 0, 0] |
64 | 79 | >>> multilayer_perceptron_classifier(X, y, [[0.0, 0.0], [1.0, 1.0]]) |
65 | | - # Multiple possible outputs True |
| 80 | + [0, 1] |
66 | 81 | """ |
67 | 82 | if len(train_features) != len(train_labels): |
68 | 83 | raise ValueError("Number of training samples and labels must match.") |
69 | 84 |
|
70 | 85 | clf = MLPClassifier( |
71 | | - solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1 |
| 86 | + solver="lbfgs", |
| 87 | + alpha=1e-5, |
| 88 | + hidden_layer_sizes=(5, 2), |
| 89 | + random_state=42, # Fixed for deterministic results |
| 90 | + max_iter=1000, # Ensure convergence |
72 | 91 | ) |
73 | 92 | clf.fit(train_features, train_labels) |
74 | 93 | predictions = clf.predict(test_features) |
|
0 commit comments