-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add streamlit, evaluating predictions
- Loading branch information
1 parent
ce36415
commit 61ad91a
Showing
7 changed files
with
149 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,55 @@ | ||
import pandas as pd | ||
import json | ||
from sklearn.preprocessing import MinMaxScaler | ||
|
||
def prepare_data(file_path, date_col='transaction_date', time_col='transaction_time'): | ||
data = pd.read_excel(file_path) | ||
|
||
# Convert date column to datetime | ||
data[date_col] = pd.to_datetime(data[date_col]) | ||
|
||
# Set the date column as the index | ||
data.set_index(date_col, inplace=True) | ||
|
||
# Extract transaction hour if the time column exists | ||
if time_col in data.columns: | ||
data['transaction_hour'] = data[time_col].apply(lambda x: x.hour) | ||
|
||
return data | ||
|
||
def generate_test_data(file_path, output_file='test_payload.json', seq_length=10): | ||
# Prepare data | ||
data = prepare_data(file_path) | ||
|
||
# Resample to daily transaction quantities | ||
daily_data = data.resample('D')['transaction_qty'].sum() | ||
|
||
# Scale the data | ||
scaler = MinMaxScaler() | ||
scaled_data = scaler.fit_transform(daily_data.values.reshape(-1, 1)) | ||
|
||
# Generate sequences | ||
sequences = [ | ||
scaled_data[i:i + seq_length].flatten().tolist() | ||
for i in range(len(scaled_data) - seq_length) | ||
] | ||
|
||
# Create a sample payload with the first sequence | ||
if sequences: | ||
test_data = {"data": sequences[0]} # Taking the first sequence for testing | ||
|
||
# Save the test payload to a JSON file | ||
with open(output_file, 'w') as f: | ||
json.dump(test_data, f) | ||
|
||
print(f"Test data saved to {output_file}") | ||
else: | ||
print("Not enough data to generate sequences. Please ensure the dataset is sufficient.") | ||
|
||
if __name__ == "__main__": | ||
data = prepare_data('../data/cafecast_data.xlsx') | ||
print(data.info()) | ||
file_path = 'data/cafecast_data.xlsx' | ||
data = prepare_data(file_path) | ||
print(data.info()) | ||
|
||
# Generate and save test data | ||
generate_test_data(file_path, output_file='test_payload.json', seq_length=10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
import numpy as np | ||
from sklearn.metrics import mean_absolute_error, mean_squared_error | ||
import matplotlib.pyplot as plt | ||
|
||
def evaluate_predictions(actuals, predictions): | ||
actuals = np.array(actuals) | ||
predictions = np.array(predictions) | ||
|
||
mae = mean_absolute_error(actuals, predictions) | ||
rmse = np.sqrt(mean_squared_error(actuals, predictions)) | ||
mape = np.mean(np.abs((actuals - predictions) / actuals)) * 100 | ||
|
||
return { | ||
"MAE": mae, | ||
"RMSE": rmse, | ||
"MAPE": mape | ||
} | ||
|
||
def plot_predictions(actuals, predictions): | ||
plt.figure(figsize=(10, 6)) | ||
plt.plot(actuals, label="Actual", marker='o') | ||
plt.plot(predictions, label="Predicted", marker='x') | ||
plt.xlabel("Time Steps") | ||
plt.ylabel("Values") | ||
plt.title("Actual vs Predicted") | ||
plt.legend() | ||
plt.grid(True) | ||
plt.show() | ||
|
||
def naive_forecast(actuals): | ||
return actuals[:-1] # Predict the last observed value for all steps | ||
|
||
def moving_average_forecast(actuals, window=3): | ||
return [np.mean(actuals[i-window:i]) for i in range(window, len(actuals))] | ||
|
||
if __name__ == "__main__": | ||
# Example usage: | ||
# Replace these with your actual test and prediction data | ||
actual_values = [100, 105, 110, 120] # Example actual values | ||
predicted_values = [98, 107, 115, 118] # Example predicted values | ||
|
||
# Evaluate metrics | ||
metrics = evaluate_predictions(actual_values, predicted_values) | ||
print("Evaluation Metrics:", metrics) | ||
|
||
# Plot actual vs predicted values | ||
plot_predictions(actual_values, predicted_values) | ||
|
||
# Baselines | ||
naive = naive_forecast(actual_values) | ||
moving_avg = moving_average_forecast(actual_values, window=2) | ||
print("Naive Forecast:", naive) | ||
print("Moving Average Forecast:", moving_avg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
{ | ||
"batch_size": 61, | ||
"epochs": 214, | ||
"num_units": 289 | ||
} | ||
[ | ||
{ | ||
"batch_size": 61, | ||
"epochs": 214, | ||
"num_units": 289 | ||
}, | ||
{ | ||
"num_units": 50, | ||
"batch_size": 40, | ||
"epochs": 100 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"data": [0.11921032649962032, 0.11009870918754744, 0.13515565679574798, 0.06150341685649202, 0.10098709187547456, 0.06909643128321946, 0.1169324221716021, 0.12224753227031132, 0.0736522399392559, 0.15945330296127563]} |