Friday, June 3, 2016

Fancy Neural Network to predict Powerball white ball numbers.

This post is also posted on GIMP LEARN forum - Anything goes

(Note: You'll need to install Anaconda Python 2.7, neurolab library, numpy library and termcolor library)

Fancy Neural Network to predict Powerball white ball numbers based on historical results.

How to run the network.

First create a folder on your desktop or whevever, let's call this folder POWER_NN.
Download http://www.powerball.com/powerball/winnums-text.txt
and save it in the POWER_NN folder as winnums-text.txt.
This file has powerball's historical results.

Now create a file in POWER_NN folder and name it "create.py" and paste this code into it.

import neurolab as nl
net = nl.net.newff([[0,1]] * 69, [69,69])
net.save("one.net")
 
Now create a file in POWER_NN folder and name it  "run.py" and paste this code into it

import neurolab as nl
import numpy as np
from termcolor import colored

#read inputs and targets
f = open("winnums-text.txt",'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] * 69
  for i in range(1,6):
   lineinput[int(line[i])-1] = 1
  input.append(lineinput)
input.reverse() #powerball shows results in reverse order from newest to oldest so we will reverse this list

#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(68,-1,-1):
  index = order[i] + 1
  print colored(str(index),"green") + colored("(" + str(int(array[index-1] * 1000)/1000.0) + ")","red"),
  if i % 5 == 0:
   print
 print 
 
net = nl.load("one.net")
print colored("Powerball","blue") + colored(" number","green") + colored("(probability)","red")
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 colored("Powerball","blue") + colored(" number","green") + colored("(probability)","red")
 out = net.sim(siminput)
 
 #output for display purposes only
 output(out)
 

Now we have all the files we need.
You''ll have to update winnums-text.txt file whenever there's a new powerball lottery result out to keep the information up-to-date.

Now, first we run from a command/terminal window: python create.py
This will create the initial network (untrained).

Now we run from a command/terminal window: python run.py
and let that run, as it trains more the error will reduce and it's prediction becomes more accurate/fancy based on historical data.

 It'll train 10 epochs/times then simulate once show its prediction once. and repeat this process forever.

You can always Control-C to exit the run process and the trained network up to that point will be saved in one.net (neural net file).

If you have bad luck and happen to Control-C while it's trying to save one.net you'll have a corrupted one.net but that's okay because you can always copy the file one.net.bk over to one.net if program complains able not being able to read one.net.

The output shows numbers from highest probability down to lowest one... so even though it shows all 69 numbers, you might only want to play the first 5 (like chosen powerball whiteballs).