Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. import numpy as np
  2. import sys
  3.  
  4. #---------------------------------------------------------------
  5.  
  6. def metrics(tp, fp, tn, fn, pos, neg):
  7.  
  8. tpr = float(tp) / pos
  9. fpr = float(fp) / neg
  10. errorRate = float(fp + fn) / (pos + neg)
  11. accuracy = float(tp + tn) / (pos + neg)
  12. precision = float(tp) / (tp + fp)
  13.  
  14. return tpr, fpr, errorRate,accuracy, precision
  15.  
  16.  
  17. def readFile(filename):
  18.  
  19. file = open(filename, 'r')
  20.  
  21. line = file.readline()
  22. specs = map(int, line.split())
  23.  
  24. data = np.genfromtxt(file)
  25. file.close()
  26.  
  27. return (data, specs)
  28.  
  29. #--------------------------------------------------------------
  30. # training
  31.  
  32. def makeCentroids(data, nA, nB, nC):
  33. # Centroids are just the average of the data blocks
  34.  
  35. A = data[:nA]
  36. B = data[nA:nB]
  37. C = data[nB:nC]
  38.  
  39. centroidA = np.mean(A, axis = 0)
  40. centroidB = np.mean(B, axis = 0)
  41. centroidC = np.mean(C, axis = 0)
  42.  
  43. return (centroidA, centroidB, centroidC)
  44.  
  45. def findDecisionFunction(centroidA, centroidB):
  46.  
  47. line = np.subtract(centroidB, centroidA)
  48. midpoint = np.divide(np.add(centroidB, centroidA), 2)
  49. decision_value = decision_function(line, centroidA, midpoint)
  50. sign = decision_value / abs(decision_value)
  51.  
  52. # Just a function with values stored in it
  53. # Use +1 or -1 to find classes
  54.  
  55. def classAorB(point_nd):
  56. value = decision_function(line, point_nd, midpoint)
  57. if value == 0:
  58. decision = 1
  59. else:
  60. decision = value * sign
  61. return decision / abs(decision)
  62.  
  63. return classAorB
  64.  
  65. def decision_function(line, point_nd, midpoint):
  66. return np.dot(line, np.subtract(point_nd, midpoint))
  67.  
  68.  
  69. if __name__ == "__main__":
  70.  
  71. if len(sys.argv) != 3:
  72. print "Received wrong number of arguments.\nUsage: \'python triclassify.py train.txt test.txt\'"
  73. sys.exit()
  74.  
  75. training_file = sys.argv[1]
  76. test_file_name = sys.argv[2]
  77.  
  78.  
  79. data, (d, a,b,c) = readFile(training_file)
  80. (centroidA, centroidB, centroidC) = makeCentroids(data,a,a+b,a+b+c)
  81.  
  82. classAOrB = findDecisionFunction(centroidA, centroidB)
  83. classAOrC = findDecisionFunction(centroidA, centroidC)
  84. classBOrC = findDecisionFunction(centroidB, centroidC)
  85.  
  86. test_file = open(test_file_name, 'r')
  87. line = test_file.readline()
  88. (dim, numA, numB, numC) = map(int, line.split())
  89.  
  90. keys =['A','B','C']
  91.  
  92. truePositives = {key: 0 for key in keys}
  93. trueNegatives = {key: 0 for key in keys}
  94. falsePositives = {key: 0 for key in keys}
  95. falseNegatives = {key: 0 for key in keys}
  96.  
  97. # Test A
  98. for i in range(numA):
  99. line = test_file.readline()
  100. point = map(float, line.split())
  101. if classAOrB(point) == 1:
  102. trueNegatives['B'] += 1
  103. if classAOrC(point) == 1:
  104. truePositives['A'] += 1
  105. trueNegatives['C'] += 1
  106. else: # C
  107. falseNegatives['A'] += 1
  108. falsePositives['C'] += 1
  109. else: # B or C
  110. falseNegatives['A'] += 1
  111. if classBOrC(point) == 1:
  112. falsePositives['B'] += 1
  113. trueNegatives['C'] += 1
  114. else: # C
  115. trueNegatives['B'] += 1
  116. falsePositives['C'] += 1
  117.  
  118. # Test B
  119. for i in range(numB):
  120. line = test_file.readline()
  121. point = map(float, line.split())
  122. if classAOrB(point) == 1:
  123. falseNegatives['B'] += 1
  124. if classAOrC(point) == 1:
  125. falsePositives['A'] += 1
  126. trueNegatives['C'] += 1
  127. else: # C
  128. trueNegatives['A'] += 1
  129. falsePositives['C'] += 1
  130. else: # B or C
  131. trueNegatives['A'] += 1
  132. if classBOrC(point) == 1:
  133. truePositives['B'] += 1
  134. trueNegatives['C'] += 1
  135. else: # C
  136. falseNegatives['B'] += 1
  137. falsePositives['C'] += 1
  138.  
  139. # Test C
  140. for i in range(numC):
  141. line = test_file.readline()
  142. point = map(float, line.split())
  143. if classAOrB(point) == 1:
  144. trueNegatives['B']+= 1
  145. if classAOrC(point) == 1:
  146. falsePositives['A'] += 1
  147. falseNegatives['C'] += 1
  148. else: # C
  149. trueNegatives['A'] += 1
  150. truePositives['C'] += 1
  151. else: # B or C
  152. trueNegatives['A'] += 1
  153. if classBOrC(point) == 1:
  154. falsePositives['B'] += 1
  155. falseNegatives['C'] += 1
  156. else: # C
  157. trueNegatives['B'] += 1
  158. truePositives['C'] += 1
  159.  
  160. test_file.close()
  161.  
  162. #Calculate Metrics
  163. truePositiveRateA, falsePositiveRateA, errorRateA,accuracyA, precisionA = metrics(truePositives['A'], falsePositives['A'],trueNegatives['A'],falseNegatives['A'],numA,(numB+numC))
  164. truePositiveRateB, falsePositiveRateB, errorRateB,accuracyB, precisionB = metrics(truePositives['B'], falsePositives['B'],trueNegatives['B'],falseNegatives['B'],numB,(numA+numC))
  165. truePositiveRateC, falsePositiveRateC, errorRateC,accuracyC, precisionC = metrics(truePositives['C'], falsePositives['C'],trueNegatives['C'],falseNegatives['C'],numC,(numB+numA))
  166.  
  167. truePositiveRate = (truePositiveRateA + truePositiveRateB + truePositiveRateC) / 3.0
  168. falsePositiveRate = (falsePositiveRateA + falsePositiveRateB + falsePositiveRateC) / 3.0
  169. errorRate = (errorRateA + errorRateB + errorRateC) / 3.0
  170. accuracy = (accuracyA + accuracyB + accuracyC) / 3.0
  171. precision = (precisionA + precisionB + precisionC) / 3.0
  172.  
  173. print "True positive rate = %f" % truePositiveRate
  174. print "False positive rate = %f" % falsePositiveRate
  175. print "Error rate = %f" % errorRate
  176. print "Accuracy = %f" % accuracy
  177. print "Precision = %f" % precision
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement