You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

1 month ago
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import sys
  4. sys.path.append("../")
  5. from model import Kronos, KronosTokenizer, KronosPredictor
  6. def plot_prediction(kline_df, pred_df):
  7. pred_df.index = kline_df.index[-pred_df.shape[0]:]
  8. sr_close = kline_df['close']
  9. sr_pred_close = pred_df['close']
  10. sr_close.name = 'Ground Truth'
  11. sr_pred_close.name = "Prediction"
  12. sr_volume = kline_df['volume']
  13. sr_pred_volume = pred_df['volume']
  14. sr_volume.name = 'Ground Truth'
  15. sr_pred_volume.name = "Prediction"
  16. close_df = pd.concat([sr_close, sr_pred_close], axis=1)
  17. volume_df = pd.concat([sr_volume, sr_pred_volume], axis=1)
  18. fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6), sharex=True)
  19. ax1.plot(close_df['Ground Truth'], label='Ground Truth', color='blue', linewidth=1.5)
  20. ax1.plot(close_df['Prediction'], label='Prediction', color='red', linewidth=1.5)
  21. ax1.set_ylabel('Close Price', fontsize=14)
  22. ax1.legend(loc='lower left', fontsize=12)
  23. ax1.grid(True)
  24. ax2.plot(volume_df['Ground Truth'], label='Ground Truth', color='blue', linewidth=1.5)
  25. ax2.plot(volume_df['Prediction'], label='Prediction', color='red', linewidth=1.5)
  26. ax2.set_ylabel('Volume', fontsize=14)
  27. ax2.legend(loc='upper left', fontsize=12)
  28. ax2.grid(True)
  29. plt.tight_layout()
  30. plt.show()
  31. # 1. Load Model and Tokenizer
  32. tokenizer = KronosTokenizer.from_pretrained('/home/csc/huggingface/Kronos-Tokenizer-base/')
  33. model = Kronos.from_pretrained("/home/csc/huggingface/Kronos-base/")
  34. # 2. Instantiate Predictor
  35. predictor = KronosPredictor(model, tokenizer, device="cuda:0", max_context=512)
  36. # 3. Prepare Data
  37. df = pd.read_csv("./data/XSHG_5min_600977.csv")
  38. df['timestamps'] = pd.to_datetime(df['timestamps'])
  39. lookback = 400
  40. pred_len = 120
  41. dfs = []
  42. xtsp = []
  43. ytsp = []
  44. for i in range(5):
  45. idf = df.loc[(i*400):(i*400+lookback-1), ['open', 'high', 'low', 'close', 'volume', 'amount']]
  46. i_x_timestamp = df.loc[(i*400):(i*400+lookback-1), 'timestamps']
  47. i_y_timestamp = df.loc[(i*400+lookback):(i*400+lookback+pred_len-1), 'timestamps']
  48. dfs.append(idf)
  49. xtsp.append(i_x_timestamp)
  50. ytsp.append(i_y_timestamp)
  51. pred_df = predictor.predict_batch(
  52. df_list=dfs,
  53. x_timestamp_list=xtsp,
  54. y_timestamp_list=ytsp,
  55. pred_len=pred_len,
  56. )