Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- def f_theta(value, theta):
- if value <= theta:
- return 0
- return 1
- def ideal_theta(theta, step, data):
- for point in data:
- if f_theta(point[0], theta) != point[1]:
- theta += step * (1 if theta <= point[0] else -1)
- return theta
- def check_accuracy(theta, data):
- correct = 0
- for point in data:
- if f_theta(point[0], theta) == point[1]:
- correct += 1
- return correct / len(data)
- def main():
- data = [(-100, 0), \
- (-20, 0), \
- (-40, 0), \
- (-67, 0), \
- (-10, 0), \
- (-5, 0), \
- (-21, 0), \
- (6, 1), \
- (20, 1), \
- (30, 1), \
- (70, 1), \
- (40, 1), \
- (10, 1)]
- theta = 20
- step = 100
- acc = 0
- while acc != 1:
- acc = check_accuracy(theta, data)
- theta = ideal_theta(20, step, data)
- step -= 0.5
- print("THETA: " + str(theta) + " STEP: " + str(step) + " ACC: " + str(acc))
- plt.hlines(0, -120, 120)
- blue = [point[0] for point in data if point[1] == 0]
- red = [point[0] for point in data if point[1] == 1]
- plt.plot(red, [0] * len(red), "bo")
- plt.plot(blue, [0] * len(blue), "ro")
- plt.plot([theta], [0], "go")
- plt.axis("off")
- plt.show()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement