A trained machine learning model alone will not add value for business. The model must be integrated into the company’s IT infrastructure. Let’s develope REST API microservice to classify Iris flowers. The dataset consists of the length and width of two types of Iris petals: sepal and petal. The target variable is Iris variety: 0 – Setosa, 1 – Versicolor, 2 – Virginica.
Saving and loading a model
Before moving on to develope API, we need to train and save the model. Take the RandomForestClassifier model. Now let’s save the model to a file and load it to make predictions. This can be done with pickle or joblib.
import pickle filename = 'model.pkl'
pickle.dump(clf, open(filename, 'wb'))
We’ll use pickle.load to load and validate the model.
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_test, y_test)
The code for training, saving and loading the model is available in the repository — link