Spaces:
Sleeping
Sleeping
Sina Media Lab commited on
Commit ·
16813ae
1
Parent(s): 91b16c9
adding training code
Browse files- iris_classifier.ipynb +101 -0
iris_classifier.ipynb
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 23,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"colab": {
|
| 22 |
+
"base_uri": "https://localhost:8080/"
|
| 23 |
+
},
|
| 24 |
+
"id": "Ee3EGX_zl1Ei",
|
| 25 |
+
"outputId": "13d12370-40e9-4fd6-a77c-6fc3721d2727"
|
| 26 |
+
},
|
| 27 |
+
"outputs": [
|
| 28 |
+
{
|
| 29 |
+
"output_type": "stream",
|
| 30 |
+
"name": "stdout",
|
| 31 |
+
"text": [
|
| 32 |
+
"Model saved as iris_knn.pkl\n"
|
| 33 |
+
]
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
"source": [
|
| 37 |
+
"from sklearn.datasets import load_iris\n",
|
| 38 |
+
"from sklearn.neighbors import KNeighborsClassifier\n",
|
| 39 |
+
"import joblib\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"# Load dataset\n",
|
| 42 |
+
"iris = load_iris()\n",
|
| 43 |
+
"X = iris.data\n",
|
| 44 |
+
"y = iris.target\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"# Train KNN\n",
|
| 47 |
+
"model = KNeighborsClassifier(n_neighbors=5)\n",
|
| 48 |
+
"model.fit(X, y)\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"# Save model\n",
|
| 51 |
+
"joblib.dump((model, iris.target_names), \"iris_knn.pkl\")\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"print(\"Model saved as iris_knn.pkl\")\n"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"source": [
|
| 59 |
+
"import requests\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"url = \"https://tofighi-iris-detector-api.hf.space/predict\"\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"data = {\n",
|
| 64 |
+
" \"sepal_length\": 1.4,\n",
|
| 65 |
+
" \"sepal_width\": 1.3,\n",
|
| 66 |
+
" \"petal_length\": 2.4,\n",
|
| 67 |
+
" \"petal_width\": 1\n",
|
| 68 |
+
"}\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"resp = requests.post(url, json=data)\n",
|
| 71 |
+
"print(resp.json())"
|
| 72 |
+
],
|
| 73 |
+
"metadata": {
|
| 74 |
+
"colab": {
|
| 75 |
+
"base_uri": "https://localhost:8080/"
|
| 76 |
+
},
|
| 77 |
+
"id": "wMe3AXJVt70u",
|
| 78 |
+
"outputId": "9c01d308-679a-461f-f535-ef98851f944d"
|
| 79 |
+
},
|
| 80 |
+
"execution_count": 22,
|
| 81 |
+
"outputs": [
|
| 82 |
+
{
|
| 83 |
+
"output_type": "stream",
|
| 84 |
+
"name": "stdout",
|
| 85 |
+
"text": [
|
| 86 |
+
"{'predicted_class': 'setosa', 'input': {'sepal_length': 1.4, 'sepal_width': 1.3, 'petal_length': 2.4, 'petal_width': 1.0}, 'class_probabilities': {'setosa': 0.8, 'versicolor': 0.2, 'virginica': 0.0}}\n"
|
| 87 |
+
]
|
| 88 |
+
}
|
| 89 |
+
]
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"cell_type": "code",
|
| 93 |
+
"source": [],
|
| 94 |
+
"metadata": {
|
| 95 |
+
"id": "yYAcMc_Zyqpa"
|
| 96 |
+
},
|
| 97 |
+
"execution_count": null,
|
| 98 |
+
"outputs": []
|
| 99 |
+
}
|
| 100 |
+
]
|
| 101 |
+
}
|