diff --git a/4-Classification/1-Introduction/notebook.ipynb b/4-Classification/1-Introduction/notebook.ipynb index f85e06e6..fd862c35 100644 --- a/4-Classification/1-Introduction/notebook.ipynb +++ b/4-Classification/1-Introduction/notebook.ipynb @@ -747,7 +747,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.13.3" }, "orig_nbformat": 2 }, diff --git a/4-Classification/4-Applied/model.onnx b/4-Classification/4-Applied/model.onnx new file mode 100644 index 00000000..4b27b87f Binary files /dev/null and b/4-Classification/4-Applied/model.onnx differ diff --git a/4-Classification/4-Applied/notebook.ipynb b/4-Classification/4-Applied/notebook.ipynb index a1f5a65c..f339d469 100644 --- a/4-Classification/4-Applied/notebook.ipynb +++ b/4-Classification/4-Applied/notebook.ipynb @@ -1,5 +1,1084 @@ { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Build a cuisine recommender" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + "[notice] A new release of pip is available: 25.0.1 -> 25.2\n", + "[notice] To update, run: python.exe -m pip install --upgrade pip\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: skl2onnx in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (1.19.1)\n", + "Requirement already satisfied: onnx>=1.2.1 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from skl2onnx) (1.19.0)\n", + "Requirement already satisfied: scikit-learn>=1.1 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from skl2onnx) (1.6.1)\n", + "Requirement already satisfied: numpy>=1.22 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from onnx>=1.2.1->skl2onnx) (2.2.6)\n", + "Requirement already satisfied: protobuf>=4.25.1 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from onnx>=1.2.1->skl2onnx) (6.32.1)\n", + "Requirement already satisfied: typing_extensions>=4.7.1 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from onnx>=1.2.1->skl2onnx) (4.14.0)\n", + "Requirement already satisfied: ml_dtypes in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from onnx>=1.2.1->skl2onnx) (0.5.3)\n", + "Requirement already satisfied: scipy>=1.6.0 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from scikit-learn>=1.1->skl2onnx) (1.15.3)\n", + "Requirement already satisfied: joblib>=1.2.0 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from scikit-learn>=1.1->skl2onnx) (1.5.1)\n", + "Requirement already satisfied: threadpoolctl>=3.1.0 in d:\\ai\\machinelearning\\ml-for-beginners\\.venv\\lib\\site-packages (from scikit-learn>=1.1->skl2onnx) (3.6.0)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install skl2onnx\n", + "import pandas as pd " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0cuisinealmondangelicaaniseanise_seedappleapple_brandyapricotarmagnac...whiskeywhite_breadwhite_winewhole_grain_wheat_flourwinewoodyamyeastyogurtzucchini
00indian00000000...0000000000
11indian10000000...0000000000
22indian00000000...0000000000
33indian00000000...0000000000
44indian00000000...0000000010
\n", + "

5 rows × 382 columns

\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 cuisine almond angelica anise anise_seed apple \\\n", + "0 0 indian 0 0 0 0 0 \n", + "1 1 indian 1 0 0 0 0 \n", + "2 2 indian 0 0 0 0 0 \n", + "3 3 indian 0 0 0 0 0 \n", + "4 4 indian 0 0 0 0 0 \n", + "\n", + " apple_brandy apricot armagnac ... whiskey white_bread white_wine \\\n", + "0 0 0 0 ... 0 0 0 \n", + "1 0 0 0 ... 0 0 0 \n", + "2 0 0 0 ... 0 0 0 \n", + "3 0 0 0 ... 0 0 0 \n", + "4 0 0 0 ... 0 0 0 \n", + "\n", + " whole_grain_wheat_flour wine wood yam yeast yogurt zucchini \n", + "0 0 0 0 0 0 0 0 \n", + "1 0 0 0 0 0 0 0 \n", + "2 0 0 0 0 0 0 0 \n", + "3 0 0 0 0 0 0 0 \n", + "4 0 0 0 0 0 1 0 \n", + "\n", + "[5 rows x 382 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = pd.read_csv('../data/cleaned_cuisines.csv')\n", + "data.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Check the column names in the dataset\n", + "print(\"Column names:\")\n", + "print(data.columns.tolist())\n", + "print(\"\\nFirst few rows:\")\n", + "data.head()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
almondangelicaaniseanise_seedappleapple_brandyapricotarmagnacartemisiaartichoke...whiskeywhite_breadwhite_winewhole_grain_wheat_flourwinewoodyamyeastyogurtzucchini
00000000000...0000000000
11000000000...0000000000
20000000000...0000000000
30000000000...0000000000
40000000000...0000000010
\n", + "

5 rows × 380 columns

\n", + "
" + ], + "text/plain": [ + " almond angelica anise anise_seed apple apple_brandy apricot \\\n", + "0 0 0 0 0 0 0 0 \n", + "1 1 0 0 0 0 0 0 \n", + "2 0 0 0 0 0 0 0 \n", + "3 0 0 0 0 0 0 0 \n", + "4 0 0 0 0 0 0 0 \n", + "\n", + " armagnac artemisia artichoke ... whiskey white_bread white_wine \\\n", + "0 0 0 0 ... 0 0 0 \n", + "1 0 0 0 ... 0 0 0 \n", + "2 0 0 0 ... 0 0 0 \n", + "3 0 0 0 ... 0 0 0 \n", + "4 0 0 0 ... 0 0 0 \n", + "\n", + " whole_grain_wheat_flour wine wood yam yeast yogurt zucchini \n", + "0 0 0 0 0 0 0 0 \n", + "1 0 0 0 0 0 0 0 \n", + "2 0 0 0 0 0 0 0 \n", + "3 0 0 0 0 0 0 0 \n", + "4 0 0 0 0 0 1 0 \n", + "\n", + "[5 rows x 380 columns]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X = data.iloc[:, 2:]\n", + "X.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
cuisine
0indian
1indian
2indian
3indian
4indian
\n", + "
" + ], + "text/plain": [ + " cuisine\n", + "0 indian\n", + "1 indian\n", + "2 indian\n", + "3 indian\n", + "4 indian" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y = data[['cuisine']] \n", + "y.head()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split\n", + "from sklearn.svm import SVC\n", + "from sklearn.model_selection import cross_val_score\n", + "from sklearn.metrics import accuracy_score, precision_score, confusion_matrix, classification_report" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
SVC(C=10, kernel='linear', probability=True, random_state=0)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "SVC(C=10, kernel='linear', probability=True, random_state=0)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)\n", + "model = SVC(kernel='linear', C=10, probability=True, random_state=0)\n", + "model.fit(X_train, y_train.values.ravel())" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "y_pred = model.predict(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " precision recall f1-score support\n", + "\n", + " chinese 0.73 0.74 0.74 244\n", + " indian 0.87 0.87 0.87 240\n", + " japanese 0.76 0.75 0.75 241\n", + " korean 0.84 0.78 0.81 235\n", + " thai 0.77 0.84 0.80 239\n", + "\n", + " accuracy 0.79 1199\n", + " macro avg 0.80 0.80 0.80 1199\n", + "weighted avg 0.80 0.79 0.79 1199\n", + "\n" + ] + } + ], + "source": [ + "print(classification_report(y_test, y_pred))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "from skl2onnx import convert_sklearn\n", + "from skl2onnx.common.data_types import FloatTensorType\n", + "\n", + "initial_type = [('float_input', FloatTensorType([None, 380]))]\n", + "options = {id(model): {'nocl': True, 'zipmap': False}}" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "onx = convert_sklearn(model, initial_types=initial_type, options= options)\n", + "with open(\"./model.onnx\", \"wb\") as f:\n", + " f.write(onx.SerializeToString())" + ] + } + ], "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, "language_info": { "codemirror_mode": { "name": "ipython", @@ -10,19 +1089,10 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": 3 + "version": "3.13.3" }, "orig_nbformat": 4 }, "nbformat": 4, - "nbformat_minor": 2, - "cells": [ - { - "source": [ - "# Build a cuisine recommender" - ], - "cell_type": "markdown", - "metadata": {} - } - ] -} \ No newline at end of file + "nbformat_minor": 2 +}