image processing (KNN classifier) - python

Asked

Viewed 69 times

2

When using Kneighborsclassifier, I am getting the following error:

/Feature Extraction (Python)/KNNpy:58: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  knn.fit(trade_conj, trade_label)

Traceback (most recent call last):

  File "/Feature Extraction (Python)/KNNpy", line 61, in <module> 
pred = knn.predict(features[i, :])#processo KNN
  File "\Python\Python37\lib\site-packages\sklearn\neighbors\_classification.py", line 171, in predict
    X = check_array(X, accept_sparse='csr')
  File "\Python\Python37\lib\site-packages\sklearn\utils\validation.py", line 556, in check_array "if it contains a single sample.".format(array))

ValueError: Expected 2D array, got 1D array instead: array=[0. 0. 0. ... 0. 0. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

I’m working with a matrix "Features" (22x2048) from the characteristic extraction process.

and an array "Labels" 1x22 representing the labels.

trade_conj="Features" and trade_labbel="Labels"

I am not intimate with python, however, this code is a "translation"(?) of another code in Matlab that uses the same array and matrix and works normally.

1 answer

0


The problem there is with the dimension of the parameters you are going through. Without the data and code you used it is difficult to know exactly what to do, but try to start with the suggestion that the error gives you, use:

knn.fit(trade_conj, trade_label.ravel())

So your Y_train will be a flat vector, and also:

pred = knn.predict(features[i, :].reshape(-1,1))

So you will have a 2D array (an "array" of a single column) because features[i, :] returns an array 1D.

Different functions require parameters in different formats, with time you get used. I hope I helped, if not, please share the part of the code that gives this problem and the data so that it is possible to test.

EDIT:

Example of array 1D: [1, 2, 3, 4, 5]

Exemplo de array 2D:[[1],[2],[3],[4],[5]] ou [[1,2,3],[4,5,6]]

  • that’s right, I was able to solve the problem with: Y_training = Y_training.values.ravel() Thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.