{ "info": { "author": "Khaled Sharif", "author_email": "kldsrf@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "# sklearn2docker\n#### Convert your trained scikit-learn classifier to a Docker container with a pre-configured API\n\n[![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](http://www.gnu.org/licenses/lgpl-3.0)\n\n## Installation\n\nThe easiest way to install `sklearn2docker` with all its dependencies is through `pip`:\n\n```bash\npip install git+git://github.com/KhaledSharif/sklearn2docker.git\n```\n\n## Getting started\n\nFirst, create your `sklearn` classifier. In this example we will use the [Iris dataset](http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html).\n\n```python\nfrom pandas import DataFrame\nfrom sklearn.datasets import load_iris\nfrom sklearn.tree import DecisionTreeClassifier\n\niris = load_iris()\ninput_df = DataFrame(data=iris['data'], columns=iris['feature_names'])\nclf = DecisionTreeClassifier(max_depth=2)\nclf.fit(input_df.values, iris['target'])\n```\n\nSecond, import the `Sklearn2Docker` class and use it to build your container.\n\n```python\nfrom sklearn2docker.constructor import Sklearn2Docker\n\ns2d = Sklearn2Docker(\n classifier=clf,\n feature_names=iris['feature_names'],\n class_names=iris['target_names'].tolist()\n)\ns2d.save(name=\"classifier\", tag=\"iris\")\n```\n\nThe name and tag arguments we passed to the `save` function are the name and tag of the Docker container we just built ([see: `docker tag`](https://docs.docker.com/engine/reference/commandline/tag/)). Below is an example of the output of the `s2d.save()` line we executed above.\n\n```\nNow attempting to run the command: \n[docker build --file /tmp/tmpywbu3_ad/Dockerfile \n --tag classifier:iris /tmp/tmpywbu3_ad]\n=====================================================================\n> Sending build context to Docker daemon\n> Step 1/6 : FROM python:3.6\n> ---> c1e459c00dc3\n... output truncated ...\n> Step 6/6 : ENTRYPOINT python /code/api.py\n> ---> Running in bd61983358d9\n> Removing intermediate container bd61983358d9\n> ---> fa2041ac6d60\n> Successfully built fa2041ac6d60\n> Successfully tagged classifier:iris\n=====================================================================\nSuccess! You can now run your Docker container using the following command:\n\t docker run -d -p 5000:5000 classifier:iris\n```\n\nYou can now test your container by asking it to predict the same Iris dataset and return the predicted probabilities ([see: `predict_proba`](http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier.predict_proba)) as a DataFrame.\n\n```python\nfrom os import system\nsystem(\"docker run -d -p 5000:5000 classifier:iris && sleep 5\")\n\nfrom requests import post\nfrom pandas import read_json\nrequest = post(\"http://localhost:5000/predict_proba/split\", json=input_df.to_json(orient=\"split\"))\nresult = read_json(request.content.decode(), orient=\"split\")\nprint(result.head())\n```\n\n```\n setosa versicolor virginica\n0 1 0.0 0.0\n1 1 0.0 0.0\n2 1 0.0 0.0\n3 1 0.0 0.0\n4 1 0.0 0.0\n```\n\nYou can also request regular classification ([see: `predict`](http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier.predict)). The format for the URL for your Docker container is as so:\n\n```\nhttp://[a]:[b]/[c]/[d]\n\na: the hostname of the container, defaults to `localhost`\nb: the port of the container, defaults to 5000\nc: one of `predict` or `predict_proba`, similar to the scikit-learn api\nd: defaults to `split`; orient of the Pandas DataFrame JSON conversion*\n```\n\n(*: see [this documentation article](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html) for more information about Pandas orients, and [this Github issue](https://github.com/pandas-dev/pandas/issues/18912#issuecomment-354430046) for a comparison; most of the time, setting the orient to `split` should do just fine)\n\n```python\nrequest = post(\n \"http://localhost:5000/predict/split\", \n json=input_df.to_json(orient=\"split\")\n)\n```\n\n```\n prediction\n0 setosa\n1 setosa\n2 setosa\n3 setosa\n4 setosa\n```", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/KhaledSharif/sklearn2docker", "keywords": "machine-learning docker deployment data-science", "license": "", "maintainer": "", "maintainer_email": "", "name": "sklearn2docker", "package_url": "https://pypi.org/project/sklearn2docker/", "platform": "", "project_url": "https://pypi.org/project/sklearn2docker/", "project_urls": { "Homepage": "https://github.com/KhaledSharif/sklearn2docker" }, "release_url": "https://pypi.org/project/sklearn2docker/0.1/", "requires_dist": null, "requires_python": "", "summary": "Convert your trained scikit-learn classifier to a Docker container with a pre-configured API.", "version": "0.1" }, "last_serial": 3581598, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "97375f4724d4d15802881e34c1af8b8a", "sha256": "90781f29d8e8a4accb6568900b272c8655577587cf9f2b6404b4630e8a086c88" }, "downloads": -1, "filename": "sklearn2docker-0.1.tar.gz", "has_sig": false, "md5_digest": "97375f4724d4d15802881e34c1af8b8a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3075, "upload_time": "2018-02-14T14:52:10", "url": "https://files.pythonhosted.org/packages/12/21/01be0cefc165e649bf5d1c0a144f78da550d97837e1a00fe5d05710e773e/sklearn2docker-0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "97375f4724d4d15802881e34c1af8b8a", "sha256": "90781f29d8e8a4accb6568900b272c8655577587cf9f2b6404b4630e8a086c88" }, "downloads": -1, "filename": "sklearn2docker-0.1.tar.gz", "has_sig": false, "md5_digest": "97375f4724d4d15802881e34c1af8b8a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3075, "upload_time": "2018-02-14T14:52:10", "url": "https://files.pythonhosted.org/packages/12/21/01be0cefc165e649bf5d1c0a144f78da550d97837e1a00fe5d05710e773e/sklearn2docker-0.1.tar.gz" } ] }