12

I am trying to implement SVM Classifier over MNIST dataset. As my parameters are 3 dimensional its throwing the following error:

ValueError: Found array with dim 3. Expected <= 2

Following is my code snippet:

import mnist
from sklearn import svm

training_images, training_labels = mnist.load_mnist("training", digits = [1,2,3,4])
classifier = svm.SVC()
classifier.fit(training_images, training_labels)

Does sklearn support a multi-dimensional classifier?

1

2 Answers 2

16

One option for fixing the problem would be to reshape the input data into a 2-dimensional array.

Let's assume that your training data consists of 10 images which are each represented as an 3x3 matrix and therefore your input data is 3-dimensional.

[ [[1,2,3],   [[1,2,3],           [
   [4,5,6],    [4,5,6],            image 10 
   [7,8,9]] ,  [7,8,9]]  , ... ,           ] ]

We can turn each image into an array of 9 elements in order to convert the dataset into 2-dimensions.

dataset_size = len(training_images)
TwoDim_dataset = dataset.reshape(dataset_size,-1)

This would turn the data into the following shape:

[ [1,2,3,4,5,6,7,8,9]  ,  [1,2,3,4,5,6,7,8,9]  , ... ,  [image 10] ]
Sign up to request clarification or add additional context in comments.

Comments

9

The problem is with your input data.

You can use sklearn to load a digit dataset as well:

from sklearn.datasets import load_digits
from sklearn import svm

digits = load_digits()
X = digits.data
y = digits.target

classifier = svm.SVC()
classifier.fit(X[:1000], y[:1000])
predictions = classifier.predict(X[1000:])

1 Comment

Thanks, Converted my dataset into lower dimensions as done for the sklearn.datasets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.