{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Classification Model" ] }, { "cell_type": "code", "execution_count": 2, "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "cuisines_df = pd.read_csv(\"../data/cleaned_cuisines.csv\")\n", "cuisines_df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 indian\n", "1 indian\n", "2 indian\n", "3 indian\n", "4 indian\n", "Name: cuisine, dtype: object" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cuisines_label_df = cuisines_df['cuisine']\n", "cuisines_label_df.head()" ] }, { "cell_type": "code", "execution_count": 4, "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": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cuisines_feature_df = cuisines_df.drop(['Unnamed: 0', 'cuisine'], axis=1)\n", "cuisines_feature_df.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from sklearn.neighbors import KNeighborsClassifier\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.svm import SVC\n", "from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier\n", "from sklearn.model_selection import train_test_split, cross_val_score\n", "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, precision_recall_curve\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "x_train, x_test, y_train, y_test = train_test_split(cuisines_feature_df, cuisines_label_df, test_size=0.3)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "c = 10\n", "#create different classifiers\n", "classifiers = {\n", " 'Linear SVC' : SVC(kernel='linear', C=c, probability=True, random_state=0),\n", " 'KNN Classifier' : KNeighborsClassifier(c),\n", " 'SVC' : SVC(kernel='rbf', C = 10, gamma=\"scale\"), #Support Vector Classifier\n", " 'RFST': RandomForestClassifier(n_estimators=100), #Emsamble Cllasifier\n", " 'ADA': AdaBoostClassifier(n_estimators=100)\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuraccy (train) of Linear SVC: 81.0%\n", " precision recall f1-score support\n", "\n", " chinese 0.71 0.75 0.73 235\n", " indian 0.90 0.88 0.89 254\n", " japanese 0.83 0.76 0.79 236\n", " korean 0.86 0.77 0.82 247\n", " thai 0.76 0.89 0.82 227\n", "\n", " accuracy 0.81 1199\n", " macro avg 0.81 0.81 0.81 1199\n", "weighted avg 0.81 0.81 0.81 1199\n", "\n", "Accuraccy (train) of KNN Classifier: 74.4%\n", " precision recall f1-score support\n", "\n", " chinese 0.67 0.72 0.70 235\n", " indian 0.81 0.77 0.79 254\n", " japanese 0.68 0.81 0.74 236\n", " korean 0.92 0.57 0.71 247\n", " thai 0.72 0.85 0.78 227\n", "\n", " accuracy 0.74 1199\n", " macro avg 0.76 0.75 0.74 1199\n", "weighted avg 0.76 0.74 0.74 1199\n", "\n", "Accuraccy (train) of SVC: 84.1%\n", " precision recall f1-score support\n", "\n", " chinese 0.80 0.80 0.80 235\n", " indian 0.90 0.91 0.90 254\n", " japanese 0.90 0.77 0.83 236\n", " korean 0.87 0.81 0.83 247\n", " thai 0.76 0.93 0.84 227\n", "\n", " accuracy 0.84 1199\n", " macro avg 0.84 0.84 0.84 1199\n", "weighted avg 0.85 0.84 0.84 1199\n", "\n", "Accuraccy (train) of RFST: 85.9%\n", " precision recall f1-score support\n", "\n", " chinese 0.83 0.81 0.82 235\n", " indian 0.92 0.91 0.92 254\n", " japanese 0.88 0.81 0.85 236\n", " korean 0.88 0.83 0.85 247\n", " thai 0.79 0.93 0.86 227\n", "\n", " accuracy 0.86 1199\n", " macro avg 0.86 0.86 0.86 1199\n", "weighted avg 0.86 0.86 0.86 1199\n", "\n", "Accuraccy (train) of ADA: 69.7%\n", " precision recall f1-score support\n", "\n", " chinese 0.63 0.61 0.62 235\n", " indian 0.87 0.79 0.83 254\n", " japanese 0.52 0.83 0.64 236\n", " korean 0.84 0.64 0.72 247\n", " thai 0.79 0.62 0.69 227\n", "\n", " accuracy 0.70 1199\n", " macro avg 0.73 0.70 0.70 1199\n", "weighted avg 0.73 0.70 0.70 1199\n", "\n" ] } ], "source": [ "n_classifiers = len(classifiers)\n", "for index, (name, classifier) in enumerate(classifiers.items()):\n", " classifier.fit(x_train, np.ravel(y_train))\n", " y_pred = classifier.predict(x_test)\n", " accuracy = accuracy_score(y_test, y_pred)\n", " print(\"Accuraccy (train) of %s: %0.1f%%\" %(name, accuracy * 100))\n", " print(classification_report(y_test, y_pred))\n", "\n", "\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" }, "metadata": { "interpreter": { "hash": "70b38d7a306a849643e446cd70466270a13445e5987dfa1344ef2b127438fa4d" } } }, "nbformat": 4, "nbformat_minor": 4 }