Na podstawie artyułu i zawartego w nim kodu, napisz mi kod pythonowy wraz z polskimi opisami i komentarzami. Dodatkowo kod ma wyświetlac poszczególne etapy działania na terminalu oraz zapisywać wszystko do pliku, łacznie z efektami nauczania i testowania, czyli musisz to rozbudować. Oto artykuł z kodem, który w niektórych miejsach moze być błędny wiec do napraw:  Chcę by przy okazji to był materiał do nauki: How to Guess Accurately 3 Lottery Numbers Out of 6 using LSTM Model
Roi Polanitzer
Roi Polanitzer

·
Follow

9 min read
·
May 27, 2022
81


17




Photo Credit: www.istockphoto.com
Motivation
In order to predict at least 3 lottery numbers out of 6 (variable y) lottery numbers in an Israeli general lottery game, I chose the Israeli general lottery games dataset that was sourced from Mifal HaPais. This dataset was based on the results of Israeli general lottery games between September 1968 and May 2022. It has many characteristics of learning, and the dataset can be downloaded from here.

Artificial Neural Networks (Multilayer Perceptrons)

Artificial Neural Networks can also be referred to as Multilayer perceptrons. The perceptron model was created in 1958 by American psychologist Frank Rosenblatt. Its singular nature allows it to adapt to basic binary patterns through a series of inputs, simulating the learning patterns of a human-brain. A Multilayer perceptron is the classic neural network model consisting of more than 2 layers.

Recurrent Neural Networks
Recurrent Neural Networks (RNNs) were invented to be used around predicting sequences. LSTM (Long short-term memory) is a popular RNN algorithm with many possible use cases:


When to use:
One to one: a single input mapped to a single output.
e.g — Image Classification
One to many: a single input mapped to a sequence of outputs.
e.g — Image captioning (multiple words from a single image)
Many to one: A sequence of inputs produces a single output.
e.g — Sentiment Analysis (binary output from multiple words)
Many to many: A sequence of inputs produces a sequence of outputs.
e.g — Video classification (splitting the video into frames and labeling each frame separately)
Business Understanding
The Israeli general lottery game called Loto. Lotto is a weekly game where the participant chooses 6 numbers out of 37 and an additional one number out of 7. Mifal HaPais draws 6 numbers of 37 and 1 number of 7, and the maximum prize is paid for matching all of them.

Various options allow the user to bet double, play random numbers, 5 numbers plus a random number, or all combinations of 7–12 numbers. The drawings are held once on Tuesday and once on Saturday, with occasional drawings on Thursday.

The prize pool is a minimum of ₪5,000,000 and a maximum of ₪80,000,000.

Data Understanding
First, let’s import the relevant libraries and packages:

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense, Bidirectional, Dropout
Second, Let’s load the required dataset using pandas’s read CSV function.

df = pd.read_csv(“IsraeliLottery.csv”)
We can take a closer look at the data took help of “head()”function of pandas library which returns first five observations.

df.head()

Similarly “tail()” returns last five observations.

df.tail()

The dataset provides the lottery results information. It includes 4,047 records and 8 fields.

print(df.shape)
(4047, 8)

After we have loaded the dataset, we might want to know a little bit more about it. We can check attributes names and datatypes using info().

data.info()

No variable column has null/missing values.

The describe() function in pandas is convenient in getting various summary statistics. This function returns the count, mean, standard deviation, minimum and maximum values and the quantiles of the data.

df.describe()

The count, mean, min and max rows are self-explanatory. The std shows the standard deviation, and the 25%, 50% and 75% rows show the corresponding percentiles.

Data Processing
There are several features that we do not need, such as “Game” and “Date”, so, we will drop them.

df.drop([‘Game’, ‘Date’], axis=1, inplace=True)
df.head()

Deep learning algorithms expect all input features to vary in a similar way, and ideally to have a mean of 0, and a variance of 1. We must re-scale our data so that it fulfills these requirements.

scaler = StandardScaler().fit(df.values)
transformed_dataset = scaler.transform(df.values)
transformed_df = pd.DataFrame(data=transformed_dataset, index=df.index)
Let’s check out our scaled data:

transformed_df.head()

Now, let’s define several variables:

# All our games
number_of_rows = df.values.shape[0]
number_of_rows
4047

# Amount of games we need to take into consideration for prediction
window_length = 7
window_length
7

# Balls counts
number_of_features = df.values.shape[1]
number_of_features
6

Next, let’s crate X and y for each row in our scaled data. It should have format for Keras LSTM model (rows, window size, balls)

X = np.empty([ number_of_rows — window_length, window_length, number_of_features], dtype=float)
y = np.empty([ number_of_rows - window_length, number_of_features], dtype=float)
for i in range(0, number_of_rows-window_length):
    X[i] = transformed_df.iloc[i : i+window_length, 0 : number_of_features]
    y[i] = transformed_df.iloc[i+window_length : i+window_length+1, 0 : number_of_features]
Let’s check out X shape:

X.shape
(4040, 7, 6)

Let’s check out y shape:

y.shape
(4040, 6)

Lets’ check out our first scaled sample (which made of 7 consecutive lottery games):

X[0]

Lets’ check out our first scaled label:

y[0]

Lets’ check out the second scaled sample (which made of 7 consecutive lottery games):

X[1]

Lets’ check out our second scaled label:

y[1]

Modeling
First, let’s initialise the RNN

model = Sequential()
Let’s add the input layer and the LSTM layer

model.add(Bidirectional(LSTM(240, input_shape = (window_length, number_of_features), return_sequences = True)))
Let’s add a first Dropout layer in order to reduce overfitting

model.add(Dropout(0.2))
Let’s add a second LSTM layer

model.add(Bidirectional(LSTM(240, input_shape = (window_length, number_of_features), return_sequences = True)))
Let’s add a second Dropout layer

model.add(Dropout(0.2))
Then, let’s add a third LSTM layer

model.add(Bidirectional(LSTM(240, input_shape = (window_length, number_of_features), return_sequences = True)))
Now, let’s add a fourth LSTM layer

model.add(Bidirectional(LSTM(240, input_shape = (window_length, number_of_features), return_sequences = False)))
Next, let’s add a dense layer

model.add(Dense(59))
Finally, let’s add the last output layer

model.add(Dense(number_of_features))
Now, let's compile the RNN

from tensorflow import keras
from tensorflow.keras.optimizers import Adam
model.compile(optimizer=Adam(learning_rate=0.0001), loss ='mse', metrics=['accuracy'])
Next, let’s train our LSTM model

model.fit(x=X, y=y, batch_size=100, epochs=300, verbose=2)


Evaluation
Let’s take the results of the last 8 Israeli general lottery games:

to_predict = df.tail(8)
to_predict

Let's remove the last raw from the 8 last games

to_predict.drop([to_predict.index[-1]],axis=0, inplace=True)
to_predict

We got exactly the 7 last games before the May 24th, 2022 lottety game.

Now, let’s take the results of the May 24th, 2022 lottety game and place it into a variable called “prediction”

prediction = df.tail(1)
prediction

Next, we have to change the format of our last 7 games from dataframe to np.array in order to insert them into our model

to_predict = np.array(to_predict)
to_predict

Then, we have to re-scale those 7 games

scaled_to_predict = scaler.transform(to_predict)
scaled_to_predict

Now, let’s predict the results (i.e., the 6 numbers) of the May 24th, 2022 lottety game based on those 7 games:

y_pred = model.predict(np.array([scaled_to_predict]))
print(“The predicted numbers in the last lottery game are:”, scaler.inverse_transform(y_pred).astype(int)[0])
The predicted numbers in the last lottery game are: [ 5 9 12 20 23 35]

Let’s see what were the real results of the May 24th, 2022 lottety game:

prediction = np.array(prediction)
print(“The actual numbers in the last lottery game were:”, prediction[0])
The actual numbers in the last lottery game were: [ 6 10 13 20 23 35]

3 number sout of 6 numbers, not bad at all!!! especially considering the fact that there was not supposed to be a model within the data, that is, the numbers had to be 100% random.