Python scikit-learn SVM Classifier "ValueError: Found array with dim 3. Expected <= 2"

Hitanshu Tiwari picture Hitanshu Tiwari · Oct 16, 2015 · Viewed 23.5k times · Source

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?

Answer

Zahra picture Zahra · Jan 26, 2016

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] ]