|
|
import pandas as pdimport matplotlib.pyplot as pltimport syssys.path.append("../")from model import Kronos, KronosTokenizer, KronosPredictor
def plot_prediction(kline_df, pred_df): pred_df.index = kline_df.index[-pred_df.shape[0]:] sr_close = kline_df['close'] sr_pred_close = pred_df['close'] sr_close.name = 'Ground Truth' sr_pred_close.name = "Prediction"
close_df = pd.concat([sr_close, sr_pred_close], axis=1)
fig, ax = plt.subplots(1, 1, figsize=(8, 4))
ax.plot(close_df['Ground Truth'], label='Ground Truth', color='blue', linewidth=1.5) ax.plot(close_df['Prediction'], label='Prediction', color='red', linewidth=1.5) ax.set_ylabel('Close Price', fontsize=14) ax.legend(loc='lower left', fontsize=12) ax.grid(True)
plt.tight_layout() plt.show()
# 1. Load Model and Tokenizertokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base")model = Kronos.from_pretrained("NeoQuasar/Kronos-small")
# 2. Instantiate Predictorpredictor = KronosPredictor(model, tokenizer, device="cuda:0", max_context=512)
# 3. Prepare Datadf = pd.read_csv("./data/XSHG_5min_600977.csv")df['timestamps'] = pd.to_datetime(df['timestamps'])
lookback = 400pred_len = 120
x_df = df.loc[:lookback-1, ['open', 'high', 'low', 'close']]x_timestamp = df.loc[:lookback-1, 'timestamps']y_timestamp = df.loc[lookback:lookback+pred_len-1, 'timestamps']
# 4. Make Predictionpred_df = predictor.predict( df=x_df, x_timestamp=x_timestamp, y_timestamp=y_timestamp, pred_len=pred_len, T=1.0, top_p=0.9, sample_count=1, verbose=True)
# 5. Visualize Resultsprint("Forecasted Data Head:")print(pred_df.head())
# Combine historical and forecasted data for plottingkline_df = df.loc[:lookback+pred_len-1]
# visualizeplot_prediction(kline_df, pred_df)
|