<jupyter_start><jupyter_text><jupyter_code>from google.colab import files
!pip install -q kaggle           # installing Kaggle to upload the dataset into Colab
uploaded = files.upload()         # Uploading the API token from locally 
                                    # that i created from my Kaggle's acccount
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/

!chmod 600 ~/.kaggle/kaggle.json
!kaggle datasets download -d uciml/mushroom-classification       # Once i have uploaded the json
                                                             # it will create a file with the name of dataset in colab
from zipfile import ZipFile
file_name = "mushroom-classification.zip"

with ZipFile(file_name,'r') as zip:
   zip.extractall()
   print('Done')
               # uploading the mushroom classification dataset zip file
                              # which will convert into csv file for use as pandas dataframe                  
  

!pip install category_encoders==2.*        #installing category encoders to encode categorical variables
import pandas as pd
from sklearn.linear_model import LogisticRegressionCV
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing
import numpy as np
from sklearn.model_selection import train_test_split                  # importing all the libraries that are needed
from sklearn.metrics import accuracy_score
%matplotlib inline
import matplotlib.pyplot as plt
import category_encoders as ce

df = pd.read_csv("mushrooms.csv")
print(df.shape)
df.head()
df['class'].value_counts(normalize=True)       # begin with baseline of a target variable
df.isnull().sum()             # checking all the missing values
df.describe().T.sort_values(by='unique')
df.dtypes
y = df["class"]        # setting "class" variable as target
x = df.drop('class',axis=1)       # and all the other columns as features<jupyter_output><empty_output><jupyter_text>> before applying our model on dataset we need to encode all the categorical variables into numeric data> To do so i have decided to apply label encoding on the target variable andone hot encoding on all the other columns<jupyter_code>le = preprocessing.LabelEncoder()
y = le.fit_transform(y)
x = pd.get_dummies(x)         # for one hot encoding i have used dummy function from pandas
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.80, stratify=y, random_state=42) # train_test_split by 80/20
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)      # scaling the data
x_test_scaled = scaler.transform(x_test)
model = LogisticRegressionCV(cv=5, n_jobs=-1, random_state=0)

model.fit(x_train_scaled, y_train)               # Applying the logisticRegressionCV model on training data and checking the Accuracy on test set

print('Validation Accuracy', model.score(x_test_scaled, y_test))<jupyter_output>Validation Accuracy 0.9987692307692307<jupyter_text>*   Getting the accuracy rate 100% does not look right. It seems an overfitted model, reason for this is either there is some data leakage or it is not being preprocessed well enough.*   So instead of applying another supervised machine learning model , i decided to apply same model with some changes to see if it would make any difference on the performance of the model.<jupyter_code>encoder = ce.OrdinalEncoder()
df_2 = encoder.fit_transform(df)       # Now applying the ordinal encoder of all the dataset
df_2.corr()['class'].sort_values(ascending=False)    # checking the correlation of the target variable with other features<jupyter_output><empty_output><jupyter_text>--- This time used highly correlated features instaed of using all of the variables.<jupyter_code>target = 'class'
features = ['gill-size','gill-spacing','cap-surface','ring-number','gill-attachment','veil-color','stalk-shape']
train, test = train_test_split(df_2, train_size=0.80, random_state=42)
X_train = train[features]
X_test = test[features]
Y_train = train[target]
Y_test = test[target]
print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
model = LogisticRegressionCV(cv=5, n_jobs=-1, random_state=0)

model.fit(X_train_scaled, Y_train)

print('Validation Accuracy', model.score(X_test_scaled, Y_test))       #got the accuracy rate much better than before
import sklearn
sklearn.__version__
from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(model, X_test_scaled, Y_test,
                      values_format='.0f', xticks_rotation='vertical', cmap='Blues');
y_pred = model.predict(X_test_scaled)    # predicted vlues of test data
from sklearn.metrics import classification_report
                                                          # getting the precision and recall score
print(classification_report(y_pred, Y_test))<jupyter_output>precision    recall  f1-score   support

           1       0.90      0.78      0.84       895
           2       0.77      0.89      0.83       730

    accuracy                           0.83      1625
   macro avg       0.83      0.84      0.83      1625
weighted avg       0.84      0.83      0.83      1625
{'fraction_non_alphanumeric': 0.07736280487804878, 'fraction_numerical': 0.020769817073170733, 'mean_word_length': 3.4407783417935702, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '573901', 'license': 'MIT', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1526, 'n_words': 494, 'path': 'Applying_Logistic_RegressionCV_on_mushroom_data_set.ipynb', 'repo_name': 'misqualzarabi/misqualzarabi.github.io', 'size': '5248'}