8

I am using sklearn to apply svm on my own set of images. The images are put in a data frame. I pass to the fit function a numpy array that has 2D lists, these 2D lists represents images and the second input I pass to the function is the list of targets (The targets are numbers). I always get this error "ValueError: setting an array element with a sequence".

trainingImages = images.ix[images.partID <=9]
trainingTargets = images.clustNo.ix[images.partID<=9]
trainingImages.reset_index(inplace=True,drop=True)
trainingTargets.reset_index(inplace=True,drop=True)

classifier = svm.SVC(gamma=0.001)
classifier.fit(trainingImages.image.values,trainingTargets.values.tolist())

The Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-5336fbeca868> in <module>()
      8 classifier = svm.SVC(gamma=0.001)
      9 
---> 10 classifier.fit(trainingImages.image.values,trainingTargets.values.tolist())
     11 
     12 #classifier.fit(t, list(range(0,2899)))

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
    148         self._sparse = sparse and not callable(self.kernel)
    149 
--> 150         X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
    151         y = self._validate_targets(y)
    152 

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    371                                       force_all_finite)
    372     else:
--> 373         array = np.array(array, dtype=dtype, order=order, copy=copy)
    374 
    375         if ensure_2d:

ValueError: setting an array element with a sequence.
1
  • What's the shape of trainingImages.image.values? X values passed to the fit function should be of shape (n_samples, n_features). If you have (n_samples, width, height), try X.reshape(-1, width*height). Commented Mar 20, 2016 at 17:49

3 Answers 3

8

I had the same exact error, it's one of two possibilities:

1- Data and labels are not in the same length.

2- For a specific feature vector, the number of elements are not equal.

Sign up to request clarification or add additional context in comments.

Comments

2

It's probably because "trainingImages.image.values" does not have the same number of elements in all it's arrays. Check a similar question here in stackoverflow:

ValueError: setting an array element with a sequence. while using SVM in scikit-learn

2 Comments

Thank you for your reply. I made sure that all its elements have the same size and I am still getting the same error.
Could you post a print of trainingImages.image.values and trainingTargets.values.tolist()? (as well as shape)
2

If you are sure the dimensions are correct, below's a piece of code/workflow that might help

import skimage.io as skio
import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
%matplotlib inline

# Load the data
trainingImages = skio.imread_collection('train/images/*.jpg',conserve_memory=True)

# cast to numpy arrays
trainingImages = np.asarray(trainingImages)

# reshape img array to vector
def reshape_image(img):
    return np.reshape(img,len(img)*len(img[0]))

img_reshape = np.zeros((len(trainingImages),len(trainingImages[0])*len(trainingImages[0][0])))

for i in range(0,len(trainingImages)):
    img_reshape[i] = reshape_image(trainingImages[i])

# SVM
clf = svm.SVC(gamma=0.01,C=10,kernel='poly')
clf.fit(img_reshape,trainingTargets.values.tolist())

Comments

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.