Amplify'd ML Model Deployment - Part III: Exporting scikit-learn Models

Subscribe to my newsletter and never miss my upcoming articles

A new Python library is developed - sklearn-model (Github Link) which aids in exporting scikit-learn models as per the JSON Machine Learning Model specification which was discussed in detail in the previous section.


PyPI - Python Version PyPI version

The library can be installed using pip

pip3 install sklearn-model


Check out the following Jupyter notebooks in the examples directory.

Notebook Snapshot - Check out the notebook here

Screenshot 2021-03-01 at 7.40.05 AM.png

Notebook Snapshot - Check out the notebook here

Screenshot 2021-03-01 at 7.40.32 AM.png

Notebook Snapshot - Check out the notebook here

Screenshot 2021-03-01 at 7.40.44 AM.png


The following examples show the usage of methods of the Model class in sklearn_model.export which aid in the process of model export:

  • Model initialization -
    mdl = Model()
  • Adding input and output fields -

    mdl.add_fields(dfX, dfY)

    where dfX is the dataframe containing the dataset with independent variables and dfY (optional for unsupervised ML) containing the target.

  • Adding output field for unsupervised learning algorithms like Kmeans -

    mdl.add_output_field("species", "category", values = ['versicolor', 'setosa', 'virginica' ])

    In unsupervised learning method like Kmeans the cluster ids are studied after clustering and labels are assigned for each cluster. In the above case the three labels are the species 'versicolor' (cluster id 0), 'setosa' (cluster id 1) and 'virginica' (cluster id 2) which were determined after further study. These labels are now assigned using add_output_field() method.

  • (Optional) Adding transformer -

    mdl.add_transformer(scaler, list(df.columns))
  • Adding the developed ML model -

  • For Decision Trees, the final classes of the leaves can be provided using the add_leaf_class(leaf_class) method as this data is not directly available in the scikit-learn decision tree model.

Now the final JSON model can be obtained in a string form using -

model_json_str = mdl.exportJSON()

Also it can be directly exported to a file by providing the file_path as an argument -


Issues & Contribution

Since, this library is released under Open-source license, please feel free to raise an issue in case of any query or send a pull request to contribute.

Github Link

Previous: Amplify'd ML Model Deployment - Part II: The Model Exchange Specification
Next: Amplify'd ML Model Deployment - Part IV: Model Scoring Application in Flutter

No Comments Yet