|
| 1 | +# src/main/arbitration/risk_assessment.py |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +from sklearn.model_selection import train_test_split |
| 6 | +from sklearn.ensemble import RandomForestClassifier |
| 7 | +from sklearn.metrics import accuracy_score, classification_report |
| 8 | +import joblib |
| 9 | +import logging |
| 10 | + |
| 11 | +class RiskAssessment: |
| 12 | + def __init__(self, model_path='./models/risk_assessment_model.pkl'): |
| 13 | + self.model_path = model_path |
| 14 | + self.model = None |
| 15 | + logging.info("RiskAssessment initialized with model path: %s", self.model_path) |
| 16 | + |
| 17 | + def load_data(self, data): |
| 18 | + """ |
| 19 | + Load and preprocess the input data for risk assessment. |
| 20 | + |
| 21 | + Args: |
| 22 | + data (dict): A dictionary containing the features for risk assessment. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + pd.DataFrame: Processed features for the model. |
| 26 | + """ |
| 27 | + logging.info("Loading data for risk assessment...") |
| 28 | + df = pd.DataFrame(data) |
| 29 | + logging.info("Data loaded with %d samples.", len(df)) |
| 30 | + return df |
| 31 | + |
| 32 | + def train(self, data): |
| 33 | + """ |
| 34 | + Train the risk assessment model using the provided data. |
| 35 | + |
| 36 | + Args: |
| 37 | + data (dict): A dictionary containing the features and target variable. |
| 38 | + """ |
| 39 | + df = self.load_data(data) |
| 40 | + |
| 41 | + # Assuming the last column is the target variable (1 for high risk, 0 for low risk) |
| 42 | + X = df.drop(columns=['risk_level']) # Features |
| 43 | + y = df['risk_level'] # Target variable |
| 44 | + |
| 45 | + # Split the data into training and testing sets |
| 46 | + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
| 47 | + |
| 48 | + # Initialize and train the Random Forest model |
| 49 | + self.model = RandomForestClassifier(n_estimators=100, random_state=42) |
| 50 | + self.model.fit(X_train, y_train) |
| 51 | + |
| 52 | + # Evaluate the model |
| 53 | + y_pred = self.model.predict(X_test) |
| 54 | + accuracy = accuracy_score(y_test, y_pred) |
| 55 | + logging.info("Model trained successfully with accuracy: %.2f%%", accuracy * 100) |
| 56 | + logging.info("Classification report:\n%s", classification_report(y_test, y_pred)) |
| 57 | + |
| 58 | + # Save the trained model |
| 59 | + self.save_model() |
| 60 | + |
| 61 | + def assess_risk(self, data): |
| 62 | + """ |
| 63 | + Assess the risk based on the input data. |
| 64 | + |
| 65 | + Args: |
| 66 | + data (dict): A dictionary containing the features for risk assessment. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + dict: The risk assessment result indicating the risk level. |
| 70 | + """ |
| 71 | + if self.model is None: |
| 72 | + logging.error("Model is not trained. Please train the model before assessment.") |
| 73 | + return None |
| 74 | + |
| 75 | + # Convert input data to DataFrame |
| 76 | + input_df = pd.DataFrame(data, index=[0]) |
| 77 | + prediction = self.model.predict(input_df) |
| 78 | + risk_level = prediction[0] # 1 for high risk, 0 for low risk |
| 79 | + |
| 80 | + logging.info("Risk assessment result: %s", "High risk" if risk_level == 1 else "Low risk") |
| 81 | + return {"risk_level": risk_level} |
| 82 | + |
| 83 | + def save_model(self): |
| 84 | + """Save the trained model to a file.""" |
| 85 | + joblib.dump(self.model, self.model_path) |
| 86 | + logging.info("Model saved to %s", self.model_path) |
| 87 | + |
| 88 | + def load_model(self): |
| 89 | + """Load the trained model from a file.""" |
| 90 | + try: |
| 91 | + self.model = joblib.load(self.model_path) |
| 92 | + logging.info("Model loaded from %s", self.model_path) |
| 93 | + except FileNotFoundError: |
| 94 | + logging.error("Model file not found. Please train the model first.") |
0 commit comments