For a more detailed guide refer to tensorflow
or pytorch
example or to the documentation on https://clipper.ai.
This example uses matplotlib
, in case you cannot use it, please comment out respective 2 cells - 7 & 8
In [2]:
from sklearn.linear_model import LinearRegression
from sklearn import datasets
from joblib import dump, load
import numpy as np
In [3]:
# Load a dataset
diabetes = datasets.load_diabetes() # load data
In [4]:
diabetes.data.shape # feature matrix shape
Out[4]:
In [5]:
# Seperate train and test data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target, test_size=0.2, random_state=0)
# There are three steps to model something with sklearn
# 1. Set up the model
model = LinearRegression()
# 2. Use fit
model.fit(X_train, y_train)
# 3. Check the score
model.score(X_test, y_test)
Out[5]:
In [6]:
model.predict(X_test) # Predict unkown data
Out[6]:
In [7]:
# Using matplotlib, comment it out if you don't want to see the plots
import matplotlib.pylab as plt
%matplotlib inline
# plot prediction and actual data
y_pred = model.predict(X_test)
plt.plot(y_test, y_pred, '.')
# plot a line, a perfit predict would all fall on this line
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()
In [8]:
error = y_pred - y_test
plt.hist(error, bins = 25)
plt.xlabel("Prediction Error")
_ = plt.ylabel("Count")
In [9]:
from joblib import dump, load
dump(model, 'model_scikit.joblib')
print("Model saved")
del(model)
# Let's check if the model variable is really gone
try:
model
except NameError:
print("Model variable removal successful")
In [10]:
# Load model
model = load('model_scikit.joblib')
In [11]:
# Data point for prediction function check
data = [0.01991321, 0.05068012, 0.10480869, 0.07007254, -0.03596778,
-0.0266789 , -0.02499266, -0.00259226, 0.00371174, 0.04034337]
In [12]:
model.predict([data])
Out[12]:
In [13]:
from clipper_admin import ClipperConnection, DockerContainerManager
from clipper_admin.deployers import python as python_deployer
clipper_conn = ClipperConnection(DockerContainerManager())
In [14]:
clipper_conn.start_clipper()
In [16]:
clipper_conn.connect()
clipper_conn.get_all_apps()
Out[16]:
In [17]:
clipper_conn.get_all_models()
Out[17]:
In [18]:
clipper_conn.register_application(name="scikit-app", input_type="doubles", default_output="-1.0", slo_micros=100000)
In [19]:
python_deployer.deploy_python_closure(clipper_conn, name="scikit-mod",
version=1,
input_type="doubles",
func=model.predict,
pkgs_to_install=['scikit-learn'])
In [20]:
clipper_conn.link_model_to_app(
app_name="scikit-app",
model_name="scikit-mod")
In [21]:
clipper_conn.get_all_apps()
Out[21]:
In [22]:
import requests, json, numpy as np
headers = {"Content-type": "application/json"}
requests.post("http://localhost:1337/scikit-app/predict", headers=headers,
data=json.dumps({"input": data})).json()
# The result is as in the local predict function call
Out[22]:
In [23]:
clipper_conn.unlink_model_from_app(model_name="scikit-mod", app_name="scikit-app")
In [24]:
clipper_conn.stop_models('scikit-mod')
In [25]:
clipper_conn.delete_application('scikit-app')
In [26]:
clipper_conn.stop_all()
In [ ]: