fork download
  1.  
  2. """
  3. learnstock.py
  4. 帰納的学習による10桁のパターン探索
  5. パターンA・B・C・Dの比較
  6. """
  7.  
  8. import random
  9.  
  10. # グローバル変数
  11. SETSIZE = 100 # 学習データの数
  12. CNO = 10 # パターンの桁数
  13. GENMAX = 10000 # 解候補を生成する回数
  14. SEED = 7 # 乱数の初期値
  15.  
  16.  
  17. # 学習データと教師データを読み込む関数
  18. def readdata(data, teacher):
  19. for i in range(SETSIZE):
  20. line = input()
  21. data[i] = [int(num) for num in line.split()]
  22. teacher[i] = data[i][CNO]
  23.  
  24.  
  25. # 解候補のスコアを計算する関数
  26. def calcscore(data, teacher, answer):
  27. score = 0
  28.  
  29. for i in range(SETSIZE):
  30. point = 0
  31.  
  32. # 学習データと解候補の一致度を調べる
  33. for j in range(CNO):
  34. if answer[j] == 2:
  35. point += 1
  36. elif answer[j] == data[i][j]:
  37. point += 1
  38.  
  39. # 教師データに対する正解数を数える
  40. if point == CNO and teacher[i] == 1:
  41. score += 1
  42. elif point != CNO and teacher[i] == 0:
  43. score += 1
  44.  
  45. return score
  46.  
  47.  
  48. # パターンを検査データに適用する関数
  49. def predict(pattern, testdata):
  50. result = []
  51.  
  52. for row in testdata:
  53. point = 0
  54.  
  55. for j in range(CNO):
  56. if pattern[j] == 2:
  57. point += 1
  58. elif pattern[j] == row[j]:
  59. point += 1
  60.  
  61. # 10桁すべてが一致すれば1、そうでなければ0
  62. if point == CNO:
  63. result.append(1)
  64. else:
  65. result.append(0)
  66.  
  67. return result
  68.  
  69.  
  70. # 表2.2の検査データ
  71. testdata = [
  72. [0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
  73. [0, 0, 1, 0, 1, 0, 0, 1, 0, 0],
  74. [0, 0, 1, 0, 0, 1, 0, 0, 1, 1],
  75. [1, 1, 1, 1, 0, 1, 0, 1, 0, 1],
  76. [1, 0, 1, 0, 0, 1, 0, 1, 0, 1],
  77. [1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
  78. [0, 1, 0, 1, 1, 1, 1, 1, 0, 1],
  79. [0, 1, 1, 0, 1, 0, 1, 1, 1, 1],
  80. [0, 0, 1, 0, 1, 0, 1, 0, 1, 1],
  81. [1, 1, 1, 0, 1, 0, 1, 0, 1, 1]
  82. ]
  83.  
  84. # 表2.2に記載された正しい結果
  85. correct = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]
  86.  
  87.  
  88. # メイン処理
  89. data = [[] for i in range(SETSIZE)]
  90. teacher = [0 for i in range(SETSIZE)]
  91.  
  92. bestscore = 0
  93. bestanswer = [0 for i in range(CNO)]
  94.  
  95. # パターンA・Bを設定する
  96. patternA = [2, 2, 0, 2, 2, 2, 0, 2, 0, 2]
  97. patternB = [2, 2, 0, 2, 2, 2, 2, 2, 0, 2]
  98.  
  99. # パターンC・Dの候補を保存する
  100. candidate_patterns = {}
  101.  
  102. # 乱数を初期化する
  103. random.seed(SEED)
  104.  
  105. # 学習データを読み込む
  106. readdata(data, teacher)
  107.  
  108. # パターンA・Bの学習データに対するスコア
  109. scoreA = calcscore(data, teacher, patternA)
  110. scoreB = calcscore(data, teacher, patternB)
  111.  
  112. print("パターンAのスコア =", scoreA)
  113. print("パターンBのスコア =", scoreB)
  114.  
  115. # 解候補を生成して評価する
  116. for i in range(GENMAX):
  117. answer = [random.randint(0, 2) for j in range(CNO)]
  118.  
  119. score = calcscore(data, teacher, answer)
  120.  
  121. # 全体の最良解を更新する
  122. if score > bestscore:
  123. bestscore = score
  124. bestanswer = answer.copy()
  125.  
  126. # スコア84点・100点以外の候補を記録する
  127. if score != 84 and score != 100:
  128. pattern = tuple(answer)
  129.  
  130. # 同じパターンを重複して登録しない
  131. if pattern not in candidate_patterns:
  132. candidate_patterns[pattern] = score
  133.  
  134. # スコアの高い順に並べる
  135. sorted_patterns = sorted(
  136. candidate_patterns.items(),
  137. key=lambda item: item[1],
  138. reverse=True
  139. )
  140.  
  141. # A・Bと同じパターンは候補から除く
  142. patternA_tuple = tuple(patternA)
  143. patternB_tuple = tuple(patternB)
  144.  
  145. sorted_patterns = [
  146. item for item in sorted_patterns
  147. if item[0] != patternA_tuple
  148. and item[0] != patternB_tuple
  149. ]
  150.  
  151. # C・Dを選ぶ
  152. patterns = {
  153. "A": patternA,
  154. "B": patternB
  155. }
  156.  
  157. if len(sorted_patterns) >= 1:
  158. patterns["C"] = list(sorted_patterns[0][0])
  159.  
  160. if len(sorted_patterns) >= 2:
  161. patterns["D"] = list(sorted_patterns[1][0])
  162.  
  163. # A・B・C・Dの予測結果を表示する
  164. print("\n表2.2 検査データの予測結果")
  165. print("番号 正解 A予測 B予測 C予測 D予測")
  166.  
  167. predictions = {}
  168.  
  169. for name, pattern in patterns.items():
  170. predictions[name] = predict(pattern, testdata)
  171.  
  172. for i in range(len(testdata)):
  173. values = []
  174.  
  175. for name in ["A", "B", "C", "D"]:
  176. if name in predictions:
  177. values.append(str(predictions[name][i]))
  178. else:
  179. values.append("-")
  180.  
  181. print(
  182. i + 1, correct[i],
  183. values[0], values[1], values[2], values[3]
  184. )
  185.  
  186. # 各パターンの正解数を表示する
  187. print("\n検査データ10例の正解数")
  188.  
  189. for name in ["A", "B", "C", "D"]:
  190. if name in predictions:
  191. count = 0
  192.  
  193. for i in range(len(correct)):
  194. if predictions[name][i] == correct[i]:
  195. count += 1
  196.  
  197. print(name, ":", count, "/ 10")
  198.  
  199. # 探索全体の最良解を表示する
  200. print("\n探索全体の最良解")
  201. print(bestanswer, ":score=", bestscore)
  202. # your code goes here
Success #stdin #stdout 0.93s 15372KB
stdin
1 0 0 0 0 0 1 0 0 1     1
0 1 0 1 0 1 1 1 0 1     1
0 1 0 0 0 1 1 0 1 0     0
1 0 0 1 1 0 1 0 0 1     1
1 0 0 1 1 0 1 1 1 1     0
0 0 0 0 0 0 1 1 0 0     1
1 1 1 1 0 0 1 1 0 1     0
0 1 1 1 0 1 1 1 0 1     0
0 0 1 1 0 1 1 1 0 0     0
1 1 1 0 0 0 0 1 1 0     0
0 0 1 1 1 0 0 0 1 0     0
1 0 0 0 1 0 1 0 1 1     0
0 1 0 0 1 0 0 0 1 1     0
0 1 1 0 1 0 0 1 1 1     0
0 0 1 0 1 0 1 0 1 0     0
0 1 1 0 1 1 1 1 1 0     0
1 1 0 0 1 1 1 1 0 0     1
1 0 0 0 1 1 1 0 0 0     1
0 0 1 0 0 1 1 1 0 0     0
0 1 1 0 0 1 1 1 0 1     0
1 1 0 1 0 1 1 1 1 1     0
1 1 1 0 1 1 1 0 1 0     0
0 1 1 0 1 1 1 0 0 1     0
1 1 1 1 1 1 0 0 0 1     0
1 1 1 0 1 0 1 1 1 0     0
1 1 1 0 1 0 0 0 0 0     0
0 0 0 1 1 1 0 0 1 0     0
1 0 1 0 0 0 1 1 1 0     0
0 0 1 1 1 1 0 1 1 0     0
1 1 0 1 0 0 0 0 0 1     1
0 1 1 1 0 1 0 1 1 1     0
1 1 0 0 0 1 1 0 0 1     1
1 1 0 1 1 1 1 1 1 1     0
1 0 1 0 1 1 0 1 0 1     0
1 1 0 1 1 0 0 1 1 0     0
0 0 1 0 1 0 1 1 0 1     0
0 1 1 1 1 0 0 1 0 0     0
0 1 1 0 0 1 1 0 0 0     0
0 0 0 1 0 1 0 0 0 0     1
1 1 1 0 0 0 0 1 0 0     0
1 0 1 1 1 1 0 0 1 0     0
0 1 0 0 1 0 1 1 0 0     1
1 1 1 0 1 1 0 0 0 0     0
0 0 1 0 1 0 1 1 0 1     0
1 0 0 1 0 1 1 1 0 0     1
1 1 1 0 1 1 0 0 1 0     0
0 1 0 1 1 1 1 1 0 1     1
0 1 1 0 0 1 0 0 1 0     0
0 0 0 1 1 1 0 1 1 1     0
1 0 1 0 1 0 1 1 1 0     0
0 1 1 0 0 0 1 0 0 0     0
0 0 1 0 1 0 0 0 1 1     0
1 0 1 0 0 1 1 0 0 0     0
0 0 0 1 0 0 1 0 0 1     1
0 0 1 1 1 1 1 1 1 0     0
0 0 1 0 1 1 1 0 1 1     0
0 1 1 0 1 0 0 0 0 0     0
0 0 1 1 0 0 0 1 1 1     0
0 1 0 1 1 1 0 0 1 0     0
1 1 1 1 0 0 1 0 1 1     0
1 1 1 0 0 1 0 1 1 1     0
0 1 0 0 0 0 1 0 0 0     1
0 0 0 0 1 0 0 0 0 1     1
1 1 0 0 1 1 0 1 0 1     1
0 0 0 1 1 0 1 0 0 1     1
1 1 1 1 1 0 1 1 0 1     0
1 1 1 1 0 0 0 0 0 0     0
1 0 1 1 1 0 1 0 0 1     0
0 1 0 1 0 1 0 1 1 0     0
1 0 0 0 1 0 0 0 0 0     1
0 1 1 1 0 0 1 1 1 0     0
0 1 1 1 0 0 0 0 1 1     0
1 0 1 1 0 1 1 1 1 1     0
1 1 0 0 1 0 1 0 1 0     0
0 1 1 0 0 1 0 1 0 1     0
0 1 0 0 0 0 1 1 1 0     0
0 1 1 0 1 0 0 0 1 1     0
0 1 0 1 1 1 1 1 0 1     1
1 0 0 1 0 0 1 1 1 1     0
1 1 0 1 1 1 1 1 0 0     1
0 0 0 0 0 1 1 1 1 1     0
0 0 0 0 1 0 0 0 0 1     1
1 1 0 1 0 1 1 0 0 1     1
0 0 1 0 0 1 0 0 0 1     0
1 0 1 1 0 0 0 0 0 0     0
1 0 1 1 1 0 0 0 0 0     0
1 0 0 1 1 1 0 1 1 1     0
0 0 1 1 0 0 1 0 0 1     0
0 0 1 1 1 1 1 0 1 1     0
0 1 0 1 0 1 0 0 0 1     1
1 0 1 1 1 1 1 0 1 1     0
1 1 1 1 1 1 0 0 1 1     0
0 1 0 0 0 0 1 0 1 1     0
1 0 1 1 1 0 0 0 0 0     0
0 1 1 1 0 0 0 0 1 1     0
0 1 1 0 1 1 1 0 0 0     0
1 1 0 0 0 0 0 1 0 0     1
1 0 1 0 0 0 1 0 0 0     0
0 0 1 1 1 0 0 0 0 0     0
0 1 0 0 1 0 0 1 1 1     0
stdout
パターンAのスコア = 84
パターンBのスコア = 100

表2.2 検査データの予測結果
番号  正解  A予測  B予測  C予測  D予測
1 1 1 1 0 1
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 0 0 0 0 0
6 0 0 0 0 0
7 1 0 1 0 0
8 0 0 0 0 0
9 0 0 0 0 0
10 0 0 0 0 0

検査データ10例の正解数
A : 9 / 10
B : 10 / 10
C : 8 / 10
D : 9 / 10

探索全体の最良解
[2, 2, 0, 2, 2, 2, 0, 2, 0, 2] :score= 84