Skip to content

Commit

Permalink
Update save_model.py (#9)
Browse files Browse the repository at this point in the history
## Summary by Sourcery

New Features:
- Persist a trained Random Forest model using pickle.
  • Loading branch information
canstralian authored Jan 8, 2025
2 parents acf1a5f + 1263470 commit 683aebb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions models/save_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import os
import pickle
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Assuming 'model' is your trained model
with open('models/your_model.pkl', 'wb') as f:
# Create the models directory if it doesn't exist
os.makedirs("models", exist_ok=True)

# Generate synthetic data
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train a Random Forest model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Save the trained model to the /models directory
model_path = "models/random_forest_model.pkl"
with open(model_path, "wb") as f:
pickle.dump(model, f)

print(f"Random Forest model saved to {model_path}")

0 comments on commit 683aebb

Please sign in to comment.