Skip to content

Commit a31f21b

Browse files
authored
Create asset_management_model.py
1 parent 6e785cf commit a31f21b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

models/asset_management_model.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import pandas as pd
2+
from sklearn.model_selection import train_test_split, GridSearchCV
3+
from sklearn.tree import DecisionTreeClassifier
4+
from sklearn.ensemble import RandomForestClassifier
5+
from sklearn.metrics import accuracy_score, classification_report
6+
import joblib
7+
8+
# Load your dataset
9+
# For demonstration, we will create a synthetic dataset
10+
# In a real scenario, you would load your dataset from a CSV or database
11+
data = {
12+
'asset_type': ['stock', 'bond', 'stock', 'bond', 'stock', 'real estate', 'real estate', 'bond', 'stock', 'real estate'],
13+
'risk_level': [1, 0, 1, 0, 1, 2, 2, 0, 1, 2], # 0: Low, 1: Medium, 2: High
14+
'return': [10, 5, 12, 3, 15, 8, 9, 4, 11, 7] # Example feature
15+
}
16+
17+
df = pd.DataFrame(data)
18+
19+
# Encode categorical variables
20+
df['asset_type'] = df['asset_type'].astype('category').cat.codes
21+
22+
# Features and target variable
23+
X = df[['asset_type', 'risk_level', 'return']]
24+
y = df['asset_type'] # Predict asset type
25+
26+
# Split the data into training and testing sets
27+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
28+
29+
# Model selection and hyperparameter tuning using GridSearchCV
30+
param_grid = {
31+
'n_estimators': [50, 100, 200],
32+
'max_depth': [None, 10, 20, 30],
33+
'min_samples_split': [2, 5, 10]
34+
}
35+
36+
# Using Random Forest Classifier for better performance
37+
model = RandomForestClassifier(random_state=42)
38+
39+
# Grid search for hyperparameter tuning
40+
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, scoring='accuracy', verbose=2, n_jobs=-1)
41+
grid_search.fit(X_train, y_train)
42+
43+
# Best model from grid search
44+
best_model = grid_search.best_estimator_
45+
46+
# Evaluate the model
47+
y_pred = best_model.predict(X_test)
48+
accuracy = accuracy_score(y_test, y_pred)
49+
report = classification_report(y_test, y_pred)
50+
51+
print(f'Accuracy: {accuracy}')
52+
print('Classification Report:')
53+
print(report)
54+
55+
# Save the model
56+
joblib.dump(best_model, 'models/asset_management_model.pkl')
57+
print('Asset management model saved as asset_management_model.pkl')

0 commit comments

Comments
 (0)