SupervisedDBNClassification' 对象没有属性 'classes_'

SupervisedDBNClassification' object has no attribute 'classes_'

我正在使用 supervisedDBN 深度学习架构的学习代码,我自定义了下面的代码并得到了以下错误... 我正在研究 KDD99 网络安全数据集来分析多种攻击。 但在代码中有以下错误。不知道怎么解决

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))

错误:

AttributeError: 'SupervisedDBNClassification' object has no attribute 'classes_'

不幸的是,SupervisedDBNClassification 没有像大多数 sklearn 模型那样的属性 classes_。但是您可以使用 idx_to_label_map 属性,它将 return 一个索引字典来标记地图。因此,您可以使用 classifier.classes_ 而不是 classifier.idx_to_label_map 并仅获取标签作为列表,您可以执行以下操作 list(classifier.idx_to_label_map.values())。所以替换

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

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

哪个应该适合你。

希望对您有所帮助!