Python Forum
Bad input shape for SVC
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bad input shape for SVC
#1
Hi everyone,

I am trying to test a support vector machine classifier on text data for a kernel I found. I found a kernel that uses a neural network on the data just fine but I cannot use a SVC. The link to the kernel is below:

https://www.kaggle.com/yufengdev/bbc-tex...gorization

The code for my SVC is

from sklearn.svm import SVC
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
k_fold = KFold(n_splits=5, shuffle=True, random_state=0)

clf = SVC()
scoring = 'accuracy'
score = cross_val_score(clf, x_train, y_train, cv=k_fold, n_jobs=1, scoring=scoring)
print(score)
The error I get is:

ValueError: bad input shape (1424, 5)

Does anyone know why I am getting this error and how I can resolve this problem?

Thanks
Reply
#2
If we use cited notebook as a basis for building train and test datasets,
we can see that keras.utils.to_categorical(y_train, num_classes)
is used. to_categorical is one-hot-encoder, so it turns y_train
with shape = (xxx, 1) to y_train with shape = (xxx, 5) (5 categories?). However,
SVC expects that the shape will be shape=(xxx, 1) (all categories should be integers, e.g. 1, 2, 3, 4, 5).

So, remove this line
y_train = keras.utils.to_categorical(y_train, num_classes)
somewhere in your code and everything should work fine.
You need to ensure that y_train consist only of numbers.
I am not sure, but it is likely that SVC requires that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with Shape/Reshape for CNN moddy10 0 1,428 Oct-12-2021, 03:54 PM
Last Post: moddy10
  Making a Basic Keras Model - Input Shape and Parameters MattKahn13 0 2,094 Aug-16-2020, 04:36 PM
Last Post: MattKahn13
  cannot reshape array of size 0 into shape Roro 2 6,196 Jun-14-2020, 11:28 AM
Last Post: Roro
  ValueError: could not broadcast input array from shape (75) into shape (25) route2sabya 0 6,443 Mar-14-2019, 01:14 PM
Last Post: route2sabya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020