Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2.  
  3. def f_theta(value, theta):
  4.     if value <= theta:
  5.         return 0
  6.  
  7.     return 1
  8.  
  9. def ideal_theta(theta, step, data):
  10.     for point in data:
  11.         if f_theta(point[0], theta) != point[1]:
  12.             theta += step * (1 if theta <= point[0] else -1)
  13.  
  14.     return theta
  15.  
  16. def check_accuracy(theta, data):
  17.     correct = 0
  18.  
  19.     for point in data:
  20.         if f_theta(point[0], theta) == point[1]:
  21.             correct += 1
  22.  
  23.     return correct / len(data)
  24.  
  25. def main():
  26.     data = [(-100, 0), \
  27.             (-20,  0), \
  28.             (-40,  0), \
  29.             (-67,  0), \
  30.             (-10,  0), \
  31.             (-5,   0), \
  32.             (-21,  0), \
  33.             (6,    1), \
  34.             (20,   1), \
  35.             (30,   1), \
  36.             (70,   1), \
  37.             (40,   1), \
  38.             (10,   1)]
  39.  
  40.     theta = 20
  41.     step = 100
  42.     acc = 0
  43.  
  44.     while acc != 1:
  45.         acc = check_accuracy(theta, data)
  46.         theta = ideal_theta(20, step, data)
  47.         step -= 0.5
  48.  
  49.         print("THETA: " + str(theta) + " STEP: " + str(step) + " ACC: " + str(acc))
  50.  
  51.     plt.hlines(0, -120, 120)
  52.    
  53.     blue = [point[0] for point in data if point[1] == 0]
  54.     red = [point[0] for point in data if point[1] == 1]
  55.    
  56.     plt.plot(red, [0] * len(red), "bo")
  57.     plt.plot(blue, [0] * len(blue), "ro")
  58.     plt.plot([theta], [0], "go")
  59.    
  60.     plt.axis("off")
  61.    
  62.     plt.show()
  63.    
  64. if __name__ == "__main__":
  65.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement