In [1]:
import numpy as np
class Perceptron:
def __init__(self, input_length, weights=None):
if weights is None:
self.weights = np.ones(input_length) * 0.5
else:
self.weights = weights
@staticmethod
def unit_step_function(x):
if x > 0.5:
return 1
return 0
def __call__(self, in_data):
weighted_input = self.weights * in_data
weighted_sum = weighted_input.sum()
return Perceptron.unit_step_function(weighted_sum)
p = Perceptron(-20, np.array([30, 30]))
for x in [np.array([0, 0]), np.array([0, 1]),
np.array([1, 0]), np.array([1, 1])]:
y = p(np.array(x))
print(x, y)
import numpy as np
import matplotlib.pyplot as plt
# function to plot graph for binary inputs
def plot_graph(data, idx_subplot, title):
xs = np.repeat(range(len(data)), 2)
ys = np.repeat(data, 2)
xs = xs[1:]
ys = ys[:-1]
plt.subplot(5,1,idx_subplot)
plt.title(title)
plt.plot(xs, ys)
plt.ylim(-0.5, 1.5)
In [ ]: