Spaces:
Running
Running
修复预测函数中的股票和指数影响计算逻辑,添加零值处理以避免除零错误,并格式化输出影响值为百分比字符串
Browse files- blkeras.py +29 -13
blkeras.py
CHANGED
|
@@ -231,10 +231,11 @@ def predict(text: str, stock_codes: list):
|
|
| 231 |
last_index_dj_value = previous_stock_dj_index_history[0][-1][0]
|
| 232 |
last_index_ixic_value = previous_stock_ixic_index_history[0][-1][0]
|
| 233 |
last_index_ndx_value = previous_stock_ndx_index_history[0][-1][0]
|
|
|
|
| 234 |
|
| 235 |
|
| 236 |
# 针对 1012 模型的修复
|
| 237 |
-
stock_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), stock_predictions[0],
|
| 238 |
index_inx_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), index_inx_predictions[0], last_index_inx_value)
|
| 239 |
index_dj_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), index_dj_predictions[0], last_index_dj_value)
|
| 240 |
index_ixic_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), index_ixic_predictions[0], last_index_ixic_value)
|
|
@@ -265,22 +266,30 @@ def predict(text: str, stock_codes: list):
|
|
| 265 |
index_ndx_day_2 = index_ndx_predictions[1][0]
|
| 266 |
index_ndx_day_3 = index_ndx_predictions[2][0]
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
# 计算 impact_1_day, impact_2_day, impact_3_day
|
| 269 |
-
impact_inx_1_day = (index_inx_day_1 - last_index_inx_value) / last_index_inx_value
|
| 270 |
-
impact_inx_2_day = (index_inx_day_2 - index_inx_day_1) / index_inx_day_1
|
| 271 |
-
impact_inx_3_day = (index_inx_day_3 - index_inx_day_2) / index_inx_day_2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
|
| 285 |
# 将 impact 值转换为百分比字符串
|
| 286 |
impact_inx_1_day_str = f"{impact_inx_1_day:.2%}"
|
|
@@ -299,6 +308,10 @@ def predict(text: str, stock_codes: list):
|
|
| 299 |
impact_ndx_2_day_str = f"{impact_ndx_2_day:.2%}"
|
| 300 |
impact_ndx_3_day_str = f"{impact_ndx_3_day:.2%}"
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
# 如果需要返回原始预测数据进行调试,可以直接将其放到响应中
|
| 304 |
if len(affected_stock_codes) > 5:
|
|
@@ -334,6 +347,9 @@ def predict(text: str, stock_codes: list):
|
|
| 334 |
"impact_ndx_1_day": impact_ndx_1_day_str, # 计算并格式化 impact_1_day
|
| 335 |
"impact_ndx_2_day": impact_ndx_2_day_str, # 计算并格式化 impact_2_day
|
| 336 |
"impact_ndx_3_day": impact_ndx_3_day_str,
|
|
|
|
|
|
|
|
|
|
| 337 |
"affected_stock_codes": affected_stock_codes_str, # 动态生成受影响的股票代码
|
| 338 |
"accuracy": float(fake_accuracy),
|
| 339 |
"impact_on_stock": stock_predictions, # 第一个预测值是股票影响
|
|
|
|
| 231 |
last_index_dj_value = previous_stock_dj_index_history[0][-1][0]
|
| 232 |
last_index_ixic_value = previous_stock_ixic_index_history[0][-1][0]
|
| 233 |
last_index_ndx_value = previous_stock_ndx_index_history[0][-1][0]
|
| 234 |
+
last_stock_value = previous_stock_history[0][-1][0]
|
| 235 |
|
| 236 |
|
| 237 |
# 针对 1012 模型的修复
|
| 238 |
+
stock_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), stock_predictions[0], last_stock_value)
|
| 239 |
index_inx_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), index_inx_predictions[0], last_index_inx_value)
|
| 240 |
index_dj_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), index_dj_predictions[0], last_index_dj_value)
|
| 241 |
index_ixic_predictions = stock_fix_for_1012_model(float(X_sentiment[0][0]), index_ixic_predictions[0], last_index_ixic_value)
|
|
|
|
| 266 |
index_ndx_day_2 = index_ndx_predictions[1][0]
|
| 267 |
index_ndx_day_3 = index_ndx_predictions[2][0]
|
| 268 |
|
| 269 |
+
stock_day_1 = stock_predictions[0][0]
|
| 270 |
+
stock_day_2 = stock_predictions[1][0]
|
| 271 |
+
stock_day_3 = stock_predictions[2][0]
|
| 272 |
+
|
| 273 |
# 计算 impact_1_day, impact_2_day, impact_3_day
|
| 274 |
+
impact_inx_1_day = (index_inx_day_1 - last_index_inx_value) / last_index_inx_value if last_index_inx_value != 0 else 0
|
| 275 |
+
impact_inx_2_day = (index_inx_day_2 - index_inx_day_1) / index_inx_day_1 if index_inx_day_1 != 0 else 0
|
| 276 |
+
impact_inx_3_day = (index_inx_day_3 - index_inx_day_2) / index_inx_day_2 if index_inx_day_2 != 0 else 0
|
| 277 |
+
|
| 278 |
+
impact_dj_1_day = (index_dj_day_1 - last_index_dj_value) / last_index_dj_value if last_index_dj_value != 0 else 0
|
| 279 |
+
impact_dj_2_day = (index_dj_day_2 - index_dj_day_1) / index_dj_day_1 if index_dj_day_1 != 0 else 0
|
| 280 |
+
impact_dj_3_day = (index_dj_day_3 - index_dj_day_2) / index_dj_day_2 if index_dj_day_2 != 0 else 0
|
| 281 |
|
| 282 |
+
impact_ixic_1_day = (index_ixic_day_1 - last_index_ixic_value) / last_index_ixic_value if last_index_ixic_value != 0 else 0
|
| 283 |
+
impact_ixic_2_day = (index_ixic_day_2 - index_ixic_day_1) / index_ixic_day_1 if index_ixic_day_1 != 0 else 0
|
| 284 |
+
impact_ixic_3_day = (index_ixic_day_3 - index_ixic_day_2) / index_ixic_day_2 if index_ixic_day_2 != 0 else 0
|
| 285 |
|
| 286 |
+
impact_ndx_1_day = (index_ndx_day_1 - last_index_ndx_value) / last_index_ndx_value if last_index_ndx_value != 0 else 0
|
| 287 |
+
impact_ndx_2_day = (index_ndx_day_2 - index_ndx_day_1) / index_ndx_day_1 if index_ndx_day_1 != 0 else 0
|
| 288 |
+
impact_ndx_3_day = (index_ndx_day_3 - index_ndx_day_2) / index_ndx_day_2 if index_ndx_day_2 != 0 else 0
|
| 289 |
|
| 290 |
+
impact_stock_1_day = (stock_day_1 - last_stock_value) / last_stock_value if last_stock_value != 0 else 0
|
| 291 |
+
impact_stock_2_day = (stock_day_2 - stock_day_1) / stock_day_1 if stock_day_1 != 0 else 0
|
| 292 |
+
impact_stock_3_day = (stock_day_3 - stock_day_2) / stock_day_2 if stock_day_2 != 0 else 0
|
| 293 |
|
| 294 |
# 将 impact 值转换为百分比字符串
|
| 295 |
impact_inx_1_day_str = f"{impact_inx_1_day:.2%}"
|
|
|
|
| 308 |
impact_ndx_2_day_str = f"{impact_ndx_2_day:.2%}"
|
| 309 |
impact_ndx_3_day_str = f"{impact_ndx_3_day:.2%}"
|
| 310 |
|
| 311 |
+
impact_stock_1_day_str = f"{impact_stock_1_day:.2%}"
|
| 312 |
+
impact_stock_2_day_str = f"{impact_stock_2_day:.2%}"
|
| 313 |
+
impact_stock_3_day_str = f"{impact_stock_3_day:.2%}"
|
| 314 |
+
|
| 315 |
|
| 316 |
# 如果需要返回原始预测数据进行调试,可以直接将其放到响应中
|
| 317 |
if len(affected_stock_codes) > 5:
|
|
|
|
| 347 |
"impact_ndx_1_day": impact_ndx_1_day_str, # 计算并格式化 impact_1_day
|
| 348 |
"impact_ndx_2_day": impact_ndx_2_day_str, # 计算并格式化 impact_2_day
|
| 349 |
"impact_ndx_3_day": impact_ndx_3_day_str,
|
| 350 |
+
"impact_stock_1_day": impact_stock_1_day_str, # 计算并格式化 impact_1_day
|
| 351 |
+
"impact_stock_2_day": impact_stock_2_day_str, # 计算并格式化 impact_2_day
|
| 352 |
+
"impact_stock_3_day": impact_stock_3_day_str,
|
| 353 |
"affected_stock_codes": affected_stock_codes_str, # 动态生成受影响的股票代码
|
| 354 |
"accuracy": float(fake_accuracy),
|
| 355 |
"impact_on_stock": stock_predictions, # 第一个预测值是股票影响
|