负维度错误使用 sklearn svm

问题描述 投票:0回答:0

我已经使用 NSL-KDD 数据集训练了一个 SVM,但是当我尝试对其进行预测时,它出现了错误“不允许使用负维度”。

下面是我的代码:

#Indexing of features and labels 
features = dataset[:,:-2]
labels = dataset[:,-2]

#split data
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.2)

#train model
model = SVC(C=1.0, kernel='rbf')

# create classifier
classifier = SklearnClassifier(model=model)

#Train  classifier
model.fit(train_features, train_labels)


predictions = classifier.predict(test_features)

我以为可能会有数据不匹配但是当我运行以下代码时,我得到了这个:

print(np.shape(test_features))
(5039, 38)

print(np.shape(test_labels))
(5039,) 

带有回溯的完整错误:

File ~\OneDrive\Documents\ids\fgsm.py:50 in <module>
   predictions = classifier.predict(test_features)

File ~\AppData\Roaming\Python\Python39\site-packages\art\estimators\classification\classifier.py:73 in replacement_function
   return fdict[func_name](self, *args, **kwargs)

File ~\AppData\Roaming\Python\Python39\site-packages\art\estimators\classification\scikitlearn.py:1418 in predict
   one_hot_targets = np.eye(self.nb_classes)[targets]

 File C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\twodim_base.py:214 in eye
   m = zeros((N, M), dtype=dtype, order=order)

ValueError: negative dimensions are not allowed

我不使用 model.predict 的主要原因是因为我使用 Adversarial Robustness Toolbox 中的分类器估计器

希望有人能帮我解决这个问题

python python-3.x scikit-learn svm adversarial-attack
© www.soinside.com 2019 - 2024. All rights reserved.