Advertisement
here2share

# b_pattern_recog_plus.py

Jun 28th, 2022 (edited)
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. # b_pattern_recog_plus.py @481
  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.                 weightsY[v] += 1
  41.         else:
  42.             for v in nodes:
  43.                 weightsN[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, 3))) ### length @ 5 = 792
  53.     # print len(nodes)
  54.    
  55.     pY = 0
  56.     pN = 0
  57.     for v in nodes:
  58.         if v not in weightsY:
  59.             weightsY[v] = 0
  60.             weightsN[v] = 0
  61.         else:
  62.             pY += weightsY[v]
  63.             pN += weightsN[v]
  64.            
  65.     p = 0
  66.     if pY > pN:
  67.         p = 1
  68.     return p, pY-pN
  69. 0
  70.  
  71. ###
  72.  
  73. summary = 1
  74.  
  75. rush = 1000
  76.  
  77. # import numpy as np ### very much recommended 2018
  78.  
  79. print ('Pattern Recognition Sequence Is Ready To Commence...')
  80.  
  81. if 1: # for testing
  82.  
  83.     # random.seed(0)
  84.  
  85.     right = 0
  86.     wrong = 0
  87.    
  88.     prev_gain = 0
  89.            
  90.     percent = '0.0'
  91.    
  92.     gain = 0.0
  93.    
  94.     weightsY = {}
  95.     weightsN = {}
  96.                
  97.     go = 0
  98.    
  99.     while not go:
  100.         full = cpbd()
  101.        
  102.         if '# yn -- combos -- at' in full:
  103.             full = full.split('# yn -- combos -- at')[-1].strip().splitlines()
  104.        
  105.             if len(full) > 50000:
  106.  
  107.                 random.shuffle(full)
  108.                
  109.                 full = full[:20000]
  110.                 zzz = full[:]
  111.                
  112.                 end = len(zzz)
  113.                 count_to_end = 0
  114.         else:
  115.             full = []
  116.        
  117.         while full:
  118.             while zzz:
  119.                 z = zzz.pop(0)
  120.                
  121.                 yn, data, at = ast.literal_eval(z)
  122.                
  123.                 data = ''.join(data)
  124.                 at = ''.join(at)
  125.                
  126.                 nodes = []
  127.                
  128.                 prediction, ppp = guess()
  129.                
  130.                 gym()
  131.                    
  132.                 if prediction == yn:
  133.                     right += 1
  134.                     count_to_end += 1
  135.                 else:
  136.                     wrong += 1
  137.                     count_to_end = 0
  138.                 if not (go+1)%rush:
  139.                     percent = '{:.1f}'.format(((right-prev_gain)*100.0)/rush)
  140.                     gain = max(gain,float(percent))
  141.                     if okay: ### for other tests
  142.                        
  143.                         t = str(go+1).zfill(8)
  144.                         print ('.')
  145.                         print ('.','>>> actual vs prediction =',[yn, prediction],ppp,' '*(7-len(str(ppp))),'::: Remaining', max(0,end-count_to_end))
  146.                         print ('.',data,':::','test#',t,'::: wrong =',wrong,'::: right =',right,':::','{:.1f}'.format(gain)+' / '+percent+'%')
  147.                         print ('.',at)
  148.                         print ('.')
  149.                        
  150.                     prev_gain = right
  151.                    
  152.                     if count_to_end >= end:
  153.                         zzz = []
  154.                         break
  155.                 zzz.append(z)
  156.                        
  157.                 go += 1
  158.            
  159.             full = []
  160.            
  161.  
  162. print ('.','*'*10,'End Of Cycle','*'*10)
  163. print ('.')
  164.        
  165. if summary:
  166.     ttt = list(weightsY.items())
  167.     ttt.sort(key=lambda x: (x[-1],x[0]), reverse=1)
  168.     for t in ttt[:1000]:
  169.         print ('.',t)
  170.     print ('.')
  171.     for t in ttt[-1000:]:
  172.         print ('.',t)
  173.     print ('.')
  174.     print ('.',len(ttt))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement