SupervisedDBNClassification' object has no attribute 'classes_'

Question:

I am using supervisedDBN learning code which is deep learning architecture, i customize the below code and got the following error…
I am working on KDD99 Network security dataset to analyze multiple attacks.
but have the following error in code. how to fix it I don’t know

import numpy as np
np.random.seed(1337)  # for reproducibility
import pandas as pd
from sklearn import preprocessing
import matplotlib.pyplot as plt

import seaborn as sns
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.metrics.classification import accuracy_score
from sklearn.metrics import f1_score, confusion_matrix
from sklearn.preprocessing import StandardScaler
from yellowbrick.classifier import ClassificationReport
from yellowbrick.classifier import ConfusionMatrix
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score

from dbn.tensorflow import SupervisedDBNClassification
path ="E:/MS Data/HEC-project/kdd_dataset.csv"
df = pd.read_csv(path)

df["label"].value_counts().plot(kind="bar");

df['label'].value_counts()
print(df['label'].value_counts())

labels = df['label'].values

classes = ["back","back_overflow","guess_passwd","ipsweep","neptune","nmap","pod","portsweep","satan","smurf","teardrop","warezclient","warezmaster","Normal"]
unique_val = np.array(labels)
print(classes)

le = preprocessing.LabelEncoder()
# Converting string labels into numbers.
df['label']=le.fit_transform(df['label'])

index = ["back","back_overflow","guess_passwd","ipsweep","neptune","nmap","pod","portsweep","satan","smurf","teardrop","warezclient","warezmaster","Normal"]
columns =  ["back","back_overflow","guess_passwd","ipsweep","neptune","nmap","pod","portsweep","satan","smurf","teardrop","warezclient","warezmaster","Normal"]

X = df.drop("label", axis=1).values
y = df["label"].values

#Splitting dataset into training and testing phase:
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=.30,random_state=1)


classifier = SupervisedDBNClassification(hidden_layers_structure=[50, 50],
                                         learning_rate_rbm=0.2,
                                         learning_rate=0.2,
                                         n_epochs_rbm=100,
                                         n_iter_backprop=100,
                                         batch_size=130,
                                         activation_function='relu',
                                         dropout_p=0.2)

classifier.fit(X_train,y_train)

# Save the model
classifier.save('model.pkl')

# Restore it
classifier = SupervisedDBNClassification.load('model.pkl')

# Test
Y_pred = classifier.predict(X_test)

print("Accuracy", metrics.accuracy_score(y_test, Y_pred))

visualizer = ClassificationReport(classifier, support='percent' )

visualizer.fit(X_train, y_train)
y_pred=visualizer.predict(X_test)
cm=confusion_matrix(y_test, y_pred)
visualizer.poof() 

#print("Accuracy", metrics.accuracy_score(y_test, y_pred))

#print(accuracy_score(y_test, y_pred))

def cm_analysis(y_true, y_pred, labels, ymap=None, figsize=(15,10)):

    if ymap is not None:
        y_pred = [ymap[yi] for yi in y_pred]
        y_true = [ymap[yi] for yi in y_true]
        labels = [ymap[yi] for yi in labels]
    cm = confusion_matrix(y_true, y_pred, labels=labels)
    cm_sum = np.sum(cm, axis=1, keepdims=True)
    cm_perc = cm / cm_sum.astype(float) * 100
    annot = np.empty_like(cm).astype(str)
    nrows, ncols = cm.shape
    for i in range(nrows):
        for j in range(ncols):
            c = cm[i, j]
            p = cm_perc[i, j]
            if i == j:
                s = cm_sum[i]
                annot[i, j] = '%.1f%%n%d/%d' % (p, c, s)
            elif c == 0:
                annot[i, j] = ''
            else:
                annot[i, j] = '%.1f%%n%d' % (p, c)
    cm = pd.DataFrame(cm, index, columns)
    cm.index.name = 'Actual'
    cm.columns.name = 'Predicted'

    fig, ax = plt.subplots(figsize=figsize)

    sns.heatmap(cm, annot=annot, fmt='', ax=ax)
    #plt.savefig(filename)
    plt.show()

cm_analysis(y_test, y_pred, classifier.classes_, ymap=None, figsize=(8,6))

Error:

AttributeError: ‘SupervisedDBNClassification’ object has no attribute ‘classes_’

Asked By: Uzair

||

Answers:

Unfortunately SupervisedDBNClassification doesn’t have the attribute classes_ like most sklearn models. But you can make use of the attribute idx_to_label_map which will return a dictionary of index to label maps. So instead of classifier.classes_ you can use
classifier.idx_to_label_map and to get only the labels as a list you can do the following
list(classifier.idx_to_label_map.values()). So replace

cm_analysis(y_test, y_pred, classifier.classes_, ymap=None, figsize=(8,6))   

with

cm_analysis(y_test, y_pred, list(classifier.idx_to_label_map.values()), ymap=None, figsize=(8,6))

which should do the trick for you.