<jupyter_start><jupyter_text>SPLEX-Project 2020-2021 : Frog Calls ------- 🐸 Step 2 : Predictory and classifications analysis of dataset 🐸 *<NAME> and <NAME>*<jupyter_code>import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.metrics import *
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import recall_score<jupyter_output><empty_output><jupyter_text>Key variables<jupyter_code>dataset=pd.read_csv('../Anuran_Calls/Frogs_MFCCs.csv')
features=dataset.columns[1:-4]
labels=dataset.columns[-4:]
family,genus,species,id=labels<jupyter_output><empty_output><jupyter_text>I. Random Forest __Independant__ 🌳 🌳 🌳<jupyter_code>for label in labels[:-1]:
    x_train, x_test, y_train, y_test = train_test_split(dataset[features].values, 
                                                        dataset[label], 
                                                        test_size=0.33, 
                                                        random_state=42)

    Rfclf = RandomForestClassifier(random_state=0)
    Rfclf.fit(x_train, y_train)

    y_predicted=Rfclf.predict(x_test)
    
    

    
    confusion_matrix(y_test,y_predicted)
    disp=plot_confusion_matrix(Rfclf,x_test,y_test,cmap=plt.cm.Blues,
                                     normalize='true',labels=None,xticks_rotation="vertical")
    disp.ax_.set_title("Classification of : "+label)
    
plt.savefig("../images/confusionMatrixGeneral_"+str(label)+".png")<jupyter_output><empty_output><jupyter_text>II. Random Forest __Hierarchical__Our idea came from this inspirationnal drawing I made :                         🌳                        / \                       🌳 🌳                       /\  /\                      🌳🌳🌳🌳                                            > <NAME>.; <NAME>.; <NAME>. [**Recognizing Family, Genus, and Species of Anuran Using a Hierarchical Classification Approach**](https://www.researchgate.net/publication/305335644_Recognizing_Family_Genus_and_Species_of_Anuran_Using_a_Hierarchical_Classification_Approach).Lecture Notes in Computer Science. 995ed.: Springer International Publishing, 2016, v. 9956, p. 198-212.<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(dataset[features].values, 
                                                    dataset[labels], 
                                                    test_size=0.33, 
                                                    random_state=42)


print("Classifying Families",'\n')
RfclfGeneral=RandomForestClassifier()
RfclfGeneral.fit(x_train,y_train[family])


RfclfFamiliesRF=[]
RfclFamiliesLabels=[]

RfclfGenusRF=[]
RfclfGenusLabels=[]

print("Classifying Genus : ",'\n')

for label in np.unique(dataset[family]):
    print("Classifying "+str(label))
    
    RfclfTemp = RandomForestClassifier()
    RfclfTemp.fit(x_train[y_train[family]==label],(y_train[y_train[family]==label][genus]))
    
    RfclFamiliesLabels.append(label)
    RfclfFamiliesRF.append(RfclfTemp)
    
RfclfFamilies=dict(zip(RfclFamiliesLabels,RfclfFamiliesRF))

print("\nClassifying Species : ",'\n')

for label in np.unique(dataset[genus]):
    print("Classifying "+str(label))
    
    RfclfTemp = RandomForestClassifier()
    RfclfTemp.fit(x_train[y_train[genus]==label],(y_train[y_train[genus]==label][species]))
    
    RfclfGenusLabels.append(label)
    RfclfGenusRF.append(RfclfTemp)
    
RfclfGenus=dict(zip(RfclfGenusLabels,RfclfGenusRF))<jupyter_output>Classifying Families 

Classifying Genus :  

Classifying Bufonidae
Classifying Dendrobatidae
Classifying Hylidae
Classifying Leptodactylidae

Classifying Species :  

Classifying Adenomera
Classifying Ameerega
Classifying Dendropsophus
Classifying Hypsiboas
Classifying Leptodactylus
Classifying Osteocephalus
Classifying Rhinella
Classifying Scinax<jupyter_text>Predicting Results 🐸<jupyter_code>y_predicted=pd.DataFrame(columns=y_test.columns[:-1])
display(y_predicted)
y_predicted[family]=RfclfGeneral.predict(x_test)

for label in np.unique(y_predicted[family]):
    
    x_testTemp=x_test[y_predicted[family]==label]
    x_testTempIndices=np.where(y_predicted[family]==label)[0]
    
    
    if type(RfclfFamilies[label])!=str:
        
        y_predictedGenusTemp=RfclfFamilies[label].predict(x_testTemp)
        y_predicted[genus].iloc[x_testTempIndices]=y_predictedGenusTemp
        
    else:

        y_predictedGenusTemp=[RfclfFamilies[label] for i in x_testTempIndices]
        y_predicted[genus].iloc[x_testTempIndices]=y_predictedGenusTemp
        
for sublabel in np.unique(y_predicted[genus]):

    x_testTemp=x_test[y_predicted[genus]==sublabel]
    x_testTempIndices=np.where(y_predicted[genus]==sublabel)[0]

    if type(RfclfGenus[sublabel])!=str:

        y_predictedSpecieTemp=RfclfGenus[sublabel].predict(x_testTemp)
        y_predicted[species].iloc[x_testTempIndices]=y_predictedSpecieTemp

    else:

        y_predictedSpecieTemp=[RfclfGenus[sublabel] for i in x_testTempIndices]
        y_predicted[species].iloc[x_testTempIndices]=y_predictedSpecieTemp
        

display(y_predicted)
for label in labels[:-1]:
    
    cm=confusion_matrix(y_test[label],y_predicted[label],normalize='true')
    disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=np.unique(y_test[label]))
    disp = disp.plot(cmap=plt.cm.Blues,xticks_rotation="vertical")
cm=confusion_matrix(y_test[species],y_predicted[species])
somme=0
for i in range(len(cm)):
    for j in range(len(cm)):
        if i==j:
            somme+=cm[i][j]
print('Accuracy for Hierarchical clustering= ',somme/cm.sum())
flat_SVC=RandomForestClassifier()
scores=cross_val_score(flat_SVC,dataset[features],dataset[species],cv=20)
print("Accuracy with specie Clustering directly",scores.mean())<jupyter_output>Accuracy with specie Clustering directly 0.9727595945527698<jupyter_text>II. SVM Classifier Independant /৲¬ 📈 📈<jupyter_code>svc_clf=SVC()
svc_clf.fit(x_train,y_train[family])
plot_confusion_matrix(svc_clf,x_test,y_test[family],cmap=plt.cm.Blues,normalize='true',xticks_rotation="vertical")
svc_clf.fit(x_train,y_train[genus])
plot_confusion_matrix(svc_clf,x_test,y_test[genus],cmap=plt.cm.Blues,normalize='true',xticks_rotation="vertical")
svc_clf.fit(x_train,y_train[species])
plot_confusion_matrix(svc_clf,x_test,y_test[species],cmap=plt.cm.Blues,normalize='true',xticks_rotation="vertical")<jupyter_output><empty_output><jupyter_text>II. SVM Classifier Hierarchical /৲¬Our idea came from this inspirationnal drawing I made :                         📈                        / \                       📈 📈                       /\  /\                      📈📈📈📈   Creating the pipeline to cluster them hierarchically<jupyter_code>print("Classifying Families",'\n')
SVMclfGeneral=SVC()
SVMclfGeneral.fit(x_train,y_train[family])


SVMclfFamiliesSVM=[]
SVMclFamiliesLabels=[]

SVMclfGenusSVM=[]
SVMclfGenusLabels=[]

print("Classifying Genus : ",'\n')

for label in np.unique(dataset[family]):
    print("Classifying "+str(label))
    
    if len(np.unique(y_train[y_train[family]==label][genus]))>1:
        SVMclfTemp = SVC()
        SVMclfTemp.fit(x_train[y_train[family]==label],(y_train[y_train[family]==label][genus]))

        SVMclFamiliesLabels.append(label)
        SVMclfFamiliesSVM.append(SVMclfTemp)
    else:
        genusTemp=np.unique(y_train[y_train[family]==label][genus])[0]
        print(genusTemp)
        SVMclFamiliesLabels.append(label)
        SVMclfFamiliesSVM.append(genusTemp)
    
SVMclfFamilies=dict(zip(SVMclFamiliesLabels,SVMclfFamiliesSVM))
print(SVMclfFamilies)

print("\nClassifying Species : ",'\n')

for label in np.unique(dataset[genus]):
    print("Classifying "+str(label))

    if len(np.unique(y_train[y_train[genus]==label][species]))>1:

        SVMclfTemp = SVC()
        SVMclfTemp.fit(x_train[y_train[genus]==label],(y_train[y_train[genus]==label][species]))
        SVMclfGenusLabels.append(label)
        SVMclfGenusSVM.append(SVMclfTemp)
    else:
        
        specieTemp=np.unique(y_train[y_train[genus]==label][species])[0]
        
        SVMclfGenusLabels.append(label)
        SVMclfGenusSVM.append(specieTemp)

        
SVMclfGenus=dict(zip(SVMclfGenusLabels,SVMclfGenusSVM))<jupyter_output>Classifying Families 

Classifying Genus :  

Classifying Bufonidae
Rhinella
Classifying Dendrobatidae
Ameerega
Classifying Hylidae
Classifying Leptodactylidae
{'Bufonidae': 'Rhinella', 'Dendrobatidae': 'Ameerega', 'Hylidae': SVC(), 'Leptodactylidae': SVC()}

Classifying Species :  

Classifying Adenomera
Classifying Ameerega
Classifying Dendropsophus
Classifying Hypsiboas
Classifying Leptodactylus
Classifying Osteocephalus
Classifying Rhinella
Classifying Scinax<jupyter_text>Predicting results 🐸<jupyter_code>y_predicted=pd.DataFrame(columns=y_test.columns[:-1])
display(y_predicted)
y_predicted[family]=SVMclfGeneral.predict(x_test)

for label in np.unique(y_predicted[family]):
    
    x_testTemp=x_test[y_predicted[family]==label]
    x_testTempIndices=np.where(y_predicted[family]==label)[0]
    
    
    if type(SVMclfFamilies[label])!=str:
        
        y_predictedGenusTemp=SVMclfFamilies[label].predict(x_testTemp)
        y_predicted[genus].iloc[x_testTempIndices]=y_predictedGenusTemp
        
    else:

        y_predictedGenusTemp=[SVMclfFamilies[label] for i in x_testTempIndices]
        y_predicted[genus].iloc[x_testTempIndices]=y_predictedGenusTemp
        
for sublabel in np.unique(y_predicted[genus]):

    x_testTemp=x_test[y_predicted[genus]==sublabel]
    x_testTempIndices=np.where(y_predicted[genus]==sublabel)[0]

    if type(SVMclfGenus[sublabel])!=str:

        y_predictedSpecieTemp=SVMclfGenus[sublabel].predict(x_testTemp)
        y_predicted[species].iloc[x_testTempIndices]=y_predictedSpecieTemp

    else:

        y_predictedSpecieTemp=[SVMclfGenus[sublabel] for i in x_testTempIndices]
        y_predicted[species].iloc[x_testTempIndices]=y_predictedSpecieTemp
        

display(y_predicted)
for label in labels[:-1]:
    
    cm=confusion_matrix(y_test[label],y_predicted[label],normalize='true')
    disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=np.unique(y_test[label]))
    disp = disp.plot(cmap=plt.cm.Blues,xticks_rotation="vertical")<jupyter_output><empty_output><jupyter_text>Precision : Hierarchical<jupyter_code>cm=confusion_matrix(y_test[species],y_predicted[species])
somme=0
for i in range(len(cm)):
    for j in range(len(cm)):
        if i==j:
            somme+=cm[i][j]
print('Accuracy for Hierarchical clustering= ',somme/cm.sum())<jupyter_output>Accuracy for Hierarchical clustering=  0.9806315789473684<jupyter_text>Precision : Standard<jupyter_code>flat_SVC=SVC()
scores=cross_val_score(flat_SVC,dataset[features],dataset[species],cv=20)
print("Accuracy with specie Clustering directly",scores.mean())<jupyter_output>Accuracy with specie Clustering directly 0.9779031259671929<jupyter_text>III. KNN Classifier Independant /৲¬ 🪢 🪢<jupyter_code>for label in labels[:-1]:
    x_train, x_test, y_train, y_test = train_test_split(dataset[features].values, 
                                                        dataset[label], 
                                                        test_size=0.33, 
                                                        random_state=42)



    Rfclf = KNeighborsClassifier(n_neighbors=7)
    Rfclf.fit(x_train, y_train)

    y_predicted=Rfclf.predict(x_test)
    
    

    
    confusion_matrix(y_test,y_predicted)
    disp=plot_confusion_matrix(Rfclf,x_test,y_test,cmap=plt.cm.Blues,
                                     normalize='true',labels=None,xticks_rotation="vertical")
    disp.ax_.set_title("Classification of : "+label)
    
plt.savefig("../images/confusionMatrixGeneral_"+str(label)+".png")

flat_SVC=KNeighborsClassifier(n_neighbors=7)
scores=cross_val_score(flat_SVC,dataset[features],dataset[species],cv=20)
print("Accuracy with specie Clustering directly",scores.mean())<jupyter_output>Accuracy with specie Clustering directly 0.9738714794181368<jupyter_text>II. KNN Classifier Hierarchical /৲¬Our idea came from this inspirationnal drawing I made :                         🪢                        / \                       🪢 🪢                       /\  /\                      🪢🪢🪢🪢<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(dataset[features].values, 
                                                    dataset[labels], 
                                                    test_size=0.33, 
                                                    random_state=42)


print("Classifying Families",'\n')
RfclfGeneral=KNeighborsClassifier(n_neighbors=5)
RfclfGeneral.fit(x_train,y_train[family])


RfclfFamiliesRF=[]
RfclFamiliesLabels=[]

RfclfGenusRF=[]
RfclfGenusLabels=[]

print("Classifying Genus : ",'\n')

for label in np.unique(dataset[family]):
    print("Classifying "+str(label))
    
    RfclfTemp = KNeighborsClassifier(n_neighbors=5)
    RfclfTemp.fit(x_train[y_train[family]==label],(y_train[y_train[family]==label][genus]))
    
    RfclFamiliesLabels.append(label)
    RfclfFamiliesRF.append(RfclfTemp)
    
RfclfFamilies=dict(zip(RfclFamiliesLabels,RfclfFamiliesRF))

print("\nClassifying Species : ",'\n')

for label in np.unique(dataset[genus]):
    print("Classifying "+str(label))
    
    RfclfTemp = KNeighborsClassifier(n_neighbors=5)
    RfclfTemp.fit(x_train[y_train[genus]==label],(y_train[y_train[genus]==label][species]))
    
    RfclfGenusLabels.append(label)
    RfclfGenusRF.append(RfclfTemp)
    
RfclfGenus=dict(zip(RfclfGenusLabels,RfclfGenusRF))<jupyter_output>Classifying Families 

Classifying Genus :  

Classifying Bufonidae
Classifying Dendrobatidae
Classifying Hylidae
Classifying Leptodactylidae

Classifying Species :  

Classifying Adenomera
Classifying Ameerega
Classifying Dendropsophus
Classifying Hypsiboas
Classifying Leptodactylus
Classifying Osteocephalus
Classifying Rhinella
Classifying Scinax<jupyter_text>Predicting results 🐸<jupyter_code>y_predicted=pd.DataFrame(columns=y_test.columns[:-1])
display(y_predicted)
y_predicted[family]=RfclfGeneral.predict(x_test)

for label in np.unique(y_predicted[family]):
    
    x_testTemp=x_test[y_predicted[family]==label]
    x_testTempIndices=np.where(y_predicted[family]==label)[0]
    
    
    if type(RfclfFamilies[label])!=str:
        
        y_predictedGenusTemp=RfclfFamilies[label].predict(x_testTemp)
        y_predicted[genus].iloc[x_testTempIndices]=y_predictedGenusTemp
        
    else:

        y_predictedGenusTemp=[RfclfFamilies[label] for i in x_testTempIndices]
        y_predicted[genus].iloc[x_testTempIndices]=y_predictedGenusTemp
        
for sublabel in np.unique(y_predicted[genus]):

    x_testTemp=x_test[y_predicted[genus]==sublabel]
    x_testTempIndices=np.where(y_predicted[genus]==sublabel)[0]

    if type(RfclfGenus[sublabel])!=str:

        y_predictedSpecieTemp=RfclfGenus[sublabel].predict(x_testTemp)
        y_predicted[species].iloc[x_testTempIndices]=y_predictedSpecieTemp

    else:

        y_predictedSpecieTemp=[RfclfGenus[sublabel] for i in x_testTempIndices]
        y_predicted[species].iloc[x_testTempIndices]=y_predictedSpecieTemp
        

display(y_predicted)
for label in labels[:-1]:
    
    cm=confusion_matrix(y_test[label],y_predicted[label],normalize='true')
    disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=np.unique(y_test[label]))
    disp = disp.plot(cmap=plt.cm.Blues,xticks_rotation="vertical")
cm=confusion_matrix(y_test[species],y_predicted[species])
somme=0
for i in range(len(cm)):
    for j in range(len(cm)):
        if i==j:
            somme+=cm[i][j]
print('Accuracy for Hierarchical clustering= ',somme/cm.sum())<jupyter_output>Accuracy for Hierarchical clustering=  0.9814736842105263
{'fraction_non_alphanumeric': 0.11028811750674786, 'fraction_numerical': 0.011047642960266148, 'mean_word_length': 4.029040404040404, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 53, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '258580', 'license': 'MIT', 'n_tokens_mistral': 5988, 'n_tokens_neox': 5636, 'n_words': 691, 'path': 'notebooks/2-Classification.ipynb', 'repo_name': 'LeroyAdrien/FrogCalls', 'size': '15963'}