{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Build Classification Model" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "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]" ], "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
" }, "metadata": {}, "execution_count": 15 } ], "source": [ "import pandas as pd\n", "recipes_df = pd.read_csv(\"../../data/cleaned_cuisine.csv\")\n", "recipes_df.head()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 indian\n", "1 indian\n", "2 indian\n", "3 indian\n", "4 indian\n", "Name: cuisine, dtype: object" ] }, "metadata": {}, "execution_count": 16 } ], "source": [ "recipes_label_df = recipes_df['cuisine']\n", "recipes_label_df.head()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "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]" ], "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
" }, "metadata": {}, "execution_count": 17 } ], "source": [ "recipes_feature_df = recipes_df.drop(['Unnamed: 0', 'cuisine'], axis=1)\n", "recipes_feature_df.head()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from sklearn.linear_model import LogisticRegression\n", "from sklearn.svm import SVC\n", "from sklearn.model_selection import train_test_split, cross_val_score\n", "from sklearn.metrics import accuracy_score,precision_score,confusion_matrix,classification_report, precision_recall_curve\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(recipes_feature_df, recipes_label_df, test_size=0.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Try different classifiers" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "\r\n", "C = 10\r\n", "# Create different classifiers.\r\n", "classifiers = {\r\n", " 'L1 logistic': LogisticRegression(C=C, penalty='l1',\r\n", " solver='saga',\r\n", " multi_class='multinomial',\r\n", " max_iter=10000),\r\n", " 'L2 logistic (Multinomial)': LogisticRegression(C=C, penalty='l2',\r\n", " solver='saga',\r\n", " multi_class='multinomial',\r\n", " max_iter=10000),\r\n", " 'L2 logistic (OvR)': LogisticRegression(C=C, penalty='l2',\r\n", " solver='saga',\r\n", " multi_class='ovr',\r\n", " max_iter=10000),\r\n", " 'Linear SVC': SVC(kernel='linear', C=C, probability=True,\r\n", " random_state=0)\r\n", "}\r\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Accuracy (train) for L1 logistic: 79.9% \n", "Accuracy (train) for L2 logistic (Multinomial): 80.6% \n", "Accuracy (train) for L2 logistic (OvR): 81.2% \n", "Accuracy (train) for Linear SVC: 78.9% \n" ] } ], "source": [ "n_classifiers = len(classifiers)\r\n", "\r\n", "for index, (name, classifier) in enumerate(classifiers.items()):\r\n", " classifier.fit(X_train, np.ravel(y_train))\r\n", "\r\n", " y_pred = classifier.predict(X_test)\r\n", " accuracy = accuracy_score(y_test, y_pred)\r\n", " print(\"Accuracy (train) for %s: %0.1f%% \" % (name, accuracy * 100))\r\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "70b38d7a306a849643e446cd70466270a13445e5987dfa1344ef2b127438fa4d" }, "kernelspec": { "name": "python3", "display_name": "Python 3.7.0 64-bit ('3.7')" }, "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.7.0" }, "metadata": { "interpreter": { "hash": "70b38d7a306a849643e446cd70466270a13445e5987dfa1344ef2b127438fa4d" } } }, "nbformat": 4, "nbformat_minor": 4 }