<jupyter_start><jupyter_text>Pipeline and Export* https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html* https://scikit-learn.org/stable/modules/model_persistence.html<jupyter_code>!rm -rf data && mkdir data && cd data && curl https://raw.githubusercontent.com/DJCordhose/deep-learning-crash-course-notebooks/master/data/insurance-customers-1500.csv -O && cd -
!ls -l data
import pandas as pd
df = pd.read_csv('data/insurance-customers-1500.csv', sep=';')
stats = df.describe()
stats
y=df['group']
df.drop('group', axis='columns', inplace=True)
X = df.values
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier(max_depth=13, min_samples_leaf=1, min_samples_split=8, n_estimators=14, n_jobs=-1)
%time clf.fit(X_train, y_train)
train_score = clf.score(X_train, y_train)
train_score
test_score = clf.score(X_test, y_test)
test_score
from sklearn.model_selection import cross_val_score
cross_val_scores = cross_val_score(clf, X_train, y_train, n_jobs=-1, cv=10)
cross_val_scores
# mean score and the 95% confidence interval (2 standard deviations assuming normal distribution)
print("Accuracy: %0.2f (+/- %0.2f)" % (cross_val_scores.mean(), cross_val_scores.std() * 2))
scores = {
    'cross_val_scores': cross_val_scores,
    'train_score': train_score,
    'test_score': test_score
}
scores
!rm -rf models && mkdir models
import pickle

pickle.dump(clf, open('models/model.pickle', 'wb'))
!rm -rf stats && mkdir stats
# has the distribution of the data changed substantially?
pickle.dump(stats, open('stats/describe.pickle', 'wb'))
# has the score descread since last time or is suspiciously different?
pickle.dump(scores, open('stats/scores.pickle', 'wb'))
import numpy, sklearn, pandas

versions = {
    'numpy': numpy.__version__,
    'sklearn': sklearn.__version__, 
    'pandas': pandas.__version__,
    'model': 1,
    'model_name': 'plain_rf'

}
versions
# we need to have exactly the same versions for serving
pickle.dump(versions, open('stats/versions.pickle', 'wb'))
!ls -l stats
!ls -l
# we also need the code that created this to have it reproduceable
!cd models
!curl https://raw.githubusercontent.com/DJCordhose/ml-workshop/master/notebooks/process/export.ipynb -O
!cd -
!ls -l models
# have everything we need to reproduce and compare to other versions here
# better to version this 
# often versioning data is a challenge simply because of its size
!tar -czf prod.tgz models stats data
!ls -l
!date<jupyter_output>Thu Jul 25 10:43:21 UTC 2019<jupyter_text>Exercise: Export your favorite model* choose any strategy you like* save all the data you need for serving* can you come up with some automated safety check for a new model?* should you deploy to production automatically anyway?<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11586985391766268, 'fraction_numerical': 0.014276228419654714, 'mean_word_length': 5.578602620087336, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '383333', 'license': 'MIT', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1010, 'n_words': 283, 'path': 'notebooks/process/export.ipynb', 'repo_name': 'kmunve/ml-workshop', 'size': '3012'}