python - SupervisedDBNClassification'对象没有属性 'classes_'

标签 python scikit-learn deep-learning spyder dbn

我正在使用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 ,它将返回标签映射的索引字典。因此,您可以使用 classifier.classes_ 代替 classifier.idx_to_label_map 并仅获取标签作为列表,您可以执行以下操作 列表(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))

这应该对你有用。

关于python - SupervisedDBNClassification'对象没有属性 'classes_',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58638406/

相关文章:

keras - 使用自动编码器的 flow_from_directory 按需加载数据

machine-learning - 神经网络中的激活函数

python - 在名称已更改的列中查找唯一值时出现 Pandas 错误

python - 检查列表中字符串内的字符串

python - TensorFlow 1.0.0 的prepare_attention API 相当于tensorflow 1.2.0

python - 如何使用 sklearn 从 ONE-HOT-ENCODED 标签返回到单列?

python - Keras LSTM - 验证损失从时期 #1 开始增加

python - 当给定网站动态更改时,如何了解我的帖子请求的最后一页索引号

python-3.x - 类型错误 : object of type 'numpy.int64' has no len()/TypeError: object of type 'int' has no len()/while using classification_report in scikitlearn

python - 如何将 Datetime 和 int 功能与 Scikit learn 混合使用?