Thursday, June 2, 2016

Fancy Neural Net Quickpicks for LottoMax and Lottery 6/49

I call it "Fancy" because it uses Neural Networks to come up with numbers but I don't think it's any better than normal quickpicks (but you never know right?).

I take 6/49 results from this page:http://lotto.bclc.com/winning-numbers/lotto-649-and-extra.html (at bottom of page, there's a "Download Lotto 6/49" button)
and LottoMax results from this page: http://lotto.bclc.com/winning-numbers/lotto-max-and-extra.html (at bottom of page, there's a "Download LOTTO MAX" button).

I create a neural network using python's neurolab library, using the below code in a file called create.py
 
import neurolab as nl

net = nl.net.newff([[0,1]] * 49, [49,49])

net.save("one.net")

This would create a network of  49 input nodes accepting values from 0 to 1, 49 hidden nodes, and 49 output nodes.

I then read from the 649.csv file (downloaded above). and as rows of 49 inputs of 0 with resulted numbers at the right indexes set to 1 to feed into the network, my target output for training are simply the next draw.  So one input row takes the row below it as target output.
and feed that into the created neural net ("one.net") for training.
I use the below code for "649.csv".

import neurolab as nl
import numpy as np


#read inputs and targets
f = open("649.csv",'r')
input = []
line_count = 0
for line in f.readlines():
 line = line.split(',')
 line_count += 1
 if line_count > 1: #skip line 1
  
  lineinput = [0] * 49
  for i in range(4,10):
   lineinput[int(line[i])-1] = 1
  input.append(lineinput)

#set target outputs for training to be from row 1 to end of list, skipping first row 0
target = input[1:len(input)]
#set siminput to be last row of input to use for simulation to predict output
siminput = [input[len(input)-1]]

#set input, make input one row less since the last row wouldn't have target output
input = input[0:len(input)-1]


#make it numpy
target = np.array(target)
input = np.array(input)
siminput = np.array(siminput)


def output(o):
 array = o[0]
 order = array.argsort()
 #ranks = order.argsort()
 
 for i in range(48,-1,-1):
  index = order[i] + 1
  print str(index) + "(" + str(int(array[index-1] * 1000)/1000.0) + ")",
  if i % 5 == 0:
   print
 print 
 
net = nl.load("one.net")
print "Lotto 649 number(probability)"
out = net.sim(siminput)
output(out)
while True:
 net = nl.load("one.net")
 print "Training..."
 error = net.train(input,target,epochs=10,show=1)
 
 # with line below we can specify training to stop when error[0] is smaller than goal it stops
 # but since we're inside a forever loop, there's no need to specify goal.
 # error = net.train(input,target,epochs=10,show=1,goal=0.01)
 
 # just a fail safe, just an extra save so that if we Ctrl-C while we're saving we'll have at least copy of latest network that works
 net.save("one.net.bk")
 net.save("one.net")
 
 print "Lotto 649 number(probability)"
 out = net.sim(siminput)
 
 #output for display purposes only
 output(out)
 

And i save this above code as run.py
 Then when to run the code. I run "python create.py" from command line, after that finishes, i run  "python run.py", and let it trains as long as i like to, each time it trains the error is reduced, and after 10 epochs (times it trains) it'll simulate using the updated trained network with the last row of the csv file as input and so we get output results of our fancy quickpick numbers.

Similarly, I use the below code for LOTTOMAX.csv.


import neurolab as nl
import numpy as np


#read inputs and targets
f = open("LOTTOMAX.csv",'r')
input = []
line_count = 0
for line in f.readlines():
 line = line.split(',')
 line_count += 1
 if line_count > 1: #skip line 1
  
  lineinput = [0] * 49
  for i in range(4,11):
   lineinput[int(line[i])-1] = 1
  input.append(lineinput)

#set target outputs for training to be from row 1 to end of list, skipping first row 0
target = input[1:len(input)]
#set siminput to be last row of input to use for simulation to predict output
siminput = [input[len(input)-1]]

#set input, make input one row less since the last row wouldn't have target output
input = input[0:len(input)-1]


#make it numpy
target = np.array(target)
input = np.array(input)
siminput = np.array(siminput)


def output(o):
 array = o[0]
 order = array.argsort()
 #ranks = order.argsort()
 
 for i in range(48,0,-1):
  index = order[i] + 1
  print str(index) + "(" + str(int(array[index-1] * 1000)/1000.0) + ")",
  if i % 5 == 0:
   print
 print 
 
net = nl.load("one.net")
print "Lotto MAX number(probability)"
out = net.sim(siminput)
output(out)
while True:
 net = nl.load("one.net")
 print "Training..."
 error = net.train(input,target,epochs=10,show=1)
 
 # with line below we can specify training to stop when error[0] is smaller than goal it stops
 # but since we're inside a forever loop, there's no need to specify goal.
 # error = net.train(input,target,epochs=10,show=1,goal=0.01)
 
 # just a fail safe, just an extra save so that if we Ctrl-C while we're saving we'll have at least copy of latest network that works
 net.save("one.net.bk")
 net.save("one.net")
 
 print "Lotto MAX number(probability)"
 out = net.sim(siminput)
 
 #output for display purposes only
 output(out)
 


And that's it.  You can copy the code above and put it in the same folder, with the downloadable .csv files from the bclc.com site and run these neural networks yourself or change the structure of the neural network configuration in create.py.

The information above was intended to for me so that if I ever factory reset my laptop, i'd have the information here to run my fancy neural net quickpicks for Lottery 6/49 and LottoMax.

HOW TO RUN this neural net.
 Install Anaconda by getting it from here: https://www.continuum.io/downloads
Choose Python 2.7 (at least that's what i use).
Once installed, from a terminal/command window run "pip install neurolab".
Also from a terminal/command window run "pip install numpy".
Copy the codes from above and put them in their respective filenames (create.py and run.py), create.py and run.py that tries to read from 649.csv should go in the same folder as 649.csv file
create.py and run.py that tries to read from LOTTOMAX.csv should go in the same folder as LOTTOMAX.csv file.
I would just create 2 separate folders (one for 649 files and one for LOTTOMAX files).
Download the 649.csv and LOTTOMAX.csv files from bclc.com and put them in the created folders.
While inside 649 folder for example, run "python create.py" then "python run.py".
And similarly for LOTTOMAX, while inside that folder you can run "python create.py" then "python run.py".