Sharing your Machine Learning models ?
image taken from https://www.interviewbit.com/blog/python-libraries/

Sharing your Machine Learning models ?

A lot of time and effort is spent on cleaning the dataset and selecting the right model, then fine-tuning the hypermeters to give the desired success metrics. I've always wondered what next ? what is the best way to share a ML model once it's been trained and tested ? What is the widely accepted standard in different industries ?

I recently came across the library called 'pickle'. As per the documentation, "The?pickle?module implements binary protocols for serializing and de-serializing a Python object structure.?“Pickling”?is the process whereby a Python object hierarchy is converted into a byte stream, and?“unpickling”?is the inverse operation, whereby a byte stream is converted back into an object hierarchy."

In simpler terms, you can export your model into a file with extension ".(dot)pkl" which can be shared with others. To use this library, you first import it.

import pickle        

Next step is to open a file called 'model.pkl' with an intention to 'write' in binary (i.e. wb) and use pickle.dump our model into the file.

pickle.dump(model, open('model.pkl', 'wb'))        

This will create a file called 'model.pkl' in your root directory which can be shared with others.

To load your model from a pickle file, use pickle.load. You can now pass any argument (i.e: X_test) to get a prediction from the model that was un-pickled.

pickled_model = pickle.load(open('model.pkl', 'rb')
pickled_model.predict(X_test))        

For the entire code on how to split the dataset into training and testing sets, creating the model, generating predictions and saving the model into a pickle file, here is my github repo for reference.

Advait Raje

Getting more people on bikes using Data, Northwestern Alumni

1 年

Pickle and Un-Pickle. Gives me a Chuckle!!

要查看或添加评论,请登录

Varun Lobo的更多文章

社区洞察

其他会员也浏览了