Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # calc_pi_by_nth_digit_pattern_recog_test_three.py
- # combination of 3 nodes instead of 2
- # written by Kirk Lawrence
- import pdb
- import math
- import random
- import os, sys
- import ast
- from itertools import combinations
- 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:
- for node in nodes:
- weights[node, yn] += 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, 3)))
- # print (len(nodes))
- ttt = {0: 0, 1: 0}
- for node in nodes:
- try:
- ttt[0] += weights[node, 0]
- ttt[1] += weights[node, 1]
- except:
- weights[node, 0] = 0
- weights[node, 1] = 0
- p = 0
- if ttt[0] < ttt[1]:
- p = 1
- return p
- 0
- ###
- summary = 1
- # import numpy as np ### very much recommended 2018
- # 5000 places of pi
- pi = '''31415926535897932...'''.strip()
- Lpi = len(pi)
- 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 = guess(data)
- gym()
- if prediction == yn:
- right += 1
- count_to_end += 1
- else:
- wrong += 1
- count_to_end = 0
- 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]] ,['::: 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 or int(t) > 500000:
- break
- 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 = 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