111 lines
3.7 KiB
Python
111 lines
3.7 KiB
Python
import numpy as np # helps with the math
|
|
import matplotlib.pyplot as plt # to plot error during training
|
|
import tensorflow as tf
|
|
|
|
from rich.progress import Progress
|
|
|
|
from data.get_data import pull_training_data, normalize_training_data
|
|
from data.db_connect import Database
|
|
from data.build_weather import get_weather, get_sun_and_moon_phase
|
|
from data.stats_importer import Importer
|
|
|
|
from neuralnet.neuralnetwork import NeuralNetwork
|
|
|
|
# input data
|
|
inputs = np.array([[0, 0, 1, 0],
|
|
[0, 0, 1, 1],
|
|
[0, 0, 0, 0],
|
|
[1, 1, 0, 0],
|
|
[1, 1, 1, 1],
|
|
[1, 1, 0, 1]])
|
|
# output data
|
|
outputs = np.array([[0], [0], [0], [1], [1], [1]])
|
|
|
|
if __name__ == '__main__':
|
|
db_file = "./database/baseball.db"
|
|
db_conn = Database(db_file)
|
|
|
|
query = "SELECT game_date, game_number, park_id FROM games ORDER BY game_date"
|
|
all_games = db_conn.selectall(query)
|
|
|
|
compiled_training_data = None
|
|
compiled_training_results = None
|
|
|
|
with Progress() as p_bar:
|
|
p_bar_val = p_bar.add_task("Getting Data ...", total=len(all_games))
|
|
for game in all_games:
|
|
p_bar.update(p_bar_val, advance=1)
|
|
game_result, training_data = pull_training_data(db_conn, str(game[0]), game[1], game[2])
|
|
|
|
training_data = normalize_training_data(training_data)
|
|
|
|
if compiled_training_results is None:
|
|
compiled_training_data = np.array([training_data])
|
|
compiled_training_results = np.array([game_result])
|
|
else:
|
|
compiled_training_data = np.append(compiled_training_data, np.array([training_data]), axis=0)
|
|
compiled_training_results = np.append(compiled_training_results, np.array([game_result]), axis=0)
|
|
|
|
ctd_len = len(compiled_training_data)
|
|
ctr_len = len(compiled_training_results)
|
|
|
|
td = compiled_training_data[:ctd_len-100]
|
|
tdt = compiled_training_data[ctd_len-100:]
|
|
|
|
tr = compiled_training_results[:ctr_len-100]
|
|
trt = compiled_training_results[ctr_len-100:]
|
|
|
|
nn = NeuralNetwork(len(compiled_training_data[0]))
|
|
nn.train(td, tr)
|
|
nn.summary()
|
|
predictions = nn.predict(np.array(tdt))
|
|
|
|
total_num = 0
|
|
accurate_num = 0
|
|
for pred, act in zip(predictions, trt):
|
|
total_num += 1
|
|
pred = round(pred[0] * 1000) / 1000.0
|
|
guess = round(pred)
|
|
print(f"Pred: {pred} -> Gue: {guess} -> Res: {act} -> Cor: {guess == act}")
|
|
|
|
accurate_num += int(guess==act)
|
|
|
|
print(f"Total: {total_num} -> Accu: {accurate_num} -> Perc: {accurate_num/total_num}")
|
|
#for index in range(len(trt)):
|
|
# print(f"pred: {nn.predict(np.array([tdt[index]]))} : act : {trt[index]}")
|
|
|
|
"""
|
|
build_db_path = "./data/sql/build_db .sql"
|
|
fill_parks_path = "./data/sql/prefill_parks.sql"
|
|
fill_teams_path = "./data/sql/prefill_teams.sql"
|
|
|
|
db_conn.run_sql_file(build_db_path)
|
|
db_conn.run_sql_file(fill_parks_path)
|
|
db_conn.run_sql_file(fill_teams_path)
|
|
|
|
imp = Importer(db_conn)
|
|
imp.parse_all_data("./data/stats/", "./data/stats/imported/")
|
|
"""
|
|
|
|
"""
|
|
else:
|
|
# create neural network
|
|
NN = NeuralNetwork(inputs, outputs)
|
|
# train neural network
|
|
NN.train()
|
|
|
|
# create two new examples to predict
|
|
example = np.array([[1, 1, 1, 0]])
|
|
example_2 = np.array([[0, 0, 1, 1]])
|
|
|
|
# print the predictions for both examples
|
|
print(NN.predict(example), ' - Correct: ', example[0][0])
|
|
print(NN.predict(example_2), ' - Correct: ', example_2[0][0])
|
|
|
|
# plot the error over the entire training duration
|
|
plt.figure(figsize=(15,5))
|
|
plt.plot(NN.epoch_list, NN.error_history)
|
|
plt.xlabel('Epoch')
|
|
plt.ylabel('Error')
|
|
plt.savefig('plot.png')
|
|
""" |