Commit
·
6d62996
1
Parent(s):
426f91f
test2
Browse files
app.py
CHANGED
@@ -19,25 +19,21 @@ def prepare_data_chronos(data):
|
|
19 |
formatted_df = pd.DataFrame({
|
20 |
'item_id': ['stock'] * len(df),
|
21 |
'timestamp': pd.to_datetime(df['Date']),
|
22 |
-
'value': df['Close'].astype('float32').values.ravel()
|
23 |
})
|
24 |
|
25 |
# 按照時間戳排序
|
26 |
formatted_df = formatted_df.sort_values('timestamp')
|
27 |
|
28 |
try:
|
29 |
-
#
|
30 |
-
ts_df = TimeSeriesDataFrame(
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
print("Value column shape:", ts_df['value'].shape)
|
35 |
-
|
36 |
return ts_df
|
37 |
except Exception as e:
|
38 |
print(f"Error creating TimeSeriesDataFrame: {str(e)}")
|
39 |
-
print("Formatted DataFrame info:")
|
40 |
-
print(formatted_df.info())
|
41 |
raise
|
42 |
|
43 |
|
@@ -113,21 +109,19 @@ def get_top_10_potential_stocks(period, selected_indices):
|
|
113 |
|
114 |
for ticker in stock_list:
|
115 |
try:
|
116 |
-
# 獲取股票數據
|
117 |
data = get_stock_data(ticker, period)
|
118 |
if data.empty:
|
119 |
continue
|
120 |
|
121 |
-
# 準備數據
|
122 |
ts_data = prepare_data_chronos(data)
|
123 |
|
124 |
-
#
|
125 |
predictor = TimeSeriesPredictor(
|
126 |
prediction_length=prediction_length,
|
127 |
-
target='value'
|
|
|
128 |
)
|
129 |
|
130 |
-
# 訓練模型
|
131 |
predictor.fit(
|
132 |
ts_data,
|
133 |
hyperparameters={
|
@@ -135,10 +129,7 @@ def get_top_10_potential_stocks(period, selected_indices):
|
|
135 |
}
|
136 |
)
|
137 |
|
138 |
-
# 進行預測
|
139 |
predictions = predictor.predict(ts_data)
|
140 |
-
|
141 |
-
# 計算潛力
|
142 |
potential = (predictions.iloc[-1] - data['Close'].iloc[-1]) / data['Close'].iloc[-1]
|
143 |
stock_predictions.append((ticker, potential, data['Close'].iloc[-1], predictions.iloc[-1]))
|
144 |
|
|
|
19 |
formatted_df = pd.DataFrame({
|
20 |
'item_id': ['stock'] * len(df),
|
21 |
'timestamp': pd.to_datetime(df['Date']),
|
22 |
+
'value': df['Close'].astype('float32').values.ravel()
|
23 |
})
|
24 |
|
25 |
# 按照時間戳排序
|
26 |
formatted_df = formatted_df.sort_values('timestamp')
|
27 |
|
28 |
try:
|
29 |
+
# 指定頻率創建 TimeSeriesDataFrame
|
30 |
+
ts_df = TimeSeriesDataFrame(
|
31 |
+
formatted_df,
|
32 |
+
freq="1D" # 指定為每日數據
|
33 |
+
)
|
|
|
|
|
34 |
return ts_df
|
35 |
except Exception as e:
|
36 |
print(f"Error creating TimeSeriesDataFrame: {str(e)}")
|
|
|
|
|
37 |
raise
|
38 |
|
39 |
|
|
|
109 |
|
110 |
for ticker in stock_list:
|
111 |
try:
|
|
|
112 |
data = get_stock_data(ticker, period)
|
113 |
if data.empty:
|
114 |
continue
|
115 |
|
|
|
116 |
ts_data = prepare_data_chronos(data)
|
117 |
|
118 |
+
# 創建預測器時指定頻率
|
119 |
predictor = TimeSeriesPredictor(
|
120 |
prediction_length=prediction_length,
|
121 |
+
target='value',
|
122 |
+
freq="1D" # 指定為每日數據
|
123 |
)
|
124 |
|
|
|
125 |
predictor.fit(
|
126 |
ts_data,
|
127 |
hyperparameters={
|
|
|
129 |
}
|
130 |
)
|
131 |
|
|
|
132 |
predictions = predictor.predict(ts_data)
|
|
|
|
|
133 |
potential = (predictions.iloc[-1] - data['Close'].iloc[-1]) / data['Close'].iloc[-1]
|
134 |
stock_predictions.append((ticker, potential, data['Close'].iloc[-1], predictions.iloc[-1]))
|
135 |
|