5折交叉验证,测试10次,取平均值:
1 import random 2 3 4 def read(path: str) -> tuple: 5 with open(path, "r") as f: 6 text = f.readlines() 7 M = [] 8 for row in text: 9 m = [] 10 substr = str.split(row, " ")[0] 11 for a in str.split(substr, ","): 12 m.append(a) 13 M.append(m) 14 return M, len(M), 5, len(M) // 5, 10 15 16 17 def calculateAccuracy(M: list, N: int, groupSize: int) -> float: 18 accuracyArray = [] 19 for groupNumber in range(N): 20 count = 0 21 for i in range(groupSize): 22 index = groupNumber * groupSize + i 23 if M[index][0] == M[index][1]: 24 count += 1 25 accuracy = count / groupSize 26 accuracyArray.append(accuracy) 27 # print("[%2d / %2d = %.3f]" % (count, groupSize, accuracy)) 28 return accuracyArray[N - 1] 29 30 31 if __name__ == '__main__': 32 path = "predict_data.txt" 33 M, size, N, groupSize, verifyTime = read(path) 34 accuracyArray = [] 35 print("Verifying...") 36 for n in range(verifyTime): 37 random.shuffle(M) 38 averageAccuracy = calculateAccuracy(M, N, groupSize) 39 accuracyArray.append(averageAccuracy) 40 print("%2d times, accuracy: %.3f" % (n + 1, averageAccuracy)) 41 print("averageAccuracy = %.5f" % (sum(accuracyArray) / len(accuracyArray)))