Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # calc_pi_by_nth_digit_pattern_recog_test_one.py
- # written by Kirk Lawrence
- import pdb
- import math
- import random
- import os, sys
- import ast
- from itertools import combinations
- # 5000 places of pi !!!
- pi = '''
- 3141592653589793...'''.strip()
- Lpi = len(pi)
- try:
- # Python2
- from Tkinter import *
- from urllib2 import urlopen
- except ImportError:
- # Python3
- from tkinter import *
- from urllib.request import urlopen
- def print_each_at_x(vars_list, nnn=22):
- sss = ''
- for var in vars_list:
- n = nnn
- if isinstance(var, list):
- var = ' '.join([str(v) for v in var])
- else:
- var = str(var)
- if len(var) > n - 4:
- n *= 2
- var = (var + ' ' * n)[:n-1] + ' '
- sss += var
- print(sss.rstrip()) # to print a new line at the end
- okay = 1
- root = Tk()
- root.withdraw()
- def cpbd():
- try:
- # t = root.selection_get(selection="CLIPBOARD")
- t = root.clipboard_get()
- return t
- except:
- return []
- # root.clipboard_clear()
- # root.destroy()
- 0
- def gym(): # for that pseudo neural exercise
- if prediction != yn:
- t = []
- if float(gain) > 99.5:
- bias[str(nodes)] = yn
- else:
- if yn:
- adj = +1
- else:
- adj = -1
- for v in nodes:
- t += [(weight_errors[(yn, v)], v)]
- weight_errors[(yn, v)] += 1
- t.sort(reverse=1)
- w0, node0 = t[0]
- w2, node2 = t[1]
- weights[node0] = weights[node2] + adj
- weight_errors[(yn, node0)] = weight_errors[(yn, node2)] - 1
- 0
- def guess(data): # guess before training for this data
- ttt = []
- for z in enumerate(data):
- s = '%d:%s'%z
- ttt.append(s)
- nodes.extend(list(combinations(ttt, 2))) ### length-3
- # print (len(nodes))
- t = len(nodes)
- if t != 10:
- print(t)
- try:
- p = bias[str(nodes)]
- return p, p
- except:
- 0
- t = []
- for v in nodes:
- try:
- t += [weights[v]]
- except:
- weights[v] = -1
- weight_errors[(0,v)] = 0
- weight_errors[(1,v)] = 0
- t += [weights[v]]
- ppp = sum(t)
- p = 0
- if ppp > -1:
- p = 1
- #print (ppp, t, yn == p)
- return p, ppp
- 0
- ###
- summary = 1
- # import numpy as np ### very much recommended 2018
- print ("Gathering Data, Please Wait...")
- right_answers = []
- wrong_answers = []
- for i in range(Lpi):
- right_answers += [(1, str(i) + pi[i])]
- wrong_answers += [(0, str(pi[i]))]
- const_wrong_answers = {}
- for i in range(10):
- const_wrong_answers[str(i)] = []
- for j in range(1,10):
- const_wrong_answers[str(i)] += [str((i + j) % 10)]
- print ('Pattern Recognition Sequence Is Ready To Commence...')
- if 1: # for testing
- random.seed(0)
- rush = 5000
- const_zzz = (right_answers + wrong_answers) * 10
- right = 0
- wrong = 0
- percent = '0.0'
- gain = 0.0
- prev_gain = gain
- weights = {}
- weight_errors = {}
- bias = {}
- zzz = const_zzz[:]
- random.shuffle(zzz)
- final = 0
- end = len(zzz)
- count_to_end = 0
- go = 0
- while zzz:
- z = zzz.pop(0)
- yn, data = z
- if not yn:
- str_digit = const_wrong_answers[data].pop(0)
- const_wrong_answers[data].append(str_digit)
- data += str_digit
- data = data.zfill(5)
- nodes = []
- prediction, ppp = guess(data)
- gym()
- if float(gain) > 99.5:
- if prev_gain == gain:
- count_to_end += 1
- else:
- count_to_end = 0
- prev_gain = gain
- if prediction == yn:
- right += 1
- else:
- wrong += 1
- if not (go+1)%rush:
- if okay: ### for other tests
- percent = '{:.1f}'.format((100.0/(right+wrong))*right)
- gain = max(gain,float(percent))
- t = str(go+1).zfill(8)
- print ('.')
- print_each_at_x([['. >>> actual vs prediction =', [yn, prediction], ppp] ,['::: Remaining', max(0,end-count_to_end)], final])
- print_each_at_x([['.',data , ':::','test#',t], ['::: wrong =',wrong], ['::: right =',right], [':::','{:.1f}'.format(gain)+' / '+percent+'%']])
- print ('.')
- right = 0
- wrong = 0
- if count_to_end >= end:
- if final:
- break
- else:
- final = 1
- count_to_end = 0
- continue
- zzz.append(z)
- go += 1
- print ('.','*'*10,'End Of Cycle','*'*10)
- print ('.')
- '''
- if summary:
- ttt = list(weights.items())
- ttt.sort(key=lambda x: (x[-1],x[0]), reverse=1)
- for t in ttt[:1000]:
- print ('.',t)
- print ('.')
- for t in ttt[-1000:]:
- print ('.',t)
- print ('.')
- print ('.',len(ttt))
- '''
- def calc_pi_digit(num):
- for pi_digit in range(10):
- del nodes[:]
- data = str(num) + str(pi_digit)
- data = data.zfill(5)
- prediction, ppp = guess(data)
- if prediction:
- break
- return pi_digit
- for n in range(Lpi):
- pi_digit = calc_pi_digit(n)
- wrong = ''
- sss = f"... the {n}th digit of pi is {pi[n]}"
- if pi[n] == str(pi_digit):
- s = "CORRECT !!!"
- else:
- s = "\t*** Wrong"
- wrong = f", not {pi_digit}"
- print(s + sss + wrong)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement