Package: django-estimators
Django-EstimatorsTidy Persistence and Retrieval for Machine LearningIntroDjango-Estimators helps persist and track machine learning models (aka estimators) and datasets.This library provides a series of proxy objects that wrap common python machine learning objects and dataset objects.  As a result, this library can be used to version, track progress and deploy models.  It’s highly extensible and can be used with almost any python object (scikit-learn, numpy arrays, modules, methods).This repo utilizes django as an ORM.  If you’d like to work outside of django, try the sqlalchemy-basedestimatorslibrary instead.InstallationDjango-estimators is on PyPI, so just run:pip install django-estimatorsQuick start1. Add “estimators” to your INSTALLED_APPS django setting like thisINSTALLED_APPS = [
    ...
    'estimators',
]2. To create the estimators table, runpython manage.py migrate3. Runpython manage.py shelland get create new models like sofrom sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()

from estimators.models import Estimator
est = Estimator(estimator=rfc)
est.description = 'a simple forest'
est.save()4.  Retrieve your model, using the classic django orm, we can pull the last Estimatorest = Estimator.objects.last()
# now use your estimator
est.estimator.predict(X)Use Case: Retrieving Models/EstimatorsIf you aren’t sure if it exists, the recommended method is to use theget_or_createmethodest = Estimator.objects.get_or_create(estimator=object)
# or potentially update it with update_or_create
est = Estimator.objects.update_or_create(estimator=object)If you already have the model, in this case of type objectest = Estimator.objects.filter(estimator=object).first()If you know the unique hash of the modelest = Estimator.objects.filter(object_hash='d9c9f286391652b89978a6961b52b674').first()Use Case: Persisting and Retrieving DataSetsTheDataSetclass functions just like theEstimatorclass.  If you have
a numpy matrix or a pandas dataframe, you can wrap it with a DataSet objectimport numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(0,10,(100,8)))

from estimators.models import DataSet

ds = DataSet(data=df)
ds.save()You can pull that same DataSet object later withds = DataSet.objects.latest('create_date')And if you already have the datasetds = DataSet.objects.filter(data=df).first()Use Case: Persisting Predictions and ResultsSometimes the most valuable part of a machine learning is the whole process.
Using anEvaluatorobject, we can define the relationships between X_test, y_test and
y_predicted ahead of time.Then we can evaluate the evaluation plan, which in turn calls thepredictmethod on the estimator
and then presists all the wrapped objects.Here’s a demo of using an Evaluator.from sklearn.datasets import load_digits
from sklearn.ensemble import RandomForestClassifier

digits = load_digits() # 1797 by 64
X = digits.data
y = digits.target

# simple splitting for validation testing
X_train, X_test = X[:1200], X[1200:]
y_train, y_test = y[:1200], y[1200:]

rfc = RandomForestClassifier()
rfc.fit(X_train, y_train)Now create your evaluation planfrom estimators.models import Evaluator
plan = Evaluator(X_test=X_test, y_test=y_test, estimator=rfc)

result = plan.evaluate() # executes `predict` method on X_testAnd you can view all the atributes on the evaluation resultresult.estimator
result.X_test
result.y_test # optional, used with supervised classifiers
result.y_predictedUsing with Jupyter Notebook (or without a django app)Django-Estimators can run as a standalone django app.In order to have access to the django db, you’ll need to set up the environment variable to load up your django project.  In ipython, by default you can set the environment variableDJANGO_SETTINGS_MODULEtoestimators.template_settingslike soimport os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = "estimators.template_settings"
django.setup()If you’re creating a new database (by default it’sdb.sqlite3).  Therefore we need to run migrations, so in pythonfrom django.core.management import call_command
call_command('migrate')Now you can continue you as usual…from estimators.models import EstimatorTo use your own custom settings, make a copy of theestimators.template_settingsand edit the fields.  Like above, runos.environ['DJANGO_SETTINGS_MODULE']= "custom_settings_file"before runningdjango.setup().Development InstallationTo install the latest version of django-estimators, clone the repo, change directory to the repo, and pip install it into your current virtual environment.:$ git clone git@github.com:fridiculous/django-estimators.git
$ cd django-estimators
$ <activate your project’s virtual environment>
(virtualenv) $ pip install -e .  # the dot specifies for this current repo