First time here? Checkout the FAQ!
x
+1 vote
2.6k views
asked in Machine Learning by (160 points)  
I am not able to figure out how the calculation of the $m$ nearest points will be in a single dimensional array using kNN.  Can anyone offer a clue or example?
Thank you
  

1 Answer

+1 vote
answered by (116k points)  

It could be a single dimensional array (vector) of $m$ nearest points. For example, let's assume the following are the data-points ($X$) and classes ($y$):

X = np.array([[0,1,3], [1,2,4], [2,3,2], [3,4,3]])
y = np.array([0, 0, 1, 1])

and you want to show us the $k=3$ nearest neighbors to the following array:

test = np.array([[1,2,3]])

 You can create a Numpy array for the  as follows:

array([[1, 0, 2]])

And it means the closest neighbors in order are the second element of $X$ ([1,2,4]), then the first element of $X$ ([0,1,3]), and finally the third element of $X$ ([2,3,2]).

commented by (160 points)  
I understand when the problem involves a vector. However I have a problem which involves a single dimensional array.  For eg:

aData = np.array([1,2,3,4,5,6,7.23,45])

How can I find say 3 nearest points to a point with a function which takes aData, k and p (point) as parameter.  In this problem I am not sure what is the relevance of the aData , since it is single dimensional array.

kNN seems to work only if it is multi-dimensional.

I get this error when using the fit method:
ValueError: Expected 2D array, got 1D array instead:
array=[ 1  3  4  5  7  8 11 12 13 15 19 24 25 29 40].
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.

Related questions

+1 vote
3 answers 2.9k views
+3 votes
2 answers 12.3k views
+1 vote
1 answer 346 views
+3 votes
1 answer 551 views
...