"""
# Mushroom Classification using Random Forest Classifier
___

### Task Details
Perform Mushroom Classification using Random Forest Classification.

### Evaluation
The notebook which gives highest accuracy.
"""
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
dataset = pd.read_csv("../input/mushroom-classification/mushrooms.csv")
dataset.head()
dataset.shape
sns.heatmap(dataset.isnull(),yticklabels=False,cbar=False,cmap='viridis')
columns = dataset.columns
columns
for i in columns:
    print(i,": ",dataset[i].unique())
"""
___
# Ordinal Encoder

By using Ordinal Encoder, we can change the data type of an object to numerical.
Because of all the dataset is an object, both X and y is transformed by Ordinal Encoder.
"""
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import OrdinalEncoder

X = dataset.drop(['class'],axis=1)
y = dataset['class']

categorical_cols_X = [cname for cname in X.columns if 
                    X[cname].dtype == "object"]
good_label_cols = ['class']

label_y_train = dataset.drop(categorical_cols_X, axis=1)
ordinal_encoder = OrdinalEncoder()
label_y_train[good_label_cols] = ordinal_encoder.fit_transform(dataset[good_label_cols])

X_train, X_test, y_train, y_test = train_test_split(X,label_y_train,train_size=0.8, test_size=0.2,random_state=0)

label_X_train = X_train
label_X_test = X_test
label_X_train[categorical_cols_X] = ordinal_encoder.fit_transform(X_train[categorical_cols_X])
label_X_test[categorical_cols_X] = ordinal_encoder.transform(X_test[categorical_cols_X])
X_test
y_train.tail()
model = RandomForestClassifier()
model.fit(label_X_train, y_train)
preds = model.predict(label_X_test)
print(mean_absolute_error(y_test, preds))
y_test
# preds = model.predict(label_X_test)
preds
result = pd.DataFrame({'class': preds })
result.tail()
X_test.reset_index(drop=True, inplace=True)
result.reset_index(drop=True, inplace=True)

submission = pd.concat([result,X_test],axis=1)
submission.head()
X_test.reset_index(drop=True, inplace=True)
y_test.reset_index(drop=True, inplace=True)

testing = pd.concat([y_test,X_test],axis=1)
testing.head()
dataset.head()
from sklearn.metrics import accuracy_score
accuracy=accuracy_score(y_test,preds)
print("Random Forest Classifier Accuracy Value: {:.2f}".format(accuracy))