I am experimenting in sklearn learn classification with some NLTK type tutorials. Can someone help me understand why the sklearn MLP neural network can handle different input shapes but the other classifiers cannot?
My input training data is a numpy.ndarray with shape (62, 2)
This is the only thing I know how to do for train test split (any tips appreciated if there is something better)
train_x = list(training[:,0])
train_y = list(training[:,1])
The data looks like this if I print(train_y):
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
The MLP classifier seems to work just fine.
model = MLPClassifier(learning_rate_init=0.0001,max_iter=9000,shuffle=True).fit(train_x, train_y)
But if I try to use the other sklearn classifiers, like:
model = GaussianNB().fit(train_x, train_y)
I get the error:
ValueError: y should be a 1d array, got an array of shape (62, 15) instead.
I think I need to incorporate .reshape(-1,1) somewhere in my code but not sure where. Any tips appreciated not a lot of wisdom here.
>Solution :
As far as I can see the labels y are in the one-hot form. Basically label is a vector with size equal to the number of classes. Each element of that vector is zero except at the index which represents the exact class. That element is one. This is why the shape of y is (62, 15)
U need to transform the label y into a form in which your labels will be represented as integers.
Example:
In this example we have 6 classes: ranging from 0 to 5
[0, 0, 0, 1, 0, 1] -> 3
[1, 0, 0, 0, 0, 0] -> 0
[0, 1, 0, 0, 0, 0] -> 1
You can do this by using the numpy.argmax(y, axis=1) which will return the index of element which has maximum value along the specified axis. Take a look at the documentation