text
stringlengths
1
93.6k
"Overfitting Score": overfitting_score,
}
)
pbar.update(1)
# Create a DataFrame with model statistics
stats_df = pd.DataFrame(model_stats)
stats_df = stats_df.sort_values("OOF R² Score", ascending=False).reset_index(
drop=True
)
# Add overfitting indicator
stats_df["Overfit"] = stats_df["Overfitting Score"].apply(
lambda x: "Yes" if x > 0.05 else "No"
)
# Print the table
print("\nModel Performance Summary:")
print(tabulate(stats_df, headers="keys", tablefmt="pretty", floatfmt=".4f"))
print("\nCalculating ensemble weights...")
ensemble_weights = calculate_ensemble_weights(models, X_test, y_test)
print(f"Ensemble weights: {ensemble_weights}")
print("Making ensemble predictions...")
ensemble_predictions = weighted_ensemble_predict(
[model for _, model in models], X, ensemble_weights
)
print(f"Predicting future data for the next {future_days} days...")
future_predictions = []
for name, model in models:
print(f" Making future predictions with {name} model...")
future_pred = predict_future(model, X[-1], scaler, future_days)
future_predictions.append(future_pred)
future_predictions = np.mean(future_predictions, axis=0)
print("Inverse transforming predictions...")
close_price_scaler = MinMaxScaler(feature_range=(0, 1))
close_price_scaler.fit(data["Close"].values.reshape(-1, 1))
ensemble_predictions = close_price_scaler.inverse_transform(
ensemble_predictions.reshape(-1, 1)
)
future_predictions = close_price_scaler.inverse_transform(
future_predictions.reshape(-1, 1)
)
# Ensure ensemble_predictions matches the length of the actual data
ensemble_predictions = ensemble_predictions[-len(data) :]
print("Plotting results...")
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(20, 24))
# Price prediction plot
plot_data = data.iloc[-len(ensemble_predictions) :]
future_dates = pd.date_range(
start=plot_data.index[-1] + pd.Timedelta(days=1), periods=future_days
)
ax1.plot(plot_data.index, plot_data["Close"], label="Actual Price", color="blue")
ax1.plot(
plot_data.index,
ensemble_predictions,
label="Predicted Price",
color="red",
linestyle="--",
)
ax1.plot(
future_dates,
future_predictions,
label="Future Predictions",
color="green",
linestyle="--",
)
# Add price indications for every day (initially invisible)
annotations = []
for i, (date, price) in enumerate(zip(plot_data.index, ensemble_predictions)):
ann = ax1.annotate(
f"${price[0]:.2f}",
(date, price[0]),
xytext=(0, 10),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=8,
alpha=0.7,
visible=False,
)
annotations.append(ann)
for i, (date, price) in enumerate(zip(future_dates, future_predictions)):
ann = ax1.annotate(
f"${price[0]:.2f}",
(date, price[0]),
xytext=(0, -10),
textcoords="offset points",
ha="center",
va="top",