fork download
  1. import csv
  2. import matplotlib.pyplot as plt
  3.  
  4. class ANN:
  5. def __init__(self, path):
  6. with open(path) as Data_file:
  7. Datas = csv.reader(Data_file)
  8. AData = [data for data in Datas]
  9. self._RawData = self._InputData(path)
  10. self._Input_array, self._Target_list = self._DivInOut()
  11. self._DivTar()
  12. self.fold = 0
  13.  
  14. def _InputData(self, path):
  15. with open(path) as Data_file:
  16. Datas = csv.reader(Data_file)
  17. AData = [data for data in Datas]
  18. newMatrix = Matrix(len(AData), len(AData[0]))
  19. count = 0
  20. for i in range(newMatrix.numRows()):
  21. for j in range(newMatrix.numCols()):
  22. newMatrix[i, j] = AData[count][j]
  23. count += 1
  24. return newMatrix
  25.  
  26. def _DivInOut(self):
  27. AInput = self._RawData.tolist() # Convert Matrix to a list
  28. AOutput = [row[-1] for row in AInput] # Extract the last column as output
  29.  
  30. self.NumData = len(AInput)
  31. self.NumInput = len(AInput[0]) - 1 # Number of columns except the output column
  32. return AInput, AOutput
  33.  
  34.  
  35. def _DivTar(self):
  36. Class = set(self._Target_list)
  37. self.NumOutput = len(Class)
  38. self._Target_array = [
  39. [1 if self._Target_list[i] == val else 0 for val in Class] for i in range(self.NumData)
  40. ]
  41. def CleanData(self, data_range):
  42. low, high = data_range
  43. newList= Alist() #ใช้ลิสที่เขียนขึ้นมาเองในการเก็บค่า
  44. case1,case2 = (0,0)
  45. for i in range(self._Input_array.num_rows()):
  46. for j in range(self._Input_array.num_cols()):
  47. try:
  48. self._Input_array[i,j]=float(self._Input_array[i,j])
  49. except ValueError:
  50. case1+=1
  51. newList.Append(i)
  52. continue
  53. if not ((low <= self._Input_array[i,j] <= high)):
  54. case2+=1
  55. newList.Append(i)
  56. newList.ClearRepeatVal() #เอาค่า row ที่ซ้ำกันออกไป
  57. print(f"Inputs contain {case1} non-float values")
  58. print(f"inputs contain {case2} out-of-range values")
  59. print(f"Removing {len(newList)} rows......")
  60. for i in range(len(newList)-1,-1,-1):
  61. self._Input_array = self._Input_array.delete(newList[i],0)
  62. self._Target_list.Pop(newList[i])
  63. self.NumData,self.NumInput = self._Input_array.shape
  64. print("Removing process done")
  65. self._DivTar()
  66.  
  67. def _SeparateFold(self,fold,reset = False): #แบ่งชุดข้อมูลการ Test
  68. if reset:
  69. self.fold = 0
  70. assert self.fold != fold , "Out of range"
  71. print(f"Divided into {fold} folds. This is round {self.fold + 1}")
  72. Num=self.NumData//fold
  73. self.Input_Train = Matrix.zeros((self.NumData-Num , self.NumInput) , 'f')
  74. self.Input_Test = Matrix.zeros((Num , self.NumInput) , 'f')
  75. self.Target_Train = Matrix.zeros((self.NumData-Num , self.NumOutput) , 'i')
  76. self.Target_Test = Matrix.zeros((Num , self.NumOutput) , 'i')
  77.  
  78. testing_indices = data_indices[:fold_size]
  79. training_indices = data_indices[fold_size:]
  80.  
  81. self.training_data = (
  82. Matrix([self.inputs.data[i] for i in training_indices]),
  83. Matrix([self.targets.data[i] for i in training_indices])
  84. )
  85.  
  86. self.testing_data = (
  87. Matrix([self.inputs.data[i] for i in testing_indices]),
  88. Matrix([self.targets.data[i] for i in testing_indices])
  89. )
  90.  
  91. self.fold += 1
  92.  
  93. def _Training(self): # การ Train ข้อมูล
  94. for i in range(self.Input_Train.numRows()):
  95. for j in range(self.Input_Train.numCols()):
  96. self.Input_Train[i, j] = float(self.Input_Train[i, j])
  97. for i in range(self.Target_Train.numRows()):
  98. for j in range(self.Target_Train.numCols()):
  99. self.Target_Train[i, j] = float(self.Target_Train[i, j])
  100. self.Input_Train_T = self.Input_Train.transpose()
  101. self.Target_Train_T = self.Target_Train.transpose()
  102. self.weights_input_hidden = Matrix.uniform(-0.1, 0.1, (self.NumInput, self._hid_node))
  103. self.weights_hidden_output = Matrix.uniform(-0.1, 0.1, (self._hid_node, self.NumOutput))
  104.  
  105.  
  106. def _Plot(self): #Plot กราฟค่า losses เทียบกับ epochs
  107. temp_epochs = Array(self._epochs)
  108. temp_losses = Array(len(self.losses))
  109. for i in range(1, self._epochs+1):
  110. temp_epochs[i-1] = i
  111. for i in range(len(self.losses)):
  112. temp_losses[i] = self.losses[i]
  113. plt.plot(temp_epochs, temp_losses)
  114. plt.show()
  115.  
  116. def _Testing(self, curTrain_seq): #การ Test ข้อมูล
  117. for i in range(self.Input_Test.numRows()):
  118. for j in range(self.Input_Test.numCols()):
  119. self.Input_Test[i,j] = float(self.Input_Test[i,j])
  120. for i in range(self.Target_Test.numRows()):
  121. for j in range(self.Target_Test.numCols()):
  122. self.Target_Test[i,j] = float(self.Target_Test[i,j])
  123. self.Input_Test_T = self.Input_Test.transpose()
  124. self.Target_Test_T = self.Target_Test.transpose()
  125.  
  126. def Train_Model(self, fold): #4 #Train ข้อมูลไปตามจำนวน fold ที่ใส่ไว้ โดยเปลี่ยนชุดข้อมูลสำหรับการ Test ทุก ๆ รอบ
  127. accuracy = Alist()
  128. for i in range(fold):
  129. if i == 0:
  130. reset = True
  131. else:
  132. reset = False
  133. self._SeparateFold(fold, reset)
  134. acc = self._Training()
  135. accuracy.Append(acc)
  136. mean_acc = sum(accuracy)/len(accuracy)
  137. print("-"*50)
  138. print('Mean Accuracy = {0:.2f}%'.format(mean_acc*100))
  139.  
Success #stdin #stdout 2.18s 53880KB
stdin
Standard input is empty
stdout
Standard output is empty