nearest-neighbor

Avoiding for-loop in NumPy 1D nearest neighbors

Avoiding for-loop in NumPy 1D nearest neighbors Question: I have the following code in which I get the N nearest neighbors in 1D: import numpy as np def find_nnearest(arr, val, N): idxs = [] for v in val: idx = np.abs(arr – v).argsort()[:N] idxs.append(idx) return np.array(idxs) A = np.arange(10, 20) test = find_nnearest(A, A, 3) …

Total answers: 1

How to n-times train K Nearest Neighboar (Knn)?

How to n-times train K Nearest Neighboar (Knn)? Question: Is there a way to tell the Knn function in Python how often he should train the model before I predict a possible outcome? From my understanding, if a model is trained more than one time, it is more robust. Or do I get that wrong? …

Total answers: 1

How to get 1:1 corresponding matches using sklearn KNearest neighbors

How to get 1:1 corresponding matches using sklearn KNearest neighbors Question: I’m writing an algorithm to match each person from setA with someone from setB, based on interest similarity, using NearestNeighbors(n_neighbors = 1). This is what I have so far: dfA = pd.DataFrame(np.array([[1, 1, 1, 1], [1,1,2,2], [4, 5, 2, 0], [8, 8, 8, 8]]), …

Total answers: 1

How to retrieve nodes from a sklearn.neighbors.KDTree?

How to retrieve nodes from a sklearn.neighbors.KDTree? Question: Is there a way to get a node by id, or all nodes, from a sklearn.neighbors.KDTree instance? from sklearn.neighbors import KDTree import numpy as np tree = KDTree(np.array([[0., 0., 0.], [1., 1., 1.]])) # How to get the array [[0., 0., 0.], [1., 1., 1.]] back out? …

Total answers: 1

Sklearn: KNeighborsRegressor vs KNeighborsClassifer

Sklearn: KNeighborsRegressor vs KNeighborsClassifer Question: What is the difference between the KNeighborsRegressor and the KNeighborsClassifier of the sklearn library? I’m trying to use the kNN algorithm to make predictions on a datset that has the names of certain emotions (like happy, sad, angry) as possible classes. The attributes are numerical pixel values. I’ve learned that …

Total answers: 3

How to get all the values of neighbours around an element in matrix?

How to get all the values of neighbours around an element in matrix? Question: I need to get the values of all the neighbors around an element in a matrix in python. Suppose I have a matrix like below, matrix=[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] For the first element i.e., …

Total answers: 2

How to filter numpy array by list of indices?

How to filter numpy array by list of indices? Question: I have a numpy array, filtered__rows, comprised of LAS data [x, y, z, intensity, classification]. I have created a cKDTree of points and have found nearest neighbors, query_ball_point, which is a list of indices for the point and its neighbors. Is there a way to …

Total answers: 5

Nearest Neighbor Search: Python

Nearest Neighbor Search: Python Question: I have a 2 dimensional array: MyArray = array([6588252.24, 1933573.3, 212.79, 0, 0], [6588253.79, 1933602.89, 212.66, 0, 0], etc…) The first two elements MyArray[0] and MyArray[1] are the X and Y coordinates of the points. For every element in the array, I would like to find the quickest way to …

Total answers: 2

Pixel neighbors in 2d array (image) using Python

Pixel neighbors in 2d array (image) using Python Question: I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create a function let’s call it “neighbors” with the following input parameter: x: a numpy 2d array (i,j): the index of an element in a 2d array d: neighborhood radius As output I …

Total answers: 8