Musabbirkm commited on
Commit
94ef51b
·
verified ·
1 Parent(s): 82acae5

Update test_model.py

Browse files
Files changed (1) hide show
  1. 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
- Function to test the trained heart disease prediction model.
7
- Loads the model and makes predictions on a sample dataset.
8
- """
9
- try:
10
- # model loading
11
- production_model = joblib.load('models/uci_heart_disease_model.pkl')
12
- model = production_model['model']
13
- optimal_threshold = production_model['metadata']['threshold']
14
-
15
-
16
-
17
- # engineered features to match training data
18
- test_data['hr_age_ratio'] = test_data['thalach'] / (test_data['age'] + 1e-5)
19
- test_data['bp_oldpeak'] = test_data['trestbps'] * (test_data['oldpeak'] + 1)
20
- test_data['risk_score'] = (test_data['age']/50 + test_data['chol']/200 + test_data['trestbps']/140)
21
-
22
- # Make predictions
23
- probabilities = model.predict_proba(test_data)[:, 1]
24
- predictions = (probabilities >= optimal_threshold).astype(int)
25
-
26
- # results DataFrame
27
- results = pd.DataFrame({
28
- 'Prediction': predictions,
29
- 'Diagnosis': ['Heart Disease' if p == 1 else 'Healthy' for p in predictions],
30
- 'Probability': probabilities,
31
- })
32
-
33
- # data for display
34
- display_data = pd.concat([test_data[['age', 'sex', 'cp', 'trestbps', 'chol']], results], axis=1)
35
-
36
- print("=== Heart Disease Prediction Results ===")
37
- print(f"Using threshold: {optimal_threshold:.3f}\n")
38
- print(display_data.to_string(index=False))
39
-
40
- return results
41
-
42
- except Exception as e:
43
- print(f"Error testing model: {str(e)}")
44
- return None
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)