# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''base'': conda)'
#     language: python
#     name: python3
# ---

# # Build Classification Models

# ## Preparation

# Assuming you completed Lesson 1, make sure that a cleaned_cuisines.csv file exists in the root /data folder for these four lessons.

# ### Exercise - predict a national cuisine

# Working in this lesson's notebook.ipynb folder, import that file along with the Pandas library:

import pandas as pd
cuisines_df = pd.read_csv("../data/cleaned_cuisines.csv")
cuisines_df.head()

# Now, import several more libraries:

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import accuracy_score,precision_score,confusion_matrix,classification_report, precision_recall_curve
from sklearn.svm import SVC
import numpy as np

# Divide the X and y coordinates into two dataframes for training. cuisine can be the labels dataframe:

cuisines_label_df = cuisines_df['cuisine']
cuisines_label_df.head()

# Drop that Unnamed: 0 column and the cuisine column, calling drop(). Save the rest of the data as trainable features:

cuisines_feature_df = cuisines_df.drop(['Unnamed: 0', 'cuisine'], axis=1)
cuisines_feature_df.head()

# Now you are ready to train your model!

# ### Choosing your classifier
# Now that your data is clean and ready for training, you have to decide which algorithm to use for the job.
#
# Scikit-learn groups classification under Supervised Learning, and in that category you will find many ways to classify. The variety is quite bewildering at first sight. The following methods all include classification techniques:
#
# - Linear Models
# - Support Vector Machines
# - Stochastic Gradient Descent
# - Nearest Neighbors
# - Gaussian Processes
# - Decision Trees
# - Ensemble methods (voting Classifier)
# - Multiclass and multioutput algorithms (multiclass and multilabel classification, multiclass-multioutput classification)

# **What classifier to go with?**
#
# So, which classifier should you choose? Often, running through several and looking for a good result is a way to test. Scikit-learn offers a side-by-side comparison on a created dataset, comparing KNeighbors, SVC two ways, GaussianProcessClassifier, DecisionTreeClassifier, RandomForestClassifier, MLPClassifier, AdaBoostClassifier, GaussianNB and QuadraticDiscrinationAnalysis.
#
#

# ## Exercise - split the data

# We can focus on logistic regression for our first training trial since you recently learned about the latter in a previous lesson. Split your data into training and testing groups by calling train_test_split():

X_train, X_test, y_train, y_test = train_test_split(cuisines_feature_df, cuisines_label_df, test_size=0.3)

# ## Exercise - apply logistic regression
#

# Since you are using the multiclass case, you need to choose what scheme to use and what solver to set. Use LogisticRegression with a multiclass setting and the liblinear solver to train.
#
# - Create a logistic regression with multi_class set to ovr and the solver set to liblinear:

# +
lr = LogisticRegression(multi_class='ovr',solver='liblinear')
model = lr.fit(X_train, np.ravel(y_train))

accuracy = model.score(X_test, y_test)
print ("Accuracy is {}".format(accuracy))

# -

# The accuracy is good at over 80%!
#
# - You can see this model in action by testing one row of data (#50):

print(f'ingredients: {X_test.iloc[50][X_test.iloc[50]!=0].keys()}')
print(f'cuisine: {y_test.iloc[50]}')


# Digging deeper, you can check for the accuracy of this prediction:

# +
test= X_test.iloc[50].values.reshape(-1, 1).T
proba = model.predict_proba(test)
classes = model.classes_
resultdf = pd.DataFrame(data=proba, columns=classes)

topPrediction = resultdf.T.sort_values(by=[0], ascending = [False])
topPrediction.head()

# -

# Get more detail by printing a classification report, as you did in the regression lessons:

y_pred = model.predict(X_test)
print(classification_report(y_test,y_pred))



StarcoderdataJupyterScriptsDedupFiltered