# 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
"""
# <font color='#6f7578'>Palmer Archipelago (Antarctica) penguin data EDA and decision tree model</font>
"""
"""
## <font color='#34b4eb'>Exploration of data set and creation of decision tree model to make prediction on species given penguin features</font>
"""
df = pd.read_csv("/kaggle/input/palmer-archipelago-antarctica-penguin-data/penguins_size.csv")
df.head()
df["species"].unique()
df.isnull().sum()
df.info()
df = df.dropna()
df.info()
df["island"].unique()
df["sex"].unique()
df[df["sex"] == "."]
df[df["species"] == "Gentoo"].groupby("sex").describe().transpose()
"""
## <font color='#34b4eb'>Given the mean feature values "." leans towards being a female</font>
"""
df.at[336,"sex"] = "FEMALE"  
df.loc[336]
sns.pairplot(df,hue="species",palette="Paired")
sns.catplot(x="species", y="culmen_length_mm", data=df, kind="box", col="sex", palette="Paired")
df.head()
"""
## <font color='#34b4eb'>Create dummy variables and drop the label</font>
"""
X = pd.get_dummies(df.drop("species", axis=1), drop_first=True)
X
y = df["species"]
y
"""
## <font color='#34b4eb'>NB - no scaling of data required for decision tree algorithm</font>
"""
from sklearn.model_selection import train_test_split
# train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
from sklearn.tree import DecisionTreeClassifier
"""
## <font color='#34b4eb'>Will just create the model with default hyperparameters</font>
"""
model = DecisionTreeClassifier()
model.fit(X_train,y_train)
base_preds = model.predict(X_test)
base_preds
from sklearn.metrics import classification_report, plot_confusion_matrix
print(classification_report(y_test,base_preds))
plot_confusion_matrix(model,X_test,y_test,cmap="Accent")
"""
## <font color='#34b4eb'>Only 3 penguins were misclassified</font>
"""
model.feature_importances_
X.columns
pd.DataFrame(index=X.columns, data=model.feature_importances_)
pd.DataFrame(index=X.columns, data=model.feature_importances_,columns=["feature importance"]).sort_values("feature importance")
from sklearn.tree import plot_tree
plt.figure(figsize=(11,11),dpi=150)
plot_tree(model,feature_names=X.columns, filled=True);