Random Forest model trained with scikit-learn in Python + save and load model

GeoSense ✅
3 min readJan 27, 2023

In this post, I will show you how to train, save and load Random Forest model with scikit-learn and joblib in Python. Moreover, I will show you, how to compress the model and get a smaller file.

Train and save Machine Learning Model
# Load Libraries
import os
import joblib
import numpy as np
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
# Load Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Train the Random Forest classifier
rf = RandomForestClassifier()
rf.fit(X,y)
rf.predict(X)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

Let’s save the Random Forest

# save
joblib.dump(rf, "random_forest.joblib")
# load, no need to initialize the loaded_rf
loaded_rf =…

--

--

GeoSense ✅

🌏 Remote sensing | 🛰️ Geographic Information Systems (GIS) | ℹ️ https://www.tnmthai.com/medium