-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pandas as pd | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.metrics import accuracy_score | ||
import joblib | ||
|
||
class RiskAssessmentModel: | ||
def __init__(self): | ||
self.model = RandomForestClassifier(n_estimators=100, random_state=42) | ||
|
||
def train(self, data_path): | ||
# Load dataset | ||
data = pd.read_csv(data_path) | ||
X = data.drop('risk_label', axis=1) | ||
y = data['risk_label'] | ||
|
||
# Split the dataset | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | ||
|
||
# Train the model | ||
self.model.fit(X_train, y_train) | ||
|
||
# Evaluate the model | ||
predictions = self.model.predict(X_test) | ||
accuracy = accuracy_score(y_test, predictions) | ||
print(f'Model accuracy: {accuracy:.2f}') | ||
|
||
def save_model(self, model_path): | ||
joblib.dump(self.model, model_path) | ||
|
||
def load_model(self, model_path): | ||
self.model = joblib.load(model_path) | ||
|
||
def predict(self, input_data): | ||
return self.model.predict([input_data]) |