<jupyter_start><jupyter_text>Scikit-Learn IRIS Model using jsonData * Wrap a scikit-learn python model for use as a prediction microservice in seldon-core   * Run locally on Docker to test   * Deploy on seldon-core running on minikube  Dependencies * [Helm](https://github.com/kubernetes/helm) * [Minikube](https://github.com/kubernetes/minikube) * [S2I](https://github.com/openshift/source-to-image)```bashpip install sklearnpip install seldon-core``` Train locally<jupyter_code>import numpy as np
import os
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.externals import joblib
from sklearn import datasets

def main():
    clf = LogisticRegression()
    p = Pipeline([('clf', clf)])
    print('Training model...')
    p.fit(X, y)
    print('Model trained!')

    filename_p = 'IrisClassifier.sav'
    print('Saving model in %s' % filename_p)
    joblib.dump(p, filename_p)
    print('Model saved!')
    
if __name__ == "__main__":
    print('Loading iris data set...')
    iris = datasets.load_iris()
    X, y = iris.data, iris.target
    print('Dataset loaded!')
    main()<jupyter_output>Loading iris data set...
Dataset loaded!
Training model...
Model trained!
Saving model in IrisClassifier.sav
Model saved!<jupyter_text>REST test Wrap model using s2i<jupyter_code>!s2i build . seldonio/seldon-core-s2i-python3:0.13 sklearn-iris-jsondata:0.1<jupyter_output>---> Installing application source...
---> Installing dependencies ...
Looking in links: /whl
Collecting scikit-learn==0.19.0 (from -r requirements.txt (line 1))
  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.
Downloading https://files.pythonhosted.org/packages/a4/b3/209652a5d60ce4a2a8a35ad893d7565bbb0f87ce043264ba5c9e7de304cd/scikit_learn-0.19.0-cp36-cp36m-manylinux1_x86_64.whl (12.4MB)
Collecting scipy==0.18.1 (from -r requirements.txt (line 2))
  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.
Downloading https://files.pythonhosted.org/packages/74/c0/f0bf4eaef1b6aa7bdd1ae5597ce1d9e729417b3ca085c47d0f1c640d34f8/scipy-0.18.1-cp36-cp36m-manylinux1_x86_64.whl (42.5MB)
Installing collected packages: scikit-learn, scipy
Successfully installed scikit-learn-0.19.0 scipy-0.18.1
WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.
WARNING: You are using pip versi[...]<jupyter_text>Serve model<jupyter_code>!docker run --name "iris_predictor" -d --rm -p 5000:5000 sklearn-iris-jsondata:0.1
%%bash

#
# Send some random features to model
#

curl -sg http://localhost:5000/predict --data-urlencode 'json=
{
    "jsonData": {
        "some_data": {
            "names": [
                "sepal_length",
                "sepal_width",
                "petal_length",
                "petal_width"
            ],
            "some_ndarray": [
                [
                    7.233,
                    4.652,
                    7.39,
                    0.324
                ]
            ]
        }
    }
}' | jq<jupyter_output>{
  "data": {
    "names": [
      "t:0",
      "t:1",
      "t:2"
    ],
    "tensor": {
      "shape": [
        1,
        3
      ],
      "values": [
        0.0011809149535246906,
        0.7867455841528153,
        0.21207350089366006
      ]
    }
  },
  "meta": {}
}<jupyter_text>Stop serving model<jupyter_code>!docker rm iris_predictor --force<jupyter_output>iris_predictor<jupyter_text>Test using Minikube<jupyter_code>!minikube start --memory 4096<jupyter_output><empty_output><jupyter_text>Setup Seldon CoreUse the setup notebook to [Setup Cluster](../../seldon_core_setup.ipynbSetup-Cluster) with [Ambassador Ingress](../../seldon_core_setup.ipynbAmbassador) and [Install Seldon Core](../../seldon_core_setup.ipynbInstall-Seldon-Core). Instructions [also online](./seldon_core_setup.html).  Wrap Model and Test<jupyter_code>!eval $(minikube docker-env) && s2i build . seldonio/seldon-core-s2i-python3:0.13 sklearn-iris-jsondata:0.1
!kubectl create -f sklearn_iris_jsondata_deployment.json
!kubectl rollout status deploy/$(kubectl get deploy -l seldon-app=sklearn-iris-deployment-sklearn-iris-predictor --no-headers=true|cut -d' ' -f1)<jupyter_output>Waiting for deployment "sklearn-iris-deployment-sklearn-iris-predictor-5d3a732" rollout to finish: 0 of 1 updated replicas are available...
deployment "sklearn-iris-deployment-sklearn-iris-predictor-5d3a732" successfully rolled out<jupyter_text>Test by sending prediction calls<jupyter_code>%%bash

PAYLOAD=$(
cat <<EOF
{
    "jsonData": {
        "some_data": {
            "names": [
                "sepal_length",
                "sepal_width",
                "petal_length",
                "petal_width"
            ],
            "some_ndarray": [
                [
                    7.233,
                    4.652,
                    7.39,
                    0.324
                ]
            ]
        }
    }
}
EOF
)

NAMESPACE_NAME=default
DEPLOYMENT_NAME=seldon-deployment-example

HOST=$(minikube ip)
PORT=$(kubectl get svc ambassador -o jsonpath='{.spec.ports[0].nodePort}')

set -x
curl -s -H "Content-Type: application/json" \
    -d "${PAYLOAD}" \
    "http://${HOST}:${PORT}/seldon/${NAMESPACE_NAME}/${DEPLOYMENT_NAME}/api/v0.1/predictions" | jq
set +x
!minikube delete<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12523225566703827, 'fraction_numerical': 0.049238201412114456, 'mean_word_length': 2.8866425992779785, 'pattern_counts': {'":': 15, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '96589', 'license': 'Apache-2.0', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1899, 'n_words': 381, 'path': 'examples/models/sklearn_iris_jsondata/sklearn_iris_jsondata.ipynb', 'repo_name': 'nav13n/seldon-core', 'size': '5382'}