计算KNN精度时出现ValueError

时间:2018-08-06 13:24:17

标签: python machine-learning scikit-learn knn

我有两个稀疏矩阵Timesort_YTimesort_X

type(Timesort_Y)type(Timesort_X)都是     scipy.sparse.csr.csr_matrix

# split the data set into train and test
X_1, X_test, y_1, y_test = cross_validation.train_test_split(Timesort_X, 
Timesort_Y, test_size=0.3, random_state=0)

# split the train data set into cross validation train and cross validation 
test

X_tr, X_cv, y_tr, y_cv = cross_validation.train_test_split(X_1, y_1, 
test_size=0.3)

for i in range(1,10,2):
  # instantiate learning model (k = 30)
  knn = KNeighborsClassifier(n_neighbors=i)

  # fitting the model on crossvalidation train
  knn.fit(X_tr, y_tr)

  # predict the response on the crossvalidation train
  pred = knn.predict(X_cv)

  # evaluate CV accuracy
  acc = accuracy_score(y_cv , pred, normalize=True) * float(100)———>>>> ERROR
  print(‘\nCV accuracy for k = %d is %d%%’ % (i, acc))

print(pred)如下。

  [[<1427x1 sparse matrix of type '<class 'numpy.int64'>'
   with 215 stored elements in Compressed Sparse Row format>
   <1427x1 sparse matrix of type '<class 'numpy.int64'>'
   with 1212 stored elements in Compressed Sparse Row format>]
  [<1427x1 sparse matrix of type '<class 'numpy.int64'>'
  with 215 stored elements in Compressed Sparse Row format>
 <1427x1 sparse matrix of type '<class 'numpy.int64'>'
  with 1212 stored elements in Compressed Sparse Row format>]
 [<1427x1 sparse matrix of type '<class 'numpy.int64'>'
  with 215 stored elements in Compressed Sparse Row format>
 <1427x1 sparse matrix of type '<class 'numpy.int64'>'
    with 1212 stored elements in Compressed Sparse Row format>]
   ...
    [<1427x1 sparse matrix of type '<class 'numpy.int64'>'
   with 215 stored elements in Compressed Sparse Row format>
  <1427x1 sparse matrix of type '<class 'numpy.int64'>'
  with 1212 stored elements in Compressed Sparse Row format>]
  [<1427x1 sparse matrix of type '<class 'numpy.int64'>'
   with 215 stored elements in Compressed Sparse Row format>

print(type(y_cv))是一个关注对象。

 <class 'scipy.sparse.csr.csr_matrix'>

错误在accuracy_score

 ValueError                                Traceback (most recent call last)
 <ipython-input-46-5e158b0a4f95> in <module>()
 16 
 17     # evaluate CV accuracy
  ---> 18     acc = accuracy_score(y_cv , pred, normalize=True) * float(100)
 19     print('\nCV accuracy for k = %d is %d%%' % (i, acc))
 20 

 ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in 
 accuracy_score(y_true, y_pred, normalize, sample_weight)
174 
175     # Compute accuracy for each possible representation
 --> 176     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
177     if y_type.startswith('multilabel'):
178         differing_labels = count_nonzero(y_true - y_pred, axis=1)

 ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in 
 _check_targets(y_true, y_pred)
 71     check_consistent_length(y_true, y_pred)
 72     type_true = type_of_target(y_true)
  ---> 73     type_pred = type_of_target(y_pred)
 74 
 75     y_type = set([type_true, type_pred])

 ~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in 
 type_of_target(y)
248         raise ValueError("y cannot be class 'SparseSeries'.")
249 
 --> 250     if is_multilabel(y):
251         return 'multilabel-indicator'
252 

    ~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in 
 is_multilabel(y)
150                  _is_integral_float(np.unique(y.data))))
151     else:
 --> 152         labels = np.unique(y)
153 
154         return len(labels) < 3 and (y.dtype.kind in 'biu' or  # bool, 
 int, uint

 ~\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, 
return_index, return_inverse, return_counts, axis)
221     ar = np.asanyarray(ar)
222     if axis is None:
 --> 223         return _unique1d(ar, return_index, return_inverse, 
return_counts)
224     if not (-ar.ndim <= axis < ar.ndim):
225         raise ValueError('Invalid axis kwarg specified for unique')

 ~\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py in _unique1d(ar, 
 return_index, return_inverse, return_counts)
281         aux = ar[perm]
282     else:
 --> 283         ar.sort()
284         aux = ar
285     flag = np.concatenate(([True], aux[1:] != aux[:-1]))

 ~\Anaconda3\lib\site-packages\scipy\sparse\base.py in __bool__(self)
286             return self.nnz != 0
287         else:
 --> 288             raise ValueError("The truth value of an array with more 
 than one "
289                              "element is ambiguous. Use a.any() or 
 a.all().")
290     __nonzero__ = __bool__

 ValueError: The truth value of an array with more than one element is 
  ambiguous. Use a.any() or a.all().

请纠正我在哪里做错了。

0 个答案:

没有答案