├── README.md ├── data structure.PNG ├── datasets └── jogo.xlsx └── mega.py /README.md: -------------------------------------------------------------------------------- 1 | # Machine-Learning-Lottery-Prediction 2 | Machine Learning code in Python/Keras. 3 | 4 | This is just an exercise to put in practice the knowledge learned in Deep Learning Specialization at Coursera (Andrew Ng). 5 | 6 | The task chosen was to predict the next game in a brazilian lottery called Mega Sena (6 balls drawn from a spining bowl with 60 balls numbered from 1 to 60). As the propability is equal for each ball, the neural network can't predict. But who knows if there is a pattern...? lol 7 | 8 | In "mega.py", the code tries to predict just the first number of a game. 9 | -------------------------------------------------------------------------------- /data structure.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/engineer1982/Machine-Learning-Lottery-Prediction/eb94ff24378fd86f436c13230ec965331545fb82/data structure.PNG -------------------------------------------------------------------------------- /datasets/jogo.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/engineer1982/Machine-Learning-Lottery-Prediction/eb94ff24378fd86f436c13230ec965331545fb82/datasets/jogo.xlsx -------------------------------------------------------------------------------- /mega.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import h5py 3 | import pandas as pd 4 | 5 | import keras 6 | from keras.models import Sequential 7 | from keras.layers import Dense, Dropout, Activation 8 | 9 | # for each game (draw), we have 60 balls numbered from 1 to 60 inside a bowl. Then, 6 of them are chosen and the result is shown. eg: 14-31-32-05-59-41 10 | # for this purpose, we will try to predict just the first number 11 | 12 | # functions 13 | 14 | def ldata(m,t): 15 | 16 | # load the data from all the games happened until 31/12/2017 17 | # m is the number of training examples ; t is the number of test examples 18 | # the output is extracting just the first number of the each game. 19 | 20 | 21 | Location = r'datasets\jogo.xlsx' 22 | jogo = pd.read_excel(Location) 23 | 24 | dataset = {} 25 | 26 | X_train = np.zeros((4,m-t)) 27 | Y_train = np.zeros((60,m-t)) 28 | X_test = np.zeros((4,t)) 29 | Y_test = np.zeros((60,t)) 30 | 31 | X_train[0][:] = jogo.values[0][0:m-t] #number of the game 32 | X_train[1][:] = jogo.values[3][:m-t] # day of the game 33 | X_train[2][:] = jogo.values[4][0:m-t] # month of the game 34 | X_train[3][:] = jogo.values[5][0:m-t] #year of the game 35 | X_train = X_train.T; 36 | 37 | X_test[0][:] = jogo.values[0][m-t:m] 38 | X_test[1][:] = jogo.values[3][m-t:m] 39 | X_test[2][:] = jogo.values[4][m-t:m] 40 | X_test[3][:] = jogo.values[5][m-t:m] 41 | X_test = X_test.T; 42 | 43 | Y_train = jogo.values[7][0:m-t]; #the number of the first ball of the game 44 | Y_train = Y_train.T; 45 | Y_train = Y_train.astype(int) 46 | Y_train = keras.utils.to_categorical(Y_train, num_classes=61) #turn it into on hot vector 47 | 48 | Y_test = jogo.values[7][m-t:m]; 49 | Y_test = Y_test.T; 50 | Y_test = Y_test.astype(int) 51 | Y_test = keras.utils.to_categorical(Y_test, num_classes=61) 52 | 53 | 54 | dataset["X_train"] = X_train 55 | dataset["X_test"] = X_test 56 | dataset["Y_train"] = Y_train 57 | dataset["Y_test"] = Y_test 58 | 59 | return dataset 60 | 61 | def modelo(m, t, epoc, bat, v): 62 | 63 | # m is the number of training examples ; t is the number of test examples 64 | # epoc for epochs; bat for batches; 65 | # v for verbose in model fit. (0 = no progress is shown; 1 = progress bar; 2 = shows just the final loss and accuracy for each epoch) 66 | 67 | #testing the model, I run: m1, l1, c1, y1 = modelo(1900, 10, 10, 32,2); 68 | 69 | dados = ldata(m, t); 70 | 71 | i_shape = dados["X_train"].shape[1] #input shape 72 | o_shape = dados["Y_train"].shape[1] #output shape 73 | 74 | x_train = dados['X_train'] 75 | y_train = dados['Y_train'] 76 | x_test = dados['X_test'] 77 | y_test = dados['Y_test'] 78 | 79 | model = Sequential() 80 | model.add(Dense(150, activation='relu', input_dim=i_shape)) 81 | #model.add(Dropout(0.5)) 82 | model.add(Dense(100, activation='relu')) 83 | #model.add(Dropout(0.5)) 84 | model.add(Dense(80, activation='relu')) 85 | #model.add(Dropout(0.5)) 86 | model.add(Dense(o_shape, activation='softmax')) 87 | 88 | model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) 89 | 90 | model.fit(x_train, y_train, epochs=epoc, batch_size=bat, verbose=v, validation_split=0.1, shuffle=True) 91 | 92 | loss_and_metrics = model.evaluate(x_test, y_test, batch_size=bat) 93 | 94 | classes = model.predict(x_test, batch_size=bat) 95 | 96 | return model, loss_and_metrics, classes, y_test 97 | 98 | --------------------------------------------------------------------------------