import numpy as np
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
X = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [0, 1, 1, 0]
### Your code here ###
MLPClassifier(hidden_layer_sizes=6, max_iter=100000)
clf.predict(X)
array([0, 1, 1, 0])
def xor(X):
return (- np.sign(X[:,0]) * np.sign(X[:,1]) + 1) / 2
# generate XOR-like data
X_train = np.random.uniform(-1, 1, (10000, 2))
y_train = xor(X_train)
# tmp = X_train > 0
# y_train = 1 * (tmp[:, 0] ^ tmp[:, 1])
from sklearn.neural_network import MLPRegressor
### Your code here ###
xmin, xmax = -1., 1.
ymin, ymax = -1., 1.
xx, yy = np.meshgrid(np.arange(xmin, xmax, 0.01),
np.arange(ymin, ymax, 0.01))
X_meshgrid = np.transpose([xx.ravel(), yy.ravel()])
# replace xor by the learned function here
Z = xor(X_meshgrid)
Z = Z.reshape(xx.shape)
fig, ax = plt.subplots()
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.contourf(xx, yy, Z)
im = ax.imshow(Z, interpolation='nearest')
fig.colorbar(im, ax=ax)
plt.savefig("xor.pdf")
### Your code here ###