Package: scikit-endpoint
pure-predictspeeds up and slims down machine learning prediction applications. It is
a foundational tool for serverless inference or small batch prediction with popular machine
learning frameworks likescikit-learnandfasttext.
It implements the predict methods of these frameworks in pure Python.Primary Use CasesThe primary use case forpure-predictis the following scenario:A model is trained in an environment without strong container footprint constraints. Perhaps a long running “offline” job on one or many machines where installing a number of python packages from PyPI is not at all problematic.At prediction time the model needs to be served behind an API. Typical access patterns are to request a prediction for one “record” (one “row” in anumpyarray or one string of text to classify) per request or a mini-batch of records per request.Preferred infrastructure for the prediction service is either serverless (AWS Lambda) or a container service where the memory footprint of the container is constrained.The fitted model object’s artifacts needed for prediction (coefficients, weights, vocabulary, decision tree artifacts, etc.) are relatively small (10s to 100s of MBs).In this scenario, a container service with a large dependency footprint can be overkill for a microservice, particularly if the access patterns favor the pricing model of a serverless application. Additionally, for smaller models and single record predictions per request, thenumpyandscipyfunctionality in the prediction methods of popular machine learning frameworks work against the application in terms of latency,underperforming pure pythonin some cases.Check out theblog postfor more information on the motivation and use cases ofpure-predict.Package DetailsIt is a Python package for machine learning prediction distributed under
theApache 2.0 software license.
It contains multiple subpackages which mirror their open source
counterpart (scikit-learn,fasttext, etc.). Each subpackage has utilities to
convert a fitted machine learning model into a custom object containing prediction methods
that mirror their native counterparts, but converted to pure python. Additionally, all
relevant model artifacts needed for prediction are converted to pure python.Apure-predictmodel object can then be pickled and later
unpickled without any 3rd party dependencies other thanpure-predict.This eliminates the need to have large dependency packages installed in order to
make predictions with fitted machine learning models using popular open source packages for
training models. These dependencies (numpy,scipy,scikit-learn,fasttext, etc.)
are large in size andnot always necessary to make fast and accurate
predictions.
Additionally, they rely on C extensions that may not be ideal for serverless applications with a python runtime.Quick Start ExampleIn a python enviornment withscikit-learnand its dependencies installed:importpicklefromsklearn.ensembleimportRandomForestClassifierfromsklearn.datasetsimportload_irisfrompure_sklearn.mapimportconvert_estimator# fit sklearn estimatorX,y=load_iris(return_X_y=True)clf=RandomForestClassifier()clf.fit(X,y)# convert to pure python estimatorclf_pure_predict=convert_estimator(clf)withopen("model.pkl","wb")asf:pickle.dump(clf_pure_predict,f)# make prediction with sklearn estimatory_pred=clf.predict([[0.25,2.0,8.3,1.0]])print(y_pred)[2]In a python enviornment with onlypure-predictinstalled:importpickle# load pickled modelwithopen("model.pkl","rb")asf:clf=pickle.load(f)# make prediction with pure-predict objecty_pred=clf.predict([[0.25,2.0,8.3,1.0]])print(y_pred)[2]Subpackagespure_sklearnPrediction in pure python for a subset ofscikit-learnestimators and transformers.estimatorslinear models- supports the majority of linear models for classificationtrees- decision trees, random forests, gradient boosting and xgboostnaive bayes- a number of popular naive bayes classifierssvm- linear SVCtransformerspreprocessing- normalization and onehot/ordinal encodersimpute- simple imputationfeature extraction- text (tfidf, count vectorizer, hashing vectorizer) and dictionary vectorizationpipeline- pipelines and feature unionsSparse data - supports a custom pure python sparse data object - sparse data is handled as would be expected by the relevent transformers and estimatorspure_fasttextPrediction in pure python forfasttext.supervised- predicts labels for supervised models; no support for quantized models (blocked bythis issue)unsupervised- lookup of word or sentence embeddings given input textInstallationDependenciespure-predictrequires:Python(>= 3.6)Dependency Notespure_sklearnhas been tested withscikit-learnversions >= 0.20 – certain functionality may work with lower versions but are not guaranteed. Some functionality is explicitly not supported for certainscikit-learnversions and exceptions will be raised as appropriate.xgboostrequires version >= 0.82 for support withpure_sklearn.pure-predictis not supported with Python 2.fasttextversions <= 0.9.1 have been tested.User InstallationThe easiest way to installpure-predictis withpip:pip install --upgrade pure-predictYou can also download the source code:git clone https://github.com/Ibotta/pure-predict.gitTestingWithpytestinstalled, you can run tests locally:pytest pure-predictExamplesThe package containsexampleson how to usepure-predictin practice.Calls for ContributorsContributing topure-predictiswelcomed by any contributors. Specific calls for contribution are as follows:Examples, tests and documentation – particularly more detailed examples with performance testing of various estimators under various constraints.Adding morepure_sklearnestimators. Thescikit-learnpackage is extensive and only partially covered bypure_sklearn.Regressiontasks in particular missing frompure_sklearn.Clustering,dimensionality reduction,nearest neighbors,feature selection, non-linearSVM, and more are also omitted and would be good candidates for extendingpure_sklearn.General efficiency. There is likely low hanging fruit for improving the efficiency of thenumpyandscipyfunctionality that has been ported topure-predict.Threadingcould be considered to improve performance – particularly for making predictions with multiple records.A publicAWS lambda layercontainingpure-predict.BackgroundThe project was started atIbotta
Inc.on the machine learning
team and open sourced in 2020. It is currently maintained by the machine
learning team at Ibotta.AcknowledgementsThanks toDavid MitchellandAndrew Tilleyfor internal review before open source. Thanks toJames Foleyfor logo artwork.