Cycles/xgboost/plot_results.py
2025-05-29 18:28:53 +08:00

112 lines
3.6 KiB
Python

import numpy as np
import dash
from dash import dcc, html
import plotly.graph_objs as go
import threading
def display_actual_vs_predicted(y_test, test_preds, timestamps, n_plot=200):
import plotly.offline as pyo
n_plot = min(n_plot, len(y_test))
plot_indices = timestamps[:n_plot]
actual = y_test[:n_plot]
predicted = test_preds[:n_plot]
trace_actual = go.Scatter(x=plot_indices, y=actual, mode='lines', name='Actual')
trace_predicted = go.Scatter(x=plot_indices, y=predicted, mode='lines', name='Predicted')
data = [trace_actual, trace_predicted]
layout = go.Layout(
title='Actual vs. Predicted BTC Close Prices (Test Set)',
xaxis={'title': 'Timestamp'},
yaxis={'title': 'BTC Close Price'},
legend={'x': 0, 'y': 1},
margin={'l': 40, 'b': 40, 't': 40, 'r': 10},
hovermode='closest'
)
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig)
def plot_target_distribution(y_train, y_test):
import plotly.offline as pyo
trace_train = go.Histogram(
x=y_train,
nbinsx=100,
opacity=0.5,
name='Train',
marker=dict(color='blue')
)
trace_test = go.Histogram(
x=y_test,
nbinsx=100,
opacity=0.5,
name='Test',
marker=dict(color='orange')
)
data = [trace_train, trace_test]
layout = go.Layout(
title='Distribution of Target Variable (Close Price)',
xaxis=dict(title='BTC Close Price'),
yaxis=dict(title='Frequency'),
barmode='overlay'
)
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig)
def plot_predicted_vs_actual_log_returns(y_test, test_preds, timestamps=None, n_plot=200):
import plotly.offline as pyo
import plotly.graph_objs as go
n_plot = min(n_plot, len(y_test))
actual = y_test[:n_plot]
predicted = test_preds[:n_plot]
if timestamps is not None:
x_axis = timestamps[:n_plot]
x_label = 'Timestamp'
else:
x_axis = list(range(n_plot))
x_label = 'Index'
# Line plot: Actual vs Predicted over time
trace_actual = go.Scatter(x=x_axis, y=actual, mode='lines', name='Actual')
trace_predicted = go.Scatter(x=x_axis, y=predicted, mode='lines', name='Predicted')
data_line = [trace_actual, trace_predicted]
layout_line = go.Layout(
title='Actual vs. Predicted Log Returns (Test Set)',
xaxis={'title': x_label},
yaxis={'title': 'Log Return'},
legend={'x': 0, 'y': 1},
margin={'l': 40, 'b': 40, 't': 40, 'r': 10},
hovermode='closest'
)
fig_line = go.Figure(data=data_line, layout=layout_line)
pyo.plot(fig_line, filename='log_return_line_plot.html')
# Scatter plot: Predicted vs Actual
trace_scatter = go.Scatter(
x=actual,
y=predicted,
mode='markers',
name='Predicted vs Actual',
opacity=0.5
)
# Diagonal reference line
min_val = min(np.min(actual), np.min(predicted))
max_val = max(np.max(actual), np.max(predicted))
trace_diag = go.Scatter(
x=[min_val, max_val],
y=[min_val, max_val],
mode='lines',
name='Ideal',
line=dict(dash='dash', color='red')
)
data_scatter = [trace_scatter, trace_diag]
layout_scatter = go.Layout(
title='Predicted vs Actual Log Returns (Scatter)',
xaxis={'title': 'Actual Log Return'},
yaxis={'title': 'Predicted Log Return'},
showlegend=True,
margin={'l': 40, 'b': 40, 't': 40, 'r': 10},
hovermode='closest'
)
fig_scatter = go.Figure(data=data_scatter, layout=layout_scatter)
pyo.plot(fig_scatter, filename='log_return_scatter_plot.html')