|
a |
|
b/scripts/model/inference.py |
|
|
1 |
from time import time |
|
|
2 |
import warnings |
|
|
3 |
import joblib |
|
|
4 |
import sys |
|
|
5 |
import os |
|
|
6 |
|
|
|
7 |
warnings.filterwarnings('ignore') |
|
|
8 |
|
|
|
9 |
# Load trained model |
|
|
10 |
try: |
|
|
11 |
model = joblib.load(open(os.path.join(os.path.dirname(__file__), '../../models/gradient_boosting.joblib'), 'rb')) |
|
|
12 |
except FileNotFoundError as err: |
|
|
13 |
print(f'Ann error occoured: {err}') |
|
|
14 |
|
|
|
15 |
# Predict new data |
|
|
16 |
def predict(data): |
|
|
17 |
return model.predict([data])[0] |
|
|
18 |
|
|
|
19 |
# Get the prediction outcome |
|
|
20 |
def proba(data): |
|
|
21 |
return f'{(model.predict_proba([data])[0][predict(data)] * 100):.0f}%' |
|
|
22 |
|
|
|
23 |
# Result |
|
|
24 |
def main(data): |
|
|
25 |
timer = time() |
|
|
26 |
|
|
|
27 |
prediction = predict(data) |
|
|
28 |
|
|
|
29 |
if prediction == 1: |
|
|
30 |
return f'Prediction: {proba(data)} - Affected | Prediction speed: {(time() - timer):.3f}' |
|
|
31 |
else: |
|
|
32 |
return f'Prediction: {proba(data)} - Benign | Prediction speed: {(time() - timer):.3f}' |
|
|
33 |
|
|
|
34 |
if __name__ == '__main__': |
|
|
35 |
print(main([1,24,1,1,2,1,1,2,1,1,1])) # My health status |