File size: 4,853 Bytes
ccd4d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f20c8e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/hanyueju/miniconda3/envs/kronos/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from model import Kronos, KronosTokenizer, KronosPredictor\n",
    "\n",
    "# Load from Hugging Face Hub\n",
    "tokenizer = KronosTokenizer.from_pretrained(\"NeoQuasar/Kronos-Tokenizer-base\")\n",
    "model = Kronos.from_pretrained(\"NeoQuasar/Kronos-base\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72dc9ba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize the predictor\n",
    "predictor = KronosPredictor(model, tokenizer, max_context=512)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b07f8d0b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X 的行数: 400\n",
      "Y 的行数: 120\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Load your data\n",
    "df = pd.read_csv(\"/home/hanyueju/MinModel/data/qlib_ready_csv/sz002709.csv\")\n",
    "df['timestamps'] = pd.to_datetime(df['date'])\n",
    "df.sort_values('timestamps', inplace=True, ascending=True)\n",
    "df = df.reset_index(drop=True)\n",
    "\n",
    "# Define context window and prediction length\n",
    "lookback = 400\n",
    "pred_len = 120\n",
    "\n",
    "# Prepare inputs for the predictor\n",
    "x_df = df.iloc[-(lookback + pred_len):-pred_len][['open', 'high', 'low', 'close', 'volume', 'amount']]\n",
    "\n",
    "x_timestamp = df.iloc[-(lookback + pred_len):-pred_len]['timestamps']\n",
    "\n",
    "y_timestamp = df.iloc[-pred_len:]['timestamps']\n",
    "# 可以打印一下长度检查是否绝对正确:\n",
    "print(f\"X 的行数: {len(x_df)}\")  # 应该输出 400\n",
    "print(f\"Y 的行数: {len(y_timestamp)}\")  # 应该输出 120"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a6466d07",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/120 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 120/120 [00:01<00:00, 80.70it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Forecasted Data Head:\n",
      "                 open       high        low      close         volume  \\\n",
      "timestamps                                                              \n",
      "2026-04-03  17.217230  17.387726  17.004833  17.084772  218270.890625   \n",
      "2026-04-07  17.034378  17.179306  16.864655  16.910782  211789.062500   \n",
      "2026-04-08  16.875727  17.017885  16.738979  16.775610  209235.468750   \n",
      "2026-04-09  16.813190  16.977127  16.632999  16.682169  252907.796875   \n",
      "2026-04-10  16.673990  16.577105  16.020086  15.899137  270104.250000   \n",
      "\n",
      "                  amount  \n",
      "timestamps                \n",
      "2026-04-03  310970.37500  \n",
      "2026-04-07  290761.37500  \n",
      "2026-04-08  282911.37500  \n",
      "2026-04-09  410148.81250  \n",
      "2026-04-10  450768.71875  \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# Generate predictions\n",
    "pred_df = predictor.predict(\n",
    "    df=x_df,\n",
    "    x_timestamp=x_timestamp,\n",
    "    y_timestamp=y_timestamp,\n",
    "    pred_len=pred_len,\n",
    "    T=1.0,          # Temperature for sampling\n",
    "    top_p=0.9,      # Nucleus sampling probability\n",
    "    sample_count=1  # Number of forecast paths to generate and average\n",
    ")\n",
    "\n",
    "print(\"Forecasted Data Head:\")\n",
    "print(pred_df.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d30a9976",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "kronos",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.25"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}