Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python KNN精度计算中的数值误差_Python_Machine Learning_Scikit Learn_Knn - Fatal编程技术网

Python KNN精度计算中的数值误差

Python KNN精度计算中的数值误差,python,machine-learning,scikit-learn,knn,Python,Machine Learning,Scikit Learn,Knn,我有两个稀疏矩阵Timesort\u Y和Timesort\u X type(Timesort\u Y)和type(Timesort\u X)都是 scipy.sparse.csr.csr\u矩阵 # 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_st

我有两个稀疏矩阵
Timesort\u Y
Timesort\u X

type(Timesort\u Y)
type(Timesort\u X)
都是
scipy.sparse.csr.csr\u矩阵

# 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))
打印(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>

请纠正我哪里做错了。

请检查您的缩进,它看起来不正确。我被纠正了,因为循环仍然面临相同的错误。请共享您的
pred
y\u cv
变量的示例,并打印错误的完整堆栈跟踪。看起来您正在使用过时版本的scikit和更新版本的numpy。请更新scikit学习。我添加了打印(pred)和打印(y_cv)并出现完整错误。寻找解决方案。提前谢谢。
 <class 'scipy.sparse.csr.csr_matrix'>
 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().