Question

I am trying to store individual feature vector (ie X_train[i]) into an array X and its corresponding label in another array Y. When I try to fit these two arrays,I get the error ValueError: setting an array element with a sequence. How to fix this error. Thanks in advance.

from sklearn.datasets import load_svmlight_file                                           
pathToTrainData="/Users/rkasat/Documents/final year project/scripts/Drydata/leaf/train_backup.txt"

X_train,Y_train= load_svmlight_file(pathToTrainData);
X= []    
y=[]
for i in range(5):
    X.append(X_train[i])
    y.append(Y_train[i])

print(type(X[0]),type(y[0]))
from sklearn import svm
clf = svm.SVC(kernel='linear')
clf.fit(X,y)

output:
--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-cd4b481af30a> in <module>()
      8 from sklearn import svm
      9 clf = svm.SVC(kernel='linear')
---> 10 clf.fit(X,y)

/Users/rkasat/anaconda/lib/python2.7/site-packages/sklearn/svm/base.pyc in fit(self, X, y, sample_weight)
    137                              "by not using the ``sparse`` parameter")
    138 
--> 139         X = atleast2d_or_csr(X, dtype=np.float64, order='C')
    140         y = self._validate_targets(y)
    141 

/Users/rkasat/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.pyc in atleast2d_or_csr(X, dtype, order, copy, force_all_finite)
    132     """
    133     return _atleast2d_or_sparse(X, dtype, order, copy, sparse.csr_matrix,
--> 134                                 "tocsr", force_all_finite)
    135 
    136 

/Users/rkasat/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.pyc in _atleast2d_or_sparse(X, dtype, order, copy, sparse_class, convmethod, force_all_finite)
    109     else:
    110         X = array2d(X, dtype=dtype, order=order, copy=copy,
--> 111                     force_all_finite=force_all_finite)
    112         if force_all_finite:
    113             _assert_all_finite(X)

/Users/rkasat/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.pyc in array2d(X, dtype, order, copy, force_all_finite)
     89         raise TypeError('A sparse matrix was passed, but dense data '
     90                         'is required. Use X.toarray() to convert to dense.')
---> 91     X_2d = np.asarray(np.atleast_2d(X), dtype=dtype, order=order)
     92     if force_all_finite:
     93         _assert_all_finite(X_2d)

/Users/rkasat/anaconda/lib/python2.7/site-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
    318 
    319     """
--> 320     return array(a, dtype, copy=False, order=order)
    321 
    322 def asanyarray(a, dtype=None, order=None):

ValueError: setting an array element with a sequence.

(<class 'scipy.sparse.csr.csr_matrix'>, <type 'numpy.float64'>)
Was it helpful?

Solution

Probably you don't have to use the for loop in your code. The following code might do what you want to do:

X_train, Y_train = load_svmlight_file(pathToTrainData);

from sklearn import svm
clf = svm.SVC(kernel='linear')
clf.fit(X[:5, :],y[:5])

OTHER TIPS

@tanemaki is right, but it is worth explaining why this resolves the problem. X_train is (most likely) a numpy array. Slicing that with an integer (X_train[i]) returns the entire i-th row. X ends up being a list of numpy arrays. The fit method expects a single matrix. If you want to train on only the first 5 rows, you should slice as @tanemaki already demonstrated: X[:5, :] and y[:5, :]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top