Spaces:
Sleeping
Sleeping
import pickle | |
import os | |
import numpy as np | |
from datetime import datetime | |
import pandas as pd | |
def save_models(models, model_type, directory): | |
print('Saving models...') | |
for i, model in enumerate(models): | |
with open(f'{directory}/{model_type}_FOLD_{i+1}.pkl', 'wb') as file: | |
pickle.dump(model, file) | |
def load_models(directory): | |
print('Loading models...') | |
models = [] | |
# List all files in the directory | |
files = os.listdir(directory) | |
pkl_files = [file for file in files if file.endswith('.pkl')] | |
for file in pkl_files: | |
with open(os.path.join(directory, file), 'rb') as file: | |
model = pickle.load(file) | |
models.append(model) | |
return models | |
def get_predictions(models, X_test): | |
print('Forecasting test data...') | |
preds = [] | |
for model in models: | |
preds.append(post_process(model.predict(X_test))) | |
return np.mean(preds, axis=0) | |
def post_process(predictions): | |
predictions = predictions.clip(0) | |
return predictions | |
def save_results(dataframe, file_name): | |
print('Saving results...') | |
today_date = datetime.now().strftime("%Y-%m-%d") | |
dataframe.to_excel(f'demand_predictions/{file_name}_{today_date}.xlsx', index=False) | |
def save_parquet(dataframe, path): | |
dataframe.to_parquet(path, index=False) | |
def load_parquet(path): | |
return pd.read_parquet(path) | |