Last active
September 30, 2017 06:35
-
-
Save kentatogashi/932d4eaa478d90ec8735c4829d0e0665 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# prepare data | |
d = 2 | |
N = 3 | |
mean = 5 | |
rng = np.random.RandomState(100) | |
x1 = rng.randn(N, d) | |
x2 = rng.randn(N, d) + np.array([mean, mean]) | |
x = np.concatenate((x1, x2), axis=0) | |
# plot data | |
# plt.scatter(x[:,0], x[:,1]) | |
# plt.show() | |
# init weight and bias | |
w = np.zeros(d) | |
b = 0 | |
def y(x): | |
return step(np.dot(x, w) + b) | |
def step(x): | |
return 1 * (x > 0) | |
def t(x): | |
if i < N: | |
return 0 | |
else: | |
return 1 | |
while True: | |
classified = True | |
for i in range(N * 2): | |
delta_w = (t(i) - y(x[i])) * x[i] | |
delta_b = (t[i] - y(x[i])) | |
w += delta_w | |
b += delta_b | |
classified *= all(delta_w == 0) * (delta_b == 0) | |
if classified: | |
break | |
print(weight, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment