Spaces:
Runtime error
Runtime error
Update test_model.py
Browse files- test_model.py +44 -47
test_model.py
CHANGED
|
@@ -1,48 +1,45 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import joblib
|
| 3 |
-
|
| 4 |
-
def test_heart_disease_model(test_data):
|
| 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 |
-
# pf =pd.read_csv('dataset/test_data.csv')
|
| 48 |
# test_results = test_heart_disease_model(pf)
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import joblib
|
| 3 |
+
|
| 4 |
+
def test_heart_disease_model(test_data):
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
# model loading
|
| 8 |
+
production_model = joblib.load('models/uci_heart_disease_model.pkl')
|
| 9 |
+
model = production_model['model']
|
| 10 |
+
optimal_threshold = production_model['metadata']['threshold']
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# engineered features to match training data
|
| 15 |
+
test_data['hr_age_ratio'] = test_data['thalach'] / (test_data['age'] + 1e-5)
|
| 16 |
+
test_data['bp_oldpeak'] = test_data['trestbps'] * (test_data['oldpeak'] + 1)
|
| 17 |
+
test_data['risk_score'] = (test_data['age']/50 + test_data['chol']/200 + test_data['trestbps']/140)
|
| 18 |
+
|
| 19 |
+
# Make predictions
|
| 20 |
+
probabilities = model.predict_proba(test_data)[:, 1]
|
| 21 |
+
predictions = (probabilities >= optimal_threshold).astype(int)
|
| 22 |
+
|
| 23 |
+
# results DataFrame
|
| 24 |
+
results = pd.DataFrame({
|
| 25 |
+
'Prediction': predictions,
|
| 26 |
+
'Diagnosis': ['Heart Disease' if p == 1 else 'Healthy' for p in predictions],
|
| 27 |
+
'Probability': probabilities,
|
| 28 |
+
})
|
| 29 |
+
|
| 30 |
+
# data for display
|
| 31 |
+
display_data = pd.concat([test_data[['age', 'sex', 'cp', 'trestbps', 'chol']], results], axis=1)
|
| 32 |
+
|
| 33 |
+
print("=== Heart Disease Prediction Results ===")
|
| 34 |
+
print(f"Using threshold: {optimal_threshold:.3f}\n")
|
| 35 |
+
print(display_data.to_string(index=False))
|
| 36 |
+
|
| 37 |
+
return results
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error testing model: {str(e)}")
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# pf =pd.read_csv('dataset/test_data.csv')
|
|
|
|
|
|
|
|
|
|
| 45 |
# test_results = test_heart_disease_model(pf)
|