Spaces:
Sleeping
Sleeping
File size: 1,981 Bytes
8bc93b0 a114d65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import yfinance as yf
import os
def download_yahoo_data(ticker, start_date, end_date):
df = yf.download(ticker, start=start_date, end=end_date)
df = df[['Close']].dropna()
df.reset_index(inplace=True)
return df
def load_offline_csv(file):
df = pd.read_csv(file)
if 'Date' in df.columns:
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
return df
def prepare_sequences(data, window_size):
X, y = [], []
for i in range(len(data) - window_size):
X.append(data[i:i + window_size])
y.append(data[i + window_size])
return np.array(X), np.array(y)
def scale_data(df, column='Close'):
scaler = StandardScaler()
scaled = scaler.fit_transform(df[[column]])
return scaled, scaler
def inverse_transform(scaler, data):
return scaler.inverse_transform(data)
def compute_metrics(y_true, y_pred):
return {
"MSE": mean_squared_error(y_true, y_pred),
"MAE": mean_absolute_error(y_true, y_pred),
"R²": r2_score(y_true, y_pred)
}
def rolling_backtest(model, X, y, step=5):
predictions = []
for i in range(0, len(X) - step, step):
X_batch = X[i:i + step]
pred = model.predict(X_batch)
predictions.extend(pred.flatten())
return predictions
# core/utils.py
import matplotlib.pyplot as plt
def plot_loss_curve(train_losses, val_losses=None):
plt.figure(figsize=(10, 5))
plt.plot(train_losses, label='Train Loss', color='blue')
if val_losses is not None:
plt.plot(val_losses, label='Validation Loss', color='orange')
plt.title('Training Loss Curve')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig("loss_curve.png") # Saves the plot
plt.close() |