Update src/components/model_timeseries_risk.py
Browse files
src/components/model_timeseries_risk.py
CHANGED
|
@@ -4,10 +4,7 @@ import tensorflow as tf
|
|
| 4 |
import joblib
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
-
import logging
|
| 8 |
|
| 9 |
-
logger = logging.getLogger(__name__)
|
| 10 |
-
logging.basicConfig(level=logging.INFO)
|
| 11 |
|
| 12 |
# ----------------------------
|
| 13 |
# Load model and scaler from HF Hub
|
|
@@ -41,7 +38,7 @@ region_name = "Shanghai"
|
|
| 41 |
|
| 42 |
df_region = df[df[region_col] == region_name].copy()
|
| 43 |
if len(df_region) < 100:
|
| 44 |
-
|
| 45 |
df_region = df.sample(200, random_state=42) if len(df) >= 200 else df
|
| 46 |
|
| 47 |
feature_cols = [
|
|
@@ -62,7 +59,7 @@ for i in range(len(X_scaled) - seq_length):
|
|
| 62 |
y_seq.append(y_all[i+seq_length])
|
| 63 |
X_seq = np.array(X_seq)
|
| 64 |
y_seq = np.array(y_seq)
|
| 65 |
-
|
| 66 |
|
| 67 |
# ----------------------------
|
| 68 |
# Train/test split (optional)
|
|
@@ -75,7 +72,7 @@ y_train, y_test = y_seq[:-test_size], y_seq[-test_size:]
|
|
| 75 |
# Evaluate using loaded model
|
| 76 |
# ----------------------------
|
| 77 |
test_loss, test_acc = model.evaluate(X_test, y_test)
|
| 78 |
-
|
| 79 |
|
| 80 |
# ----------------------------
|
| 81 |
# Prediction function
|
|
@@ -85,7 +82,7 @@ def predict_risk_for_next_day(sequence, threshold=0.5):
|
|
| 85 |
seq_window = np.expand_dims(seq_scaled, axis=0)
|
| 86 |
pred_prob = model.predict(seq_window)[0][0]
|
| 87 |
pred_label = int(pred_prob > threshold)
|
| 88 |
-
|
| 89 |
return pred_prob, pred_label
|
| 90 |
|
| 91 |
# Example usage
|
|
|
|
| 4 |
import joblib
|
| 5 |
import requests
|
| 6 |
from io import BytesIO
|
|
|
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ----------------------------
|
| 10 |
# Load model and scaler from HF Hub
|
|
|
|
| 38 |
|
| 39 |
df_region = df[df[region_col] == region_name].copy()
|
| 40 |
if len(df_region) < 100:
|
| 41 |
+
|
| 42 |
df_region = df.sample(200, random_state=42) if len(df) >= 200 else df
|
| 43 |
|
| 44 |
feature_cols = [
|
|
|
|
| 59 |
y_seq.append(y_all[i+seq_length])
|
| 60 |
X_seq = np.array(X_seq)
|
| 61 |
y_seq = np.array(y_seq)
|
| 62 |
+
|
| 63 |
|
| 64 |
# ----------------------------
|
| 65 |
# Train/test split (optional)
|
|
|
|
| 72 |
# Evaluate using loaded model
|
| 73 |
# ----------------------------
|
| 74 |
test_loss, test_acc = model.evaluate(X_test, y_test)
|
| 75 |
+
|
| 76 |
|
| 77 |
# ----------------------------
|
| 78 |
# Prediction function
|
|
|
|
| 82 |
seq_window = np.expand_dims(seq_scaled, axis=0)
|
| 83 |
pred_prob = model.predict(seq_window)[0][0]
|
| 84 |
pred_label = int(pred_prob > threshold)
|
| 85 |
+
|
| 86 |
return pred_prob, pred_label
|
| 87 |
|
| 88 |
# Example usage
|