ValueError in calculating KNN accuracy

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



ValueError in calculating KNN accuracy



I have two sparse matrices Timesort_Y and Timesort_X


Timesort_Y


Timesort_X



type(Timesort_Y) and also type(Timesort_X) are
scipy.sparse.csr.csr_matrix


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) is as below.


[[<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)) is a follows.


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



ERROR is at accuracy_score:


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

~Anaconda3libsite-packagessklearnmetricsclassification.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)

~Anaconda3libsite-packagessklearnmetricsclassification.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])

~Anaconda3libsite-packagessklearnutilsmulticlass.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

~Anaconda3libsite-packagessklearnutilsmulticlass.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

~Anaconda3libsite-packagesnumpylibarraysetops.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')

~Anaconda3libsite-packagesnumpylibarraysetops.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]))

~Anaconda3libsite-packagesscipysparsebase.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().



please correct me where i am doing wrong.





Please review your indentation, it looks like it's not correct.
– toti08
Aug 6 at 13:27





i am corrected the for loop still facing the same error.
– ravi
Aug 6 at 13:42





please share a sample of both your pred and y_cv variables
– desertnaut
Aug 6 at 13:47


pred


y_cv





Also print full stack trace of error. Looks like you are using an outdated version of scikit and newer version of numpy. Please update scikit-learn.
– Vivek Kumar
Aug 7 at 7:35





i had added print(pred) and print(y_cv) and complete error. looking for solution pls. Thanks in advance.
– ravi
Aug 8 at 2:30









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard