Skip to content

Instantly share code, notes, and snippets.

@AfvanMoopen
Created June 23, 2021 13:13
Show Gist options
  • Save AfvanMoopen/3f26d67ff8fc6d9732d7fdfb7331dae2 to your computer and use it in GitHub Desktop.
Save AfvanMoopen/3f26d67ff8fc6d9732d7fdfb7331dae2 to your computer and use it in GitHub Desktop.
def preceptron(X, y, lr, epochs):
m,n = X.shape
theta = np.zeros((n+1, 1))
n_miss_list = []
for epoch in range(epochs):
n_miss = 0
for idx, x_i in enumerate(X):
x_i = np.insert(x_i, 0, 1).reshape(-1, 1)
y_hat = step_func(np.dot(x_i.T, theta))
if (np.squeeze(y_hat) - y[idx]) != 0:
theta += lr * ((y[idx] - y_hat) * x_i)
n_miss += 1
n_miss_list.append(n_miss)
return theta, n_miss, list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment