SK-Learn Deployment 

If the metrics are to your liking, then its ready for deployment. This can be done by the following code is an example of deploying the logistic regression model which has the arbitrary definition of log:

log.deploy_model(description="example model for documentation",
                    author="Maxwell Flitton",
                    organisation="Example Organisation",
                    contact="example@gmail.com",
                    file_name="example_model.sav")

This then saves the model in the directory of where the script is running.

The saved package is essentially a dictionary with the following keys:

  • model_title = This is automatically defined by the model you selected
  • model = This is the actual model that accepts data and makes a prediction.
  • description = Short description of what the model does, this is defined as parameter in the deploy_model method
  • author = String describing the name of the person who trained the model, this is defined as a parameter in the deploy_model method
  • organisation = String describing the organisation where the model was trained, this is defined as a parameter in the deploy_model method
  • prediction_target = This is the outcome that the model is trying to predict. This is automatically defined in the training process
  • scaler = scaling object used in the training process. This is automatically defined in the training process
  • scaling used = string describing the type of scaler used. Automatically defined in the training process.
  • contact = This can be left blank. It's the contact info of the person who trained the model. This is defined in the parameter of the deploy_model method
  • date = date and time when the model is packaged for deployment. Automatically defined in the deployment process
  • metrics = This is the precision, recall, and accuracy of the model. This is automatically defined in the training process
  • input order = This is a list of strings that defines the order in which parameters need to be fed into the algorithm for calculation. This is automatically defined in the training process.
  • system version = This is the version of python that the model was trained and packaged on. This shouldn't matter too much but might be important if having to configure a server to run the model on. This is automatically configured in the deployment process.
  • sklearn version = This is automatically defined in the deployment process

Loading a saved model

You can use Pickle to load the saved model:

import pickle

loaded_model = pickle.load(open("example_model.sav", "rb"))

We can use the scaler in the loaded model to scale the input data:

input_data = loaded_model['scaler'].transform([[24, 500, 0, 0, 400,
                                                       500, 10, 1, 12, 1, 0]])

Now the data is scaled, we can get a prediction from the packaged model:

loaded_model['model'].predict_proba(input_data)[0][1]