Advertisement
Guest User

Perceptron

a guest
Nov 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. trainFile = open("E:\\Studies\\4-2\\Pattern Recognition Sessional\\Train.txt", "r")
  5.  
  6. dataSet = []
  7. weights = []
  8.  
  9. numOfFeatures = 3;
  10. numOfClasses = 3;
  11.  
  12. LABEL_INDEX = numOfFeatures + 1;
  13. PUNISH = -1;
  14. REWARD = 1;
  15. RO = 1;
  16.  
  17.  
  18. def findLabel (sample, weights):
  19. label = 0;
  20. maxVal = -math.inf
  21. for i in range (numOfClasses):
  22. val = 1;
  23. for j in range(numOfFeatures + 1):
  24. val += sample[j]*weights[i][j]
  25. if val > maxVal:
  26. label = i;
  27. maxVal = val;
  28. return label+1 #classes are 1,2.. || you calculated 0,1...
  29.  
  30.  
  31. def updateWeight (weightID, sample, type): #type : punish = -1, reward = +1
  32. for i in range (numOfFeatures + 1):
  33. weights[weightID][i] += RO*type*sample[i]
  34.  
  35.  
  36. def prepareDataset():
  37. count = 0;
  38. for line in trainFile:
  39. count+=1;
  40. if(count==1): #dont take the first line
  41. continue
  42. sample = []
  43. for s in line.split():
  44. try:
  45. sample.append(float(s))
  46. except ValueError:
  47. pass
  48. if(len(sample)==numOfFeatures+1):
  49. sample.insert(numOfFeatures, 1) #bias
  50. dataSet.append(sample)
  51.  
  52.  
  53. def inintializeWeights():
  54. for i in range (numOfClasses):
  55. w = []
  56. for j in range (numOfFeatures):
  57. w.append(0)
  58. w.append(1)
  59. weights.append(w)
  60.  
  61.  
  62.  
  63. def main ():
  64. prepareDataset()
  65. inintializeWeights()
  66.  
  67. for sample in dataSet:
  68. result = findLabel(sample, weights)
  69. print(sample, result)
  70. if (result != sample[LABEL_INDEX]):
  71. updateWeight(int(result)-1, sample, PUNISH)
  72. updateWeight(int(sample[LABEL_INDEX])-1, sample, REWARD)
  73.  
  74.  
  75. print("So the final vectors are:")
  76. for w in weights:
  77. print(w)
  78.  
  79. #main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement