受监督DBNClassization"对象没有属性"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))

错误:

属性

错误:"受监督的DBNClassization"对象没有属性"classes_">

不幸的是,SupervisedDBNClassification不像大多数sklearn模型那样具有属性classes_。但是您可以使用属性idx_to_label_map它将返回索引字典来标记映射。因此,您可以使用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))

这应该可以为您解决问题。

相关内容

  • 没有找到相关文章

最新更新