<jupyter_start><jupyter_text>ZOO ANIMAL CLASSIFICATION   ObjectiveTo develop a model to predict the class of 101 animals given 18 features.Dataset : The dataset used for this work consists of information on 101 animals from a Zoo. Each animal belongs to one of the following 7 classes : mammal, bird, reptile, fish, amphibian, bug, and invertebrate.The predictors are described below:1. animal_name2. hair3. feathers4. eggs5. milk6. airborne7. aquatic8. predator9. toothed10. backbone11. breathes12. Venomous13. fins14. legs15. tail16. domestic17. catsizeAcknowledgementsSource:UCI Machine Learning: https://archive.ics.uci.edu/ml/datasets/ZooCreator:<NAME>: <NAME> <NAME>Mapperley ParkNottingham NG3 5DX0602-621676 1. Importing Libraries<jupyter_code>import pandas as pd #Loading pandas
import seaborn as sns
import sklearn 
import matplotlib.pyplot as plt
import numpy as np
#Loading the data 
data=pd.read_csv("zoo.csv")
data.head()
data.shape # check the number of rows and columns in dataset
data['class_type'].unique() # check the unique class of the response variable
#  Store the namess of the features and class of the animals
feature_cols=['hair','feathers', 'eggs', 'milk', 'airborne','aquatic','predator','toothed', 'backbone', 'breathes', 'venomous', 'fins','legs','tail','domestic','catsize']

class_type1=['Mammal','Bird','Reptile','Fish','Amphipian','Bug','Invertebrate']

data.info()  #view information on the variables
# Save the data without index
data.to_csv('data_noindex.csv',index=False)
data_withoutindex=pd.read_csv("data_noindex.csv")<jupyter_output><empty_output><jupyter_text>2. Data Visualization<jupyter_code>#Barchat showing the number of animals per category
sns.catplot(x="class_type", kind="count",data=data_withoutindex)
# Correlation Heatmap
matrix = np.triu(data.corr())
sns.heatmap(data.corr(),mask=matrix, cmap = 'coolwarm')<jupyter_output><empty_output><jupyter_text>3.1 Preparing Data for Modeling & Analysis<jupyter_code>from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict 
from sklearn.metrics import accuracy_score,recall_score,balanced_accuracy_score,mean_squared_error,confusion_matrix,roc_auc_score
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import graphviz
from sklearn import metrics 
from sklearn import linear_model
from sklearn.svm import SVC

# Extracting the features
X=data.iloc[:,1:17]
X.head()
# Exract the response variable
Y=data.iloc[:,17]
Y.head()
# Reshuffle data for analysis
from sklearn.utils import shuffle
data_new=shuffle(data_withoutindex,random_state=28)
data_new['class_type']
# Divinding data into 70% trainset and 30% testset
X_train, X_test, y_train, y_test=train_test_split(X, Y, test_size=0.30, random_state=13, stratify=Y)

X_train.head() # Preview the trainset for the features
y_train.head() # Preview the trainset for the response
X_test.head()  # Preview the trainset for the features
y_train.head()  # Preview the testset for the response<jupyter_output><empty_output><jupyter_text>3.2  Data for Modeling & Analysis  3.21 KNN<jupyter_code># Fitting knn by first finding the best k
k_values=range(1, 51)
accuracy = []
for k in k_values:
    knn = KNeighborsClassifier(n_neighbors=k)   
    knn.fit(X_train, y_train)
    pred=knn.predict(X_test)
    accuracy_sco=accuracy_score(y_test, pred)
    accuracy.append(accuracy_sco)
accuracy


# Plot showing accuracy for some k
plt.plot(k_values,accuracy)
plt.xlabel('Values for k')
plt.ylabel('Accuracy')
# Fitting knn using best k=1
knn = KNeighborsClassifier(n_neighbors=1)   
knn.fit(X_train, y_train)
y_pred_knn=knn.predict(X_test)
# Accuracy for knn
ACC_knn=accuracy_score(y_test,y_pred_knn)
ACC_knn
#Balanced Accuracy for knn
B_ACC_knn=balanced_accuracy_score(y_test,y_pred_knn)
B_ACC_knn
#Sensitivity
Sensitivity_knn=recall_score(y_pred_knn, y_test, average='weighted')
Sensitivity_knn
#Specificity
specificity=2*B_ACC_knn-Sensitivity_knn
specificity<jupyter_output><empty_output><jupyter_text>3.22  Decision Tree Classifier<jupyter_code># Selecting the best depth/nodes
# List values to try for the best depth
from sklearn.tree import DecisionTreeClassifier
max_depth=[]
max_values=range(1, 11)
for i in max_values:
    clf=DecisionTreeClassifier(max_depth=i, random_state=16)
    clf.fit(X_train, y_train)
    score= clf.score(X_test, y_test) # Returns the accuracy of the model
    max_depth.append(score)
max_depth
plt.plot(max_values,max_depth) #Clearly the best node is 6 and above 
plt.xlabel('max_depth')
plt.ylabel('accuracy_score')
print("the best max_depth is: 6")

# Training the decision tree using the best max_depth
DTC=tree.DecisionTreeClassifier(random_state=3, max_depth=6)
DTC=DTC.fit(X_train, y_train)
# Make Predition on the test data
y_predict_DTC=DTC.predict(X_test)
# Accuracy for Decision Tree
metrics.accuracy_score(y_test,y_predict_DTC)
# Sensitivity for Decision Tree
Sensitivity_DTC=recall_score(y_predict_DTC, y_test, average='weighted')
Sensitivity_DTC
# Balanced Accuracy for Decision Tree
B_ACC_DTC=balanced_accuracy_score(y_test,y_predict_DTC)
B_ACC_DTC
# Specificity FOR Decision Tree
specificity_DTC=2*B_ACC_DTC-Sensitivity_DTC
specificity<jupyter_output><empty_output><jupyter_text>3.23 Support Vector Classifier<jupyter_code># Train the model
svc=SVC(random_state=28)
model_svc=svc.fit(X_train, y_train)
# predict on the test data
y_predict_svc=model_svc.predict(X_test)
# Accuracy of the model
metrics.accuracy_score(y_predict_svc, y_test)
# Balanaced Accuracy
B_ACC_svc=balanced_accuracy_score(y_test,y_predict_svc)
B_ACC_svc
# Sensitivity for Logistic
Sensitivity_svc=recall_score(y_predict_svc, y_test, average='weighted',labels=np.unique(y_predict_svc) )
Sensitivity_svc
# Specificity
specificity=2*B_ACC_svc-Sensitivity_svc
specificity<jupyter_output><empty_output><jupyter_text>3.24 Multinomial Logistic Model<jupyter_code># Train the logisitc model
LR_Model=linear_model.LogisticRegression(random_state=34)
LR_Model.fit(X_train, y_train)
# Make Prediction on the test set
LR_y_predict=LR_Model.predict(X_test)
# Sensitivity for Logistic
Sensitivity_LR=recall_score(LR_y_predict, y_test, average='weighted', labels=np.unique(LR_y_predict))
Sensitivity_LR
# Accuracy score
LR_accuracy=accuracy_score(LR_y_predict, y_test)
LR_accuracy
# Balanced Accuracy for Logisticc
B_ACC_LR=balanced_accuracy_score(y_test,LR_y_predict)
B_ACC_LR
# Specificity
specificity=2*B_ACC_LR-Sensitivity_LR
specificity<jupyter_output><empty_output><jupyter_text>Summary of the models and some relevant metrics|No.|Model|Accuracy|Balanced Accuracy|Recall| Specificity||---|-----|--------|-----------------|-----------|----||1. | KNN |0.9355  |0.88095        |0.9355      |0.8264 ||2. |Decision Tree| 0.9677|0.9524 |0.9677 |0.8264 ||3. |Support Vector Classifier|0.8387 |0.7509  |0.8387  |0.6631  ||4. |Multinomial Logistic Model|0.8710| 0.7619|0.8710 |0.6528|<jupyter_code>Full_DTC=tree.DecisionTreeClassifier(max_depth=6)
Full_DTC=DTC.fit(X, Y)
#graphical representation
import graphviz
dot_data=tree.export_graphviz(Full_DTC, 
                     out_file=None, 
                     feature_names=feature_cols,
                     class_names=class_type1,
                     filled=True,
                     rounded=True
)
graph1=graphviz.Source(dot_data)
graph1
# Confusion Matrix
conf_matrix=confusion_matrix(y_test, y_predict_DTC)
df=pd.DataFrame(conf_matrix,index=['Mammal','Bird','Reptile','Fish','Amphipian','Bug','Invertebrate'])
df.columns=['Mammal','Bird','Reptile','Fish','Amphipian','Bug','Invertebrate']
#df.rows=class_type1=['Mammal','Bird','Reptile','Fish','Amphipian','Bug','Invertebrate']
print('Confusion Matrix \n..........................................................................')
print(df)<jupyter_output>Confusion Matrix 
..........................................................................
              Mammal  Bird  Reptile  Fish  Amphipian  Bug  Invertebrate
Mammal            13     0        0     0          0    0             0
Bird               0     6        0     0          0    0             0
Reptile            0     0        2     0          0    0             0
Fish               0     0        0     4          0    0             0
Amphipian          0     0        0     0          1    0             0
Bug                0     0        0     0          0    2             0
Invertebrate       0     0        0     0          0    1             2
{'fraction_non_alphanumeric': 0.11500815660685156, 'fraction_numerical': 0.028315078070379866, 'mean_word_length': 4.37781954887218, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '518965', 'license': 'MIT', 'n_tokens_mistral': 3159, 'n_tokens_neox': 2932, 'n_words': 659, 'path': 'Animal_Classification.Final.ipynb', 'repo_name': 'CZIKPI/ZOO-ANIMAL-CLASSIFICATION', 'size': '8619'}