<jupyter_start><jupyter_text>EDA<jupyter_code>df.head()
df.shape
df.columns.values 
df.info()
df.describe(include='all')
#Histogram to check distribution and skewness
l= ['Zero_Crossings', 'Duration', 'Amp_range', 'Avg_amp', 'Freq_range',
    'Pulses_per_Sec', 'Partials', 'MFCC', 'Spectral Rolloff']

plt.figure(figsize=(15,20))
for i in range(len(l)):
    plt.subplot(5,2,i+1)
    sns.histplot(df[l[i]],kde=True) 
plt.show()
#Boxplot to check for outliers
l= ['Zero_Crossings', 'Duration', 'Amp_range', 'Avg_amp', 'Freq_range',
    'Pulses_per_Sec', 'Partials', 'MFCC', 'Spectral Rolloff']

plt.figure(figsize=(20,25))

for i in range(0,len(l)):
    plt.subplot(5,2,i+1)
    sns.set_style('whitegrid')
    sns.boxplot(df[l[i]],color='green',orient='h')
    plt.tight_layout()
plt.show()    
#Quality correlation matrix
k = 9 #number of variables for heatmap
cols = df.corr().nlargest(k, 'Amp_range')['Amp_range'].index
cm = df[cols].corr()
plt.figure(figsize=(10,6))
sns.heatmap(cm, annot=True, cmap = 'coolwarm')
df['Call'].value_counts()
df['Species'].value_counts()<jupyter_output><empty_output><jupyter_text>Data Cleansing<jupyter_code>df['Call'].unique()
clean = {'unknown':np.NaN, 'growl?': 'growl','Growl':'growl', 'growl ':'growl', 'hiss?':'hiss', 'Hiss':'hiss', 
         'Sharp Hiss':'hiss', 'purr sequence': 'purr', 'Loud rumble/roar':'roar', 'call?':'call', 'main call':'call', 
         'call sequence':'call', 'roar or call':'roar', 'roar?':'roar', 'purr sequence':'purr', ' roar':'roar', 'hiss ':'hiss', 
         'mew?':'mew', 'Call sequence(possible mew)':'call', 'call sequence?':'call', 'single call?':'call', 
         'grow/hiss':'growl/hiss'}
df.replace(clean, inplace = True)
df['Call'].unique()
df['Age'].unique()
clean2 = {'A':'Adult','Adult ':'Adult', 'Juvenile ':'Juvenile', 'juvenile':'Juvenile'}

df.replace(clean2, inplace = True)
df['Age'].fillna('Unknown', inplace = True)
df['Age'].unique()
df['Sex'].unique()
clean3 = {'Female ':'Female','F':'Female', 'M':'Male','male ':'Male', 'P':'Pair', 'Pair (Unknown)':'Pair', 'G':'Group', 'G (1 M and 2F)':'Group'}

df.replace(clean3, inplace = True)
df['Sex'].fillna('Unknown', inplace = True)
df['Sex'].unique()
df.describe(include='object')<jupyter_output><empty_output><jupyter_text>Standardize Continuous Features<jupyter_code>continuous = ['Zero_Crossings', 'Duration', 'Amp_range', 'Avg_amp', 'Freq_range',
       'Pulses_per_Sec', 'Partials', 'MFCC', 'Spectral Rolloff']

scaler = StandardScaler()

for var in continuous:
    df[var] = df[var].astype('float64')
    df[var] = scaler.fit_transform(df[var].values.reshape(-1, 1))
df.describe(include='float64')
#Save new clean data to new CSV
df.to_csv('features_cleaned.csv', index=False)<jupyter_output><empty_output><jupyter_text>Convert categorical variables into dummy/indicator variables<jupyter_code>categorical = ['Sex', 'Age', 'Species']

for var in categorical:
    df = pd.concat([df, pd.get_dummies(df[var], prefix=var)], axis=1)
    del df[var]
    
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 345 entries, 0 to 344
Data columns (total 24 columns):
 #   Column                  Non-Null Count  Dtype  
---  ------                  --------------  -----  
 0   Zero_Crossings          345 non-null    float64
 1   Duration                345 non-null    float64
 2   Amp_range               345 non-null    float64
 3   Avg_amp                 345 non-null    float64
 4   Freq_range              345 non-null    float64
 5   Pulses_per_Sec          345 non-null    float64
 6   Partials                345 non-null    float64
 7   MFCC                    345 non-null    float64
 8   Spectral Rolloff        345 non-null    float64
 9   Call                    331 non-null    object 
 10  Sex_Female              345 non-null    uint8  
 11  Sex_Group               345 non-null    uint8  
 12  Sex_Male                345 non-null    uint8  
 13  Sex_Pair                345 non-null    uint8  
 14  Sex_Unknown             345 non-null    ui[...]<jupyter_text>Vocalization Classification  Splitting data<jupyter_code>X = df[pd.notnull(df['Call'])].drop(['Call'], axis=1)
y = df[pd.notnull(df['Call'])]['Call']
print(X.shape)
print(y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30)
print(X_train.shape)
print(X_test.shape)
print('Call values for Data')
print(df['Call'].value_counts())
print('\n')
print('Call values for Training')
print(y_train.value_counts())
print('\n')
print('Call values for Testing')
print(y_test.value_counts())
print('Calls trained for but not tested for')
print(set(np.unique(y_train))-set(np.unique(y_test)))
print('Calls test for but not trained for')
print(set(np.unique(y_test))-set(np.unique(y_train)))<jupyter_output>Calls trained for but not tested for
{'growl/hiss'}
Calls test for but not trained for
set()<jupyter_text>XGBoost<jupyter_code>parameters = dict(
    objective='multi:softprob',
    random_state = 30,
    max_depth=9,
    learning_rate=0.01,
    subsample=0.8,
    colsample_bytree=0.4,
    tree_method='gpu_hist')
#eval_metric='mlogloss'

clf = XGBClassifier(**parameters, n_estimators=1200)
clf.fit(X_train, y_train)
clf.score(X_train, y_train)
clf.score(X_test,y_test)
y_pred = clf.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred)))
names = sorted(list(set(np.unique(y_test)).union(set(y_pred))))
cnf = confusion_matrix(y_test, y_pred)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names, yticklabels=names,cmap= "YlOrBr")
plt.title('XGBoost')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',clf.score(X_test, y_test))
plot_importance(clf)
figsize=(5,10)<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>clf_svc = SVC()
clf_svc.fit(X_train, y_train)
clf_svc.score(X_train, y_train)
clf_svc.score(X_test, y_test)
y_pred_svc=clf_svc.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred_svc))
print('3. False Positive')
print(set(np.unique(y_pred_svc))-set(np.unique(y_test)))
print('4. False Negative')
print(set(np.unique(y_test))-set(np.unique(y_pred_svc)))
names_svc = sorted(list(set(np.unique(y_test)).union(set(y_pred_svc))))
cnf = confusion_matrix(y_test, y_pred_svc)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_svc, yticklabels=names_svc,cmap= "YlOrBr")
plt.title('SVM Confusion Matrix')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',clf_svc.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>gnb = GaussianNB()
gnb.fit(X_train, y_train)
gnb.score(X_train, y_train)
gnb.score(X_test, y_test)
y_pred_nb = gnb.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred_nb))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_nb))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_nb)))
names_nb = sorted(list(set(np.unique(y_test)).union(set(y_pred_nb))))
cnf = confusion_matrix(y_test, y_pred_nb)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_nb, yticklabels=names_nb,cmap= "YlOrBr")
plt.title('Naive Bayes Confusion Matrix')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',gnb.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>lr = LogisticRegression(solver='liblinear', multi_class='ovr')
lr.fit(X_train,y_train)
lr.score(X_train, y_train)
lr.score(X_test, y_test)
y_pred_lr = lr.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred_lr))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_lr))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_lr)))
names_lr = sorted(list(set(np.unique(y_test)).union(set(y_pred_lr))))
cnf = confusion_matrix(y_test, y_pred_lr)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_lr, yticklabels=names_lr,cmap= "YlOrBr")
plt.title('Logistic Regression')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',lr.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
knn.score(X_train, y_train)
knn.score(X_test, y_test)
y_pred_knn = knn.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred_knn))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_knn))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_knn)))
names_knn = sorted(list(set(np.unique(y_test)).union(set(y_pred_knn))))
cnf = confusion_matrix(y_test, y_pred_knn)
fig, ax = plt.subplots(figsize=(8,4))

#Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_knn, yticklabels=names_knn,cmap= "YlOrBr")
plt.title('KNN')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',knn.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier<jupyter_code>cart = DecisionTreeClassifier()
cart.fit(X_train, y_train)
cart.score(X_train, y_train)
cart.score(X_test, y_test)
y_pred_cart = cart.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred_cart))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_cart))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_cart)))
names_cart = sorted(list(set(np.unique(y_test)).union(set(y_pred_cart))))
cnf = confusion_matrix(y_test, y_pred_cart)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_cart, yticklabels=names_cart,cmap= "YlOrBr")
plt.title('Decision Tree Classifier')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',cart.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Random Forest Classifier<jupyter_code>rf = RandomForestClassifier()
rf.fit(X_train, y_train)
rf.score(X_train, y_train)
rf.score(X_test, y_test)
y_pred_rf = cart.predict(X_test)
print('1. Tested Calls')
print(np.unique(y_test))
print('2. Predicted Calls')
print(np.unique(y_pred_rf))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_rf))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_rf)))
names_rf = sorted(list(set(np.unique(y_test)).union(set(y_pred_rf))))
cnf = confusion_matrix(y_test, y_pred_rf)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_rf, yticklabels=names_rf,cmap= "YlOrBr")
plt.title('Random Forest')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',rf.score(X_test, y_test))
#The data is unbalanced, this could be fixed by updating the class weights 
#Or getting more varied data
df['Call'].value_counts()<jupyter_output><empty_output><jupyter_text>Species Classification<jupyter_code>df2= pd.read_csv('features_cleaned.csv')
df2.head()
df2.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 345 entries, 0 to 344
Data columns (total 13 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   Zero_Crossings    345 non-null    float64
 1   Duration          345 non-null    float64
 2   Amp_range         345 non-null    float64
 3   Avg_amp           345 non-null    float64
 4   Freq_range        345 non-null    float64
 5   Pulses_per_Sec    345 non-null    float64
 6   Partials          345 non-null    float64
 7   MFCC              345 non-null    float64
 8   Spectral Rolloff  345 non-null    float64
 9   Sex               345 non-null    object 
 10  Age               345 non-null    object 
 11  Species           345 non-null    object 
 12  Call              331 non-null    object 
dtypes: float64(9), object(4)
memory usage: 35.2+ KB<jupyter_text>Convert categorical variables into dummy/indicator variables<jupyter_code>categorical = ['Sex', 'Age', 'Call']

for var in categorical:
    df2 = pd.concat([df2, pd.get_dummies(df2[var], prefix=var)], axis=1)
    del df2[var]
    
df2.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 345 entries, 0 to 344
Data columns (total 25 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   Zero_Crossings    345 non-null    float64
 1   Duration          345 non-null    float64
 2   Amp_range         345 non-null    float64
 3   Avg_amp           345 non-null    float64
 4   Freq_range        345 non-null    float64
 5   Pulses_per_Sec    345 non-null    float64
 6   Partials          345 non-null    float64
 7   MFCC              345 non-null    float64
 8   Spectral Rolloff  345 non-null    float64
 9   Species           345 non-null    object 
 10  Sex_Female        345 non-null    uint8  
 11  Sex_Group         345 non-null    uint8  
 12  Sex_Male          345 non-null    uint8  
 13  Sex_Pair          345 non-null    uint8  
 14  Sex_Unknown       345 non-null    uint8  
 15  Age_Adult         345 non-null    uint8  
 16  Age_Juvenile      345 non-null    uint8  
 1[...]<jupyter_text>Splitting data<jupyter_code>X = df2[pd.notnull(df2['Species'])].drop(['Species'], axis=1)
y = df2[pd.notnull(df2['Species'])]['Species']
print(X.shape)
print(y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30)
print(X_train.shape)
print(X_test.shape)
print('Species values for Data')
print(df2['Species'].value_counts())
print('\n')
print('Species values for Training')
print(y_train.value_counts())
print('\n')
print('Species values for Testing')
print(y_test.value_counts())
print('Species trained for but not tested for')
print(set(np.unique(y_train))-set(np.unique(y_test)))
print('Species test for but not trained for')
print(set(np.unique(y_test))-set(np.unique(y_train)))<jupyter_output>Species trained for but not tested for
set()
Species test for but not trained for
set()<jupyter_text>XGBoost<jupyter_code>parameters = dict(
    objective='multi:softprob',
    random_state = 30,
    max_depth=9,
    learning_rate=0.01,
    subsample=0.8,
    colsample_bytree=0.4,
    tree_method='gpu_hist')
#eval_metric='mlogloss'

clf = XGBClassifier(**parameters, n_estimators=1200)
clf.fit(X_train, y_train)
clf.score(X_train, y_train)
clf.score(X_test,y_test)
y_pred = clf.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred)))
names = sorted(list(set(np.unique(y_test)).union(set(y_pred))))
cnf = confusion_matrix(y_test, y_pred)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names, yticklabels=names,cmap= "YlOrBr")
plt.title('XGBoost')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',clf.score(X_test, y_test))
plot_importance(clf)
figsize=(8,4)<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>clf_svc = SVC()
clf_svc.fit(X_train, y_train)
clf_svc.score(X_train, y_train)
clf_svc.score(X_test, y_test)
y_pred_svc=clf_svc.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred_svc))
print('3. False Positive')
print(set(np.unique(y_pred_svc))-set(np.unique(y_test)))
print('4. False Negative')
print(set(np.unique(y_test))-set(np.unique(y_pred_svc)))
names_svc = sorted(list(set(np.unique(y_test)).union(set(y_pred_svc))))
cnf = confusion_matrix(y_test, y_pred_svc)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_svc, yticklabels=names_svc,cmap= "YlOrBr")
plt.title('SVM Confusion Matrix')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',clf_svc.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>gnb = GaussianNB()
gnb.fit(X_train, y_train)
gnb.score(X_train, y_train)
gnb.score(X_test, y_test)
y_pred_nb = gnb.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred_nb))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_nb))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_nb)))
names_nb = sorted(list(set(np.unique(y_test)).union(set(y_pred_nb))))
cnf = confusion_matrix(y_test, y_pred_nb)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_nb, yticklabels=names_nb,cmap= "YlOrBr")
plt.title('Naive Bayes Confusion Matrix')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',gnb.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>lr = LogisticRegression(solver='liblinear', multi_class='ovr')
lr.fit(X_train,y_train)
lr.score(X_train, y_train)
lr.score(X_test, y_test)
y_pred_lr = lr.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred_lr))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_lr))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_lr)))
names_lr = sorted(list(set(np.unique(y_test)).union(set(y_pred_lr))))
cnf = confusion_matrix(y_test, y_pred_lr)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_lr, yticklabels=names_lr,cmap= "YlOrBr")
plt.title('Logistic Regression')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',lr.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>KNN<jupyter_code>knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
knn.score(X_train, y_train)
knn.score(X_test, y_test)
y_pred_knn = knn.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred_knn))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_knn))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_knn)))
names_knn = sorted(list(set(np.unique(y_test)).union(set(y_pred_knn))))
cnf = confusion_matrix(y_test, y_pred_knn)
fig, ax = plt.subplots(figsize=(8,4))

#Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_knn, yticklabels=names_knn,cmap= "YlOrBr")
plt.title('KNN')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',knn.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier<jupyter_code>cart = DecisionTreeClassifier()
cart.fit(X_train, y_train)
cart.score(X_train, y_train)
cart.score(X_test, y_test)
y_pred_cart = cart.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred_cart))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_cart))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_cart)))
names_cart = sorted(list(set(np.unique(y_test)).union(set(y_pred_cart))))
cnf = confusion_matrix(y_test, y_pred_cart)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_cart, yticklabels=names_cart,cmap= "YlOrBr")
plt.title('Decision Tree Classifier')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',cart.score(X_test, y_test))<jupyter_output><empty_output><jupyter_text>Random Forest Classifier<jupyter_code>rf = RandomForestClassifier()
rf.fit(X_train, y_train)
rf.score(X_train, y_train)
rf.score(X_test, y_test)
y_pred_rf = cart.predict(X_test)
print('1. Tested Species')
print(np.unique(y_test))
print('2. Predicted Species')
print(np.unique(y_pred_rf))
print('3. Not tested for but predicted')
print(set(np.unique(y_pred_rf))-set(np.unique(y_test)))
print('4. Tested for but not predicted')
print(set(np.unique(y_test))-set(np.unique(y_pred_rf)))
names_rf = sorted(list(set(np.unique(y_test)).union(set(y_pred_rf))))
cnf = confusion_matrix(y_test, y_pred_rf)
fig, ax = plt.subplots(figsize=(8,4))

# Normalise
cnf = cnf.astype('float')/cnf.sum(axis=1)[:, np.newaxis]

sns.heatmap(cnf, annot=True, fmt='.1%', xticklabels=names_rf, yticklabels=names_rf,cmap= "YlOrBr")
plt.title('Random Forest')
ax.xaxis.set_label_position('top')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show(block=False)
print('Accuracy',rf.score(X_test, y_test))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup