-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-07_XOR.py
More file actions
44 lines (35 loc) · 1.45 KB
/
01-07_XOR.py
File metadata and controls
44 lines (35 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# XOR problem using MLPClassifier from sklearn
# This code demonstrates how to use the MLPClassifier from sklearn to solve the XOR problem.
# The XOR problem is a classic example of a non-linear classification problem.
# The MLPClassifier is a multi-layer perceptron classifier that can learn non-linear decision boundaries.
# The code uses the logistic activation function and stochastic gradient descent (SGD) as the solver.
# The code also visualizes the decision boundary learned by the MLPClassifier.
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# XOR problem using MLPClassifier from sklearn
f=np.array([[0,0],[1,1],[0,1],[1,0]])
g=np.array([0,0,1,1])
clf=MLPClassifier(
hidden_layer_sizes=2,
activation='logistic',
solver='sgd',
random_state=42,
learning_rate_init=0.1,
max_iter=1000)
clf.fit(f,g)
x_min, x_max = f[:, 0].min() - .5, f[:, 0].max() + .5
y_min, y_max = f[:, 1].min() - .5, f[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, .02), np.arange(y_min, y_max, .02))
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
sns.set_theme(style='white')
plt.contourf(xx, yy, Z, cmap=plt.cm.RdBu, alpha=.3)
for i in range(4):
if g[i]==0:
plt.scatter(f[i][0],f[i][1],s=200,c='r',edgecolor='k')
else:
plt.scatter(f[i][0],f[i][1],s=200, c='b', edgecolor='k')
plt.title('XOR')
plt.show()