Spaces:
Sleeping
Sleeping
Update src/tests/inference.py
Browse files- src/tests/inference.py +85 -85
src/tests/inference.py
CHANGED
|
@@ -1,85 +1,85 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import joblib
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from typing import Dict
|
| 5 |
-
from config import MODEL_PATH, FEATURES_PATH
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
class CreditScorePredictor:
|
| 9 |
-
def __init__(self):
|
| 10 |
-
self.model = None
|
| 11 |
-
self.features = None
|
| 12 |
-
self._model_loaded = False
|
| 13 |
-
|
| 14 |
-
def load_model(self):
|
| 15 |
-
if not self._model_loaded:
|
| 16 |
-
self.model = joblib.load(MODEL_PATH)
|
| 17 |
-
with open(FEATURES_PATH, 'r') as f:
|
| 18 |
-
self.features = json.load(f)
|
| 19 |
-
self._model_loaded = True
|
| 20 |
-
|
| 21 |
-
def predict(self, features_dict: Dict[str, float]) -> str:
|
| 22 |
-
# Ensure model is loaded
|
| 23 |
-
self.load_model()
|
| 24 |
-
|
| 25 |
-
df = pd.DataFrame([features_dict])
|
| 26 |
-
|
| 27 |
-
# Ensure correct feature order
|
| 28 |
-
df = df[
|
| 29 |
-
self.features['all_features']
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
# Get prediction
|
| 33 |
-
pred_class = self.model.predict(df)[0]
|
| 34 |
-
|
| 35 |
-
# Map to credit score labels
|
| 36 |
-
credit_labels = {0: 'Poor', 1: 'Standard', 2: 'Good'}
|
| 37 |
-
prediction = credit_labels.get(pred_class, 'Unknown')
|
| 38 |
-
|
| 39 |
-
return prediction
|
| 40 |
-
|
| 41 |
-
def predict_proba(self, features_dict: Dict[str, float]) -> Dict[str, float]:
|
| 42 |
-
# Ensure model is loaded
|
| 43 |
-
self.load_model()
|
| 44 |
-
|
| 45 |
-
df = pd.DataFrame([features_dict])
|
| 46 |
-
|
| 47 |
-
# Ensure correct feature order
|
| 48 |
-
df = df[self.features['all_features']]
|
| 49 |
-
|
| 50 |
-
# Get prediction probabilities
|
| 51 |
-
proba = self.model.predict_proba(df)[0]
|
| 52 |
-
|
| 53 |
-
# Map to credit score labels
|
| 54 |
-
credit_labels = {0: 'Poor', 1: 'Standard', 2: 'Good'}
|
| 55 |
-
probabilities = {
|
| 56 |
-
credit_labels[i]: float(proba[i]) for i in range(len(proba))
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
return probabilities
|
| 60 |
-
|
| 61 |
-
def get_feature_names(self):
|
| 62 |
-
# Ensure model is loaded to get feature names
|
| 63 |
-
self.load_model()
|
| 64 |
-
return self.features['all_features']
|
| 65 |
-
|
| 66 |
-
def get_top_features(self, n=10):
|
| 67 |
-
# Ensure model is loaded
|
| 68 |
-
self.load_model()
|
| 69 |
-
# Top 10 most important features based on model evaluation
|
| 70 |
-
top_features = [
|
| 71 |
-
'Credit_Mix_Ordinal',
|
| 72 |
-
'Outstanding_Debt',
|
| 73 |
-
'Delay_from_due_date',
|
| 74 |
-
'Payment_of_Min_Amount_Yes',
|
| 75 |
-
'Changed_Credit_Limit',
|
| 76 |
-
'Credit_Utilization_Ratio',
|
| 77 |
-
'Monthly_Balance',
|
| 78 |
-
'Num_Bank_Accounts',
|
| 79 |
-
'Num_Credit_Inquiries',
|
| 80 |
-
'Annual_Income'
|
| 81 |
-
]
|
| 82 |
-
return top_features[:n]
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
predictor = CreditScorePredictor()
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Dict
|
| 5 |
+
from src.tests.config import MODEL_PATH, FEATURES_PATH
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class CreditScorePredictor:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.model = None
|
| 11 |
+
self.features = None
|
| 12 |
+
self._model_loaded = False
|
| 13 |
+
|
| 14 |
+
def load_model(self):
|
| 15 |
+
if not self._model_loaded:
|
| 16 |
+
self.model = joblib.load(MODEL_PATH)
|
| 17 |
+
with open(FEATURES_PATH, 'r') as f:
|
| 18 |
+
self.features = json.load(f)
|
| 19 |
+
self._model_loaded = True
|
| 20 |
+
|
| 21 |
+
def predict(self, features_dict: Dict[str, float]) -> str:
|
| 22 |
+
# Ensure model is loaded
|
| 23 |
+
self.load_model()
|
| 24 |
+
|
| 25 |
+
df = pd.DataFrame([features_dict])
|
| 26 |
+
|
| 27 |
+
# Ensure correct feature order
|
| 28 |
+
df = df[
|
| 29 |
+
self.features['all_features']
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
# Get prediction
|
| 33 |
+
pred_class = self.model.predict(df)[0]
|
| 34 |
+
|
| 35 |
+
# Map to credit score labels
|
| 36 |
+
credit_labels = {0: 'Poor', 1: 'Standard', 2: 'Good'}
|
| 37 |
+
prediction = credit_labels.get(pred_class, 'Unknown')
|
| 38 |
+
|
| 39 |
+
return prediction
|
| 40 |
+
|
| 41 |
+
def predict_proba(self, features_dict: Dict[str, float]) -> Dict[str, float]:
|
| 42 |
+
# Ensure model is loaded
|
| 43 |
+
self.load_model()
|
| 44 |
+
|
| 45 |
+
df = pd.DataFrame([features_dict])
|
| 46 |
+
|
| 47 |
+
# Ensure correct feature order
|
| 48 |
+
df = df[self.features['all_features']]
|
| 49 |
+
|
| 50 |
+
# Get prediction probabilities
|
| 51 |
+
proba = self.model.predict_proba(df)[0]
|
| 52 |
+
|
| 53 |
+
# Map to credit score labels
|
| 54 |
+
credit_labels = {0: 'Poor', 1: 'Standard', 2: 'Good'}
|
| 55 |
+
probabilities = {
|
| 56 |
+
credit_labels[i]: float(proba[i]) for i in range(len(proba))
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
return probabilities
|
| 60 |
+
|
| 61 |
+
def get_feature_names(self):
|
| 62 |
+
# Ensure model is loaded to get feature names
|
| 63 |
+
self.load_model()
|
| 64 |
+
return self.features['all_features']
|
| 65 |
+
|
| 66 |
+
def get_top_features(self, n=10):
|
| 67 |
+
# Ensure model is loaded
|
| 68 |
+
self.load_model()
|
| 69 |
+
# Top 10 most important features based on model evaluation
|
| 70 |
+
top_features = [
|
| 71 |
+
'Credit_Mix_Ordinal',
|
| 72 |
+
'Outstanding_Debt',
|
| 73 |
+
'Delay_from_due_date',
|
| 74 |
+
'Payment_of_Min_Amount_Yes',
|
| 75 |
+
'Changed_Credit_Limit',
|
| 76 |
+
'Credit_Utilization_Ratio',
|
| 77 |
+
'Monthly_Balance',
|
| 78 |
+
'Num_Bank_Accounts',
|
| 79 |
+
'Num_Credit_Inquiries',
|
| 80 |
+
'Annual_Income'
|
| 81 |
+
]
|
| 82 |
+
return top_features[:n]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
predictor = CreditScorePredictor()
|