Negative dimensions are not allowed error

I have trained an SVM using the NSL-KDD dataset but when I try to make a prediction on it, it comes up with the error “negative dimensions are not allowed”.

full error with traceback:

File ~\OneDrive\Documents\ids\ in
predictions = classifier.predict(test_features)

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

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

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

ValueError: negative dimensions are not allowed

As it says, trying to make a prediction from those test features, caused the classifier library to try to make a Numpy array with a negative size for some dimension, and Numpy complained because that doesn’t make sense.

The np.eye function makes an identity matrix (it is normally square, and has 1s on the main diagonal and 0s everywhere else). It seems that the classifier tried to call this with a single argument (to make a square matrix), self.nb_classes. That should be an integer, and it sounds like it means the number of classes. I guess that this was internally set to -1 as some kind of initial value, that should get replaced somewhere else but didn’t.

The best we can conclude from this is that there is something wrong with the data; but you haven’t shown us where the data comes from. (I think it would be nice if the classifier library checked for this and gave you a nicer error message, but this is how things normally work out - it’s a lot harder for programmers to consider what can go wrong, than to make sure of what happens when it goes right.)

1 Like

Hi, Thanks for getting back to me. I have encoded my labels from the dataset split the dataset into train and test. Is it maybe because I have changed the dataset into a numpy array thats causing this issue?

Also, I thought that maybe this error came from a data mismatch because when I run this code, it is similar.

(5039, 38)

(5039,) `


If anyone can help me solve this error it would be much appreciated :slight_smile: