upload the ai trainer
This commit is contained in:
@@ -1,49 +1,54 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
import datapuller
|
||||||
import os
|
import os
|
||||||
from sklearn.model_selection import train_test_split
|
from sklearn.model_selection import train_test_split
|
||||||
from keras import Sequential, layers, optimizers
|
from keras import Sequential, layers, optimizers
|
||||||
|
|
||||||
# Get the CWD for pathing due to being called from C# now
|
def TrainAI():
|
||||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
# Pull New Data
|
||||||
DATA_DIR = os.path.join(SCRIPT_DIR, "data")
|
datapuller.pull()
|
||||||
|
|
||||||
# Load the dataset
|
# Get the CWD for pathing due to being called from C# now
|
||||||
dataset = pd.read_parquet(os.path.join(DATA_DIR, "stocks.parquet"))
|
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
X = dataset.drop('Volatility_5', axis=1)
|
DATA_DIR = os.path.join(SCRIPT_DIR, "data")
|
||||||
Y = dataset['Volatility_5']
|
|
||||||
|
|
||||||
# Show the datatypes
|
# Load the dataset
|
||||||
print(dataset.dtypes)
|
dataset = pd.read_parquet(os.path.join(DATA_DIR, "stocks.parquet"))
|
||||||
|
X = dataset.drop('Volatility_5', axis=1)
|
||||||
|
Y = dataset['Volatility_5']
|
||||||
|
|
||||||
# Split out the test and train
|
# Show the datatypes
|
||||||
train_features, test_features, train_labels, test_labels = train_test_split(X, Y, test_size=0.2)
|
print(dataset.dtypes)
|
||||||
|
|
||||||
# Create a normalizer to nomralize the data
|
# Split out the test and train
|
||||||
normalizer = layers.Normalization(axis=-1)
|
train_features, test_features, train_labels, test_labels = train_test_split(X, Y, test_size=0.2)
|
||||||
normalizer.adapt(np.array(train_features))
|
|
||||||
|
|
||||||
# Start with a linear model
|
# Create a normalizer to nomralize the data
|
||||||
dnn_linear_model = Sequential([
|
normalizer = layers.Normalization(axis=-1)
|
||||||
|
normalizer.adapt(np.array(train_features))
|
||||||
|
|
||||||
|
# Start with a linear model
|
||||||
|
dnn_linear_model = Sequential([
|
||||||
layers.Input(shape=(9,)), # Explicitly tell Keras there are 9 features
|
layers.Input(shape=(9,)), # Explicitly tell Keras there are 9 features
|
||||||
normalizer,
|
normalizer,
|
||||||
layers.Dense(64, activation='relu'),
|
layers.Dense(64, activation='relu'),
|
||||||
layers.Dense(64, activation='relu'),
|
layers.Dense(64, activation='relu'),
|
||||||
layers.Dense(1)
|
layers.Dense(1)
|
||||||
])
|
])
|
||||||
|
|
||||||
# Configure the model
|
# Configure the model
|
||||||
dnn_linear_model.compile(
|
dnn_linear_model.compile(
|
||||||
optimizer=optimizers.Adam(learning_rate=0.001),
|
optimizer=optimizers.Adam(learning_rate=0.001),
|
||||||
loss='mean_absolute_error'
|
loss='mean_absolute_error'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Show the summary before training the model
|
# Show the summary before training the model
|
||||||
dnn_linear_model.summary()
|
dnn_linear_model.summary()
|
||||||
|
|
||||||
# Train the model
|
# Train the model
|
||||||
Training_Data = dnn_linear_model.fit(
|
Training_Data = dnn_linear_model.fit(
|
||||||
train_features,
|
train_features,
|
||||||
train_labels,
|
train_labels,
|
||||||
epochs=100,
|
epochs=100,
|
||||||
@@ -51,23 +56,23 @@ Training_Data = dnn_linear_model.fit(
|
|||||||
verbose=1,
|
verbose=1,
|
||||||
# Calculate validation results on 20% of the training data.
|
# Calculate validation results on 20% of the training data.
|
||||||
validation_split = 0.2
|
validation_split = 0.2
|
||||||
)
|
)
|
||||||
|
|
||||||
# Predict
|
# Predict
|
||||||
test_predictions = dnn_linear_model.predict(test_features).flatten()
|
test_predictions = dnn_linear_model.predict(test_features).flatten()
|
||||||
a = plt.axes(aspect='equal')
|
a = plt.axes(aspect='equal')
|
||||||
plt.scatter(test_labels, test_predictions)
|
plt.scatter(test_labels, test_predictions)
|
||||||
plt.xlabel('True Values')
|
plt.xlabel('True Values')
|
||||||
plt.ylabel('Predictions')
|
plt.ylabel('Predictions')
|
||||||
lims = [0, 50]
|
lims = [0, 50]
|
||||||
plt.xlim(lims)
|
plt.xlim(lims)
|
||||||
plt.ylim(lims)
|
plt.ylim(lims)
|
||||||
_ = plt.plot(lims, lims)
|
_ = plt.plot(lims, lims)
|
||||||
|
|
||||||
|
|
||||||
test_results = dnn_linear_model.evaluate(
|
test_results = dnn_linear_model.evaluate(
|
||||||
test_features, test_labels, verbose=0
|
test_features, test_labels, verbose=0
|
||||||
)
|
)
|
||||||
|
|
||||||
# Save the model
|
# Save the model
|
||||||
dnn_linear_model.save(os.path.join(DATA_DIR, "model.keras"))
|
dnn_linear_model.save(os.path.join(DATA_DIR, "model.keras"))
|
||||||
Reference in New Issue
Block a user