Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. from __future__ import print_function
  2. from matplotlib import pyplot as plt
  3. import numpy as np
  4.  
  5. def predict (input, weights):
  6. activation =0.0
  7. for i, w in zip(input, weights):
  8. activation +=i*w
  9. return 1.0 if activation>=0.0 else 0.0
  10.  
  11. def plot (matrix, weights=None, title='Prediction Matrix'):
  12.  
  13. fig, ax = plt.subplots()# возвращает кортеж из фигуры и осей
  14. ax.set_title(title)
  15. ax.set_xlabel("i1")
  16. ax.set_ylabel("i2")
  17.  
  18. if weights != None:
  19. map_min = 0.0
  20. map_max = 1.1
  21. y_res = 0.001
  22. x_res = 0.001
  23. ys = np.arange(map_min, map_max, y_res)
  24. xs = np.arange(map_min, map_max, x_res)
  25. zs = []
  26. for cur_y in np.arange(map_min, map_max, y_res):
  27. for cur_x in np.arange(map_min, map_max, x_res):
  28. zs.append(predict([1.0, cur_x, cur_y], weights))
  29. xs, ys = np.meshgrid(xs, ys)
  30. zs = np.array(zs)
  31. zs = zs.reshape(xs.shape)
  32. cp = plt.contourf(xs, ys, zs, levels=[-1, -0.0001, 0, 1], colors=('b', 'r'), alpha=0.1) # линии уровня
  33.  
  34. c1_data = [[], []]
  35. c0_data = [[], []]
  36. for i in range(len(matrix)):
  37. cur_i1 = matrix[i][1]
  38. cur_i2 = matrix[i][2]
  39. cur_y = matrix[i][-1]
  40. if cur_y == 1:
  41. c1_data[0].append(cur_i1)
  42. c1_data[1].append(cur_i2)
  43. else:
  44. c0_data[0].append(cur_i1)
  45. c0_data[1].append(cur_i2)
  46.  
  47. plt.xticks(np.arange(0.0, 1.1, 0.1))
  48. plt.yticks(np.arange(0.0, 1.1, 0.1))
  49. plt.xlim(0, 1.05)
  50. plt.ylim(0, 1.05)
  51.  
  52. c0s = plt.scatter(c0_data[0], c0_data[1], s=40.0, c='r', label='Class -1')
  53. c1s = plt.scatter(c1_data[0], c1_data[1], s=40.0, c='b', label='Class 1')
  54.  
  55. plt.legend(fontsize=10, loc=1)
  56. plt.show()
  57. return
  58.  
  59.  
  60.  
  61. def accuracy (matrix, weights):
  62. num_correct =0.0
  63. preds =[]
  64. for i in range(len(matrix)):
  65. pred = predict(matrix[i][:-1], weights)
  66. preds.append(pred)
  67. if pred ==matrix[i][-1]: num_correct+=1.0
  68. print('Predictions:', preds)
  69. return num_correct/float(len(matrix))
  70.  
  71. def train_weights(matrix, weights, nb_epoch=10, l_rate=1.00, do_plot=False, stop_early=True):
  72. for epoch in range(nb_epoch):
  73. cur_acc = accuracy(matrix, weights)
  74. print("\nEpoch %d \nWeights: " %epoch, weights)
  75. print("Accuracy: ", cur_acc)
  76.  
  77. if cur_acc==1.0 and stop_early: break
  78. if do_plot: plot(matrix, weights, title='Epoch %d'%epoch)
  79.  
  80. for i in range(len(matrix)):
  81. prediction = predict(matrix[i][:-1], weights)
  82. error = matrix[i][-1] - prediction
  83.  
  84. for j in range(len(weights)):
  85.  
  86. weights[j] = weights[j] + (l_rate * error * matrix[i][j])
  87.  
  88. plot(matrix, weights, title='Final Epoch')
  89. return weights
  90.  
  91. def main():
  92.  
  93. nb_epoch =10
  94. l_rate=1.0
  95. plot_each_epoch = True
  96. stop_early =True
  97.  
  98. matrix = [[1.00, 0.08, 0.72, 1.0],
  99. [1.00, 0.10, 1.00, 0.0],
  100. [1.00, 0.26, 0.58, 1.0],
  101. [1.00, 0.35, 0.95, 0.0],
  102. [1.00, 0.45, 0.15, 1.0],
  103. [1.00, 0.60, 0.30, 1.0],
  104. [1.00, 0.70, 0.65, 0.0],
  105. [1.00, 0.92, 0.45, 0.0]]
  106. weights = [ 0.20, 1.00, -1.00]
  107.  
  108.  
  109.  
  110. train_weights(matrix, weights=weights, nb_epoch=nb_epoch, l_rate=l_rate, do_plot=plot_each_epoch, stop_early=stop_early)
  111.  
  112. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement