# # b_pattern_recog_plus.py v2

Jun 28th, 2022 (edited)
498
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # b_pattern_recog_plus.py
2.
3. # written by Kirk Lawrence + https://pastebin.com/pqyNaFQi
4.
5. import math
6. import random
7. import os, sys
8. import ast
9. from itertools import combinations
10.
11. try:
12.     # Python2
13.     from Tkinter import *
14.     from urllib2 import urlopen
15. except ImportError:
16.     # Python3
17.     from tkinter import *
18.     from urllib.request import urlopen
19.
20. okay  = 1
21.
22. root = Tk()
23. root.withdraw()
24.
25. def cpbd():
26.     try:
27.         # t = root.selection_get(selection="CLIPBOARD")
28.         t = root.clipboard_get()
29.         return t
30.     except:
31.         return []
32.     # root.clipboard_clear()
33.     # root.destroy()
34. 0
35.
36. def gym(): # for that pseudo neural exercise
37.     if prediction != yn:
38.         if yn:
39.             for v in nodes:
40.                     weights[v] += 1
41.         else:
42.             for v in nodes:
43.                     weights[v] -= 1
44.
45. 0
46. def guess(): # guess before training for this data
47.     ttt = []
48.     for z in enumerate(data):
49.         s = '%d:%s'%z
50.         ttt.append(s)
51.
52.     nodes.extend(list(combinations(ttt, 9))) ### length-3
53.     # print (len(nodes))
54.
55.     ppp = 0
56.     for v in nodes:
57.         if v not in weights:
58.             weights[v] = 0
59.         else:
60.             ppp += weights[v]
61.
62.     p = 0
63.     if ppp > -1:
64.         p = 1
65.     return p, ppp
66. 0
67.
68. ###
69.
70. summary = 1
71.
72. rush = 1000
73.
74. # import numpy as np ### very much recommended 2018
75.
76. print ('Pattern Recognition Sequence Is Ready To Commence...')
77.
78. if 1: # for testing
79.
80.     # random.seed(0)
81.
82.     right = 0
83.     wrong = 0
84.
85.     prev_gain = 0
86.
87.     percent = '0.0'
88.
89.     gain = 0.0
90.
91.     weights = {}
92.
93.     go = 0
94.
95.     while not go:
96.         full = cpbd()
97.
98.         if '# yn -- combos -- at' in full:
99.             full = full.split('# yn -- combos -- at')[-1].strip().splitlines()
100.
101.             if len(full) > 50000:
102.
103.                 random.shuffle(full)
104.
105.                 full = full[:50000]
106.                 zzz = full[:]
107.
108.                 end = len(zzz)
109.                 count_to_end = 0
110.         else:
111.             full = []
112.
113.         while full:
114.             while zzz:
115.                 z = zzz.pop(0)
116.
117.                 yn, data, at = ast.literal_eval(z)
118.
119.                 data = ''.join(data)
120.                 at = ''.join(at)
121.
122.                 nodes = []
123.
124.                 prediction, ppp = guess()
125.
126.                 gym()
127.
128.                 if prediction == yn:
129.                     right += 1
130.                     count_to_end += 1
131.                 else:
132.                     wrong += 1
133.                     count_to_end = 0
134.                 if not (go+1)%rush:
135.                     percent = '{:.1f}'.format(((right-prev_gain)*100.0)/rush)
136.                     gain = max(gain,float(percent))
137.                     if okay: ### for other tests
138.
139.                         t = str(go+1).zfill(8)
140.                         print ('.')
141.                         print ('.','>>> actual vs prediction =',[yn, prediction],ppp,' '*(7-len(str(ppp))),'::: Remaining', max(0,end-count_to_end))
142.                         print ('.',data,':::','test#',t,'::: wrong =',wrong,'::: right =',right,':::','{:.1f}'.format(gain)+' / '+percent+'%')
143.                         print ('.',at)
144.                         print ('.')
145.
146.                     prev_gain = right
147.
148.                     if count_to_end >= end:
149.                         zzz = []
150.                         break
151.                 zzz.append(z)
152.
153.                 go += 1
154.
155.             full = []
156.
157.
158. print ('.','*'*10,'End Of Cycle','*'*10)
159. print ('.')
160.
161. if summary:
162.     ttt = list(weights.items())
163.     ttt.sort(key=lambda x: (x[-1],x[0]), reverse=1)
164.     for t in ttt[:1000]:
165.         print ('.',t)
166.     print ('.')
167.     for t in ttt[-1000:]:
168.         print ('.',t)
169.     print ('.')
170.     print ('.',len(ttt))