Skip to content

Instantly share code, notes, and snippets.

View bobbywlindsey's full-sized avatar

Bobby Lindsey bobbywlindsey

View GitHub Profile
@bobbywlindsey
bobbywlindsey / perceptron-update-weights-algo2.py
Created October 7, 2019 21:33
Perceptron update weights with algo 2: weights = weights + error * vector
weights = initialize_weights(train_data.shape[1])
for epoch in range(3):
sum_of_squared_errors = 0.0
for sample in range(train_data.shape[0]):
vector = train_data.loc[sample].values
label = train_label.loc[sample]
if label == 0: label = -1
prediction = predict(vector, weights, 'sign')
error = label - prediction
sum_of_squared_errors += error**2
@bobbywlindsey
bobbywlindsey / perceptron-update-weights-algo1.py
Created October 7, 2019 21:32
Perceptron update weights with algo 1: weights = weights + label * vector
weights = initialize_weights(train_data.shape[1])
for epoch in range(3):
sum_of_squared_errors = 0.0
for sample in range(train_data.shape[0]):
vector = train_data.loc[sample].values
label = train_label.loc[sample]
if label == 0: label = -1
prediction = predict(vector, weights, 'sign')
error = label - prediction
sum_of_squared_errors += error**2
@bobbywlindsey
bobbywlindsey / perceptron-update-weights-three-epochs.py
Created October 7, 2019 21:31
Three iterations of updates for perceptron weights
weights = initialize_weights(train_data.shape[1])
for epoch in range(3):
sum_of_squared_errors = 0.0
for sample in range(train_data.shape[0]):
vector = train_data.loc[sample].values
label = train_label.loc[sample]
prediction = predict(vector, weights, 'step')
error = label - prediction
sum_of_squared_errors += error**2
weights = weights + error * vector
@bobbywlindsey
bobbywlindsey / perceptron-update-weights-one-epoch.py
Created October 7, 2019 21:30
One epoch update of perceptron weights
weights = initialize_weights(train_data.shape[1])
sum_of_squared_errors = 0.0
for sample in range(train_data.shape[0]):
vector = train_data.loc[sample].values
label = train_label.loc[sample]
prediction = predict(vector, weights, 'step')
error = label - prediction
sum_of_squared_errors += error**2
weights = weights + error * vector
print(f'SSE: {sum_of_squared_errors}')
@bobbywlindsey
bobbywlindsey / perceptron-predict-first-sample.py
Created October 7, 2019 21:30
Predict first sample with perceptron
weights = initialize_weights(train_data.shape[1])
vector = train_data.loc[0].values
label = train_label.loc[0]
prediction = predict(vector, weights, 'step')
error = label - prediction
print(f'Prediction: {prediction}, Label: {label}, Error: {error}')
@bobbywlindsey
bobbywlindsey / perceptron-inference.py
Created October 7, 2019 21:29
Perceptron inference
def activation(x, function_name):
if function_name == 'step':
return step(x)
elif function_name == 'sign':
return sign(x)
else:
raise NotImplementedError
def initialize_weights(num_columns):
@bobbywlindsey
bobbywlindsey / perceptron-splitting-features-and-labels.py
Created October 7, 2019 21:28
Splitting features and label for the perceptron data
train_data = pd.DataFrame(data = data, index=list(range(len(data))), columns=['X_1', 'X_2', 'label'])
# Don't forget to add a vector of 1s for the bias weight
train_data['bias'] = np.repeat(1.0, train_data.shape[0])
train_label = train_data.label
train_data = train_data[[each for each in train_data.columns if each != 'label']]
train_data.head()
@bobbywlindsey
bobbywlindsey / perceptron-data.py
Created October 7, 2019 21:27
Perceptron training data
import pandas as pd
import numpy as np
data = [[2.7810836,2.550537003,0],
[1.465489372,2.362125076,0],
[3.396561688,4.400293529,0],
[1.38807019,1.850220317,0],
[3.06407232,3.005305973,0],
[7.627531214,2.759262235,1],
[5.332441248,2.088626775,1],
@bobbywlindsey
bobbywlindsey / perceptron-activation-functions.py
Created October 7, 2019 21:26
Perceptron activation functions
def sign(x):
if x > 0:
return 1.0
elif x < 0:
return -1.0
else:
return 0.0
def step(x):

Keybase proof

I hereby claim:

  • I am bobbywlindsey on github.
  • I am bobbylindsey (https://keybase.io/bobbylindsey) on keybase.
  • I have a public key ASDhPbagCzR47qYnqzdc76WQ-KqheCkZRS_v8qgjmwLB1Ao

To claim this, I am signing this object: