machine learning Experiment Psuedocode only

This is all psuedocode
Disclaimer: None of the code has been run, tested or even typed into a code editor.  This post is to hold the experiment idea, for future use.


Machine Learning Experiment (1-27-19)

The idea is to try this out, so I can see how machine learning works more.
I understand there is a lot of data reconciliation and probability involved.
The libraries currently used in python are advanced and extremely well built I am sure, but I need to see the interaction, see what's happening and how.
This may be completely wrong, but once again.... Why not?

After completing the initial post, I have the suspicion that I am in way over my
head. It's way past my bedtime, I gotta sleep.  So not just a grain of salt with this one, bring a whole bucket.

1:  SQLite does not support holding a list
     solution, either use an INTEGER and increment
                    or use a string, and python can count the length of that string
     StackOverflow Q&A

2:  Reference Material
     learn sql the hard way: Zed Shaw
     StackOverflow rounding decimal places Q & A
     StackOverflow run a process x times Q & A
     SQL website python: Create Table
     Tutorial SQL UPDATE QUERY
    **<http://WWW>Simple probability examples**

3: Future use, reference material
    Bayes Theorom (Wikipedia)  

4:  fix the python variables:
     Should be----
         cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))
 
    reference:
    StackOverflow use python variables in Query 
 
table:
database: PythonRandom.db --------------------random_table---------------------------------
|  total Odds   | integer start = 1  |
|               |  python update     |
----------------------------------------------------------------------------
|  total Evens  | integer start = 1  |
|               |  python update     |
----------------------------------------------------------------------------
| P_of_Odd      | start= .5          |
| current       |  python update     |
----------------------------------------------------------------------------
| P_of_Even     | start = .5         |
| current       |  python update     |
----------------------------------------------------------------------------
| id            | integer start = 1  |
| also:         |  python update     |
| which run     |                    |
---------------------------------------------------------------------------- 

#########
  update only*   run_sequence on update
               database:   RunSequence.db 

----------------run_sequence-------------------------------------
| id            | integer start = 1  |
| also:         |  python update     |
| which run     |                    |
----------------------------------------------------------------------------
| P_of_Odd      | start= .50         |
| current       |  python update     |
----------------------------------------------------------------------------
| P_of_Even     | start = .50        |
| current       |  python update     |
----------------------------------------------------------------------------
| P_of_OBayes   | start = .50        |
|               |  python update     |
----------------------------------------------------------------------------
| P_of_EBayes   | start = .50        |
|               |  python update     |
---------------------------------------------------------------------------



The python psuedocode:

########## PsuedoCode #############

import random
import sqlite
import math

#Database previously created name= PythonRandom
conn = sqlite3.connect('PythonRandom.db') 
 cursor = conn.cursor()
 
""" 

CREATE TABLE random_table (
    id INTEGER PRIMARY KEY,
    P_of_O integer,
    P_of_E integer,
    total_odds integer,
    total_evens integer 

);
 
""" 
        
#Database previously created name = RunSequence 
conn2 = sqlite3.connect('RunSequence.db')
 cursor2 = conn2.cursor() 
""" 
CREATE TABLE run_sequence (
    id INTEGER PRIMARY KEY,
    P_of_O integer,
    P_of_E integer,
    P_of_OBayes integer,
    P_of_EBayes integer 

); 
 """  

def get_random():
    x = random(1, 100)
    return x

def is_it_even(x):
    if x % 2:
       returnTrue
    else:
       return False

def get_table_integers():
    """
    from the SQLite table -- random_table -- get and return
    the numbers needed for calculation of probability
    """
    total_odd = cursor.execute("select 'total_odds' from random_table")
    total_even= cursor.execute("select 'total_evens' from
                                                         random_table")                       
    total_runs = cursor.execute("select 'total_tested' from random_table")
    P_of_O = cursor.execute("select 'P_of_O' from random_table")
    P_of_E = cursor.execute("select 'P_of_E' from random_table")
    return total_odd, total_even, total_runs, P_of_O, P_of_E
    
    
    

def update_sql(odd=False, even=False):
    """
    increment table data depending on even or false
    use get_table_integers to get a list containing:
 
    odd_total, even_total, P_of_E, P_of_O, total_tested
  
    P_of_E *Probablility of Even*
    P_of_O *Probability of Odd*
    return statement in get_table_integers:
               return total_odd, total_even, total_runs, P_of_O, P_of_E
    Notes:
    the random_table will be refreshed each sample run
    the run_sequence should be altered for the Bayes Formula
    the Bayes formula is what determines the probability from 
    multiple sequences of probability events.... I THINK....  that's why
    I have not included that part yet. 

    """
    total_odd, total_even, total_runs, P_of_O, P_of_E = get_table_integers()
    new_total = total_runs + 1
    cursor.execute("UPDATE random_table SET total_tested = new_total;") 
    if even: 
        new_even_total = total_even + 1
        new_even_prob = math.round( new_even_total / new_total, 2)                             
        cursor.execute(
      "UPDATE random_table SET total_evens = new_even_total;")
 
          cursor.execute("UPDATE random_table SET P_of_E = new_even_prob ") 
           
 
           # Here is where Bayes numbers will need to be updated also 
          #  updating cursor2 should be by selecting id of run.  The Bayes formula
          #  looks like it needs an old probabilty factored by the new probabilty.
          # When creating the table, 20 id's should be made, each with a starting probability
          # for all probabilities of 50%
        
          Bayes_Even = .50 #<-- this will be the big fancy formula
         value1 = new_even_prob
         value2 = Bayes_Even

           cursor2.execute(UPDATE run_sequence
SET P_of_E = value1, P_of_EBayes = value2
WHERE id = total_runs;)
 
            #######  also cursor 2, find what sequence we are on to get old prob
            ####### add the Bayes prob into table that is produced in multiple runs
            ###  The Bayes prob should change depending on how many times we update
            ###  should become more accurate?? 
 
     if odd:
     new_odd_total = total_odd + 1
        new_odd_prob = math.round(new_total_odds/ new_total, 2)  
        cursor.execute( "UPDATE random_table SET total_odds = new_odd_total" )
        cursor.execute("UPDATE random_table SET P_of_O = new_odd_prob ")
        cursor2.execute("INSERT INTO  run_sequence (id, P_of_O, P_of_E)
                                       VALUES ( total_runs, new_odd_prob,  P_of_E );")
 
        # Bayes stuff... See above in if even: comments
        # Shouldn't the odd probability go down as even goes up?? 
           Bayes_Odd = .50 #<-- this will be the big fancy formula
         value1 = new_odd_prob
         value2 = Bayes_Odd


        cursor2.execute("UPDATE run_sequence SET P_of_O= value1, P_of_OBayes
        = value2  WHERE id = total_runs;")
 

          
def database_update()
     integer = get_random()
     even_odd = is_it_even(integer)
     if even_odd:
        update_sql(even=True, odd=False)
     else:
        update_sql(odd=True, even=False)

def get_best_pick(arg):
     """
     from which random number we are on,
        first time, second time ---  ninety-ninth
     which is python random more likely to produce:
     odd or even?
     Math round ,  rounds down, so there should be a margin of error
    
     """
    odd_prob = cursor2.execute( "select P_of_O from run_sequence
                                                     where id = arg;") 
    even_prob = cursor2.execute(" select P_of_E from run_sequence
                                                      where id = arg;")
    if odd_prob > even_prob:
       string_odd = "Probablility that it will be odd:\n\t" + str(odd_prob)
       return string_odd
   elif even_prob > odd_prob:
       string_even = "Probablitity that it will be even:\n\t" + str(even_prob) 
       return string_even
   else:
       return "Chances are equal for it to be odd or even"

def run_samples():
      """
      twenty at a time, update the tables to reflect random's choices
      new probability should be calculated from old probablity and current
      This is where Baye's theorom comes in.  That part is going to wait until
      I have all this working first.
      """
    # reset random_table  -- total_tested, which is used
                #for run_sequence id lookup
                #total of evens + total of odds should be used for prob/totals??
    #  on each run, random_table just holds the integers needed for run_sequence
    # update to calculate the probability happening for each run 1-20
                #run_sequence is not reset, only updated

    # Bayes math not included yet.

    # the other database should read which random_table id we are on to locate
         # which values to update in it's 20 id's.

      cursor.execute(cursor.execute("UPDATE random_table
                               SET total_tested = 1;") 

      for carrot in range(20):
            database_update()
            



Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame