Is there a function for finding the midpoint of n points on sklearn.neighbors.NearestNeighbors?
Date : January 11 2021, 03:34 PM

To fix this issue The second return value of the function gives you the indices of the points in the sample that you want. Basically the midpoint is formed by taking the mean of each column. You can do this with the mean method of np.array objects. Assuming the original set of points is samples, and that indexes of the desired points is inds: import numpy as np
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
inds = [1,2]
print(np.array(samples)[inds].mean(axis=0))
inds = yournnmodel.radius_neighbors(your_query_point).asarray()[1][0]
Boards Message : 
You Must Login
Or Sign Up
to Add Your Comments . 
Share :

How to iterate over points in VTK while finding the Nearest Neighbors of each point?
Date : March 29 2020, 07:55 AM
With these it helps I found the problem...I was using /vtkPointSource which generates random points and I was feeding in my points into that function. I am not sure why, but that made getting results out very difficult but once I passed in the correct information and placed a Reset() on the point Id array and the result Id array, it works like a champ. Hopefully this will same others some trouble because I spent some hrs working on this. Luke H vtkSmartPointer<vtkKdTreePointLocator> Tree =
vtkSmartPointer<vtkKdTreePointLocator>::New();
Tree>SetDataSet( Output );
Tree>BuildLocator();
vtkSmartPointer<vtkIdList> result =
vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> point_ind =
vtkSmartPointer<vtkIdList>::New();
unsigned int k = 5;
double testpoint[3];
for( vtkIdType n = 0; n < Output>GetNumberOfPoints(); n++ )
{
Output>GetPoint( n,testpoint );
std::cout << "Point: " << testpoint[0] << ", " << testpoint[1]
<< ", " << testpoint[2] << ": " << endl;
Tree>FindClosestNPoints( k, testpoint, result );
for(vtkIdType i = 0; i < k; i++)
{
vtkIdType point_ind = result>GetId( i );
double p[3];
Output>GetPoint( point_ind, p );
std::cout << "Closest point " << i+1 << ": Point "
<< point_ind << ": (" << p[0] << ", " << p[1] << ", "
<< p[2] << ")" << std::endl;
}
result>Reset();
point_ind>Reset();
}

SkLearn estimator error in NearestNeighbors
Date : March 29 2020, 07:55 AM
hope this fix your issue NearestNeighbors will only give you the neighbors, i think you need to try KNeighborsClassifier. Try this: from sklearn import neighbors, cross_validation
for k in range(1, 50):
neigh = neighbors.KNeighborsClassifier(n_neighbors=k)
neigh.fit(data, dataClass)
a = cross_validation.cross_val_score(neigh, data, y=dataClass, cv=kf)

Custom metric for NearestNeighbors sklearn
Date : March 29 2020, 07:55 AM
should help you out The problem: Numpy can't handle numbers so big and converts them to float losing a lot of precision.

numpy argsort vs sklearn NearestNeighbors
Tag : python , By : marocchino
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further In general, sorting an array is going to be more computationally efficient than fitting a learned model like a nearest neighbors classifier. However, you may be to gain a runtime advantage if you only need to fit the model one time at the beginning. Sorting an array with Numpy has a best time complexity of O(nlogn). If you have to sort every time you do a lookup, this might be inefficient. The KDTree algorithm that you can configure scikitlearn's NearestNeighbors class to use can be constructed in a time that is O(nlogn) and then lookups can be performed in a time that is O(logn). So if you only need to build the KDTree once at the beginning, it may be more efficient. Note that the search will have to compute distances from your feature vectors, so if your feature space is highdimensional, this may end up being less efficient. And if you won't ever be encountering unseen data points it may be entirely wasteful.

confused with the output of sklearn.neighbors.NearestNeighbors
Date : March 29 2020, 07:55 AM
wish helps you Its pretty straightforward actually. For each data sample in the input to kneighbors() (X here), it will show 2 neighbors. (Because you have specified n_neighbors=2. The indices will give you the index of training data (again X here) and distances will give you the distance for the corresponding data point in training data (to which the indices are referring). Take an example of single data point. Assuming X[0] as the first query point, the answer will be indices[0] and distances[0]



Related QUESTIONS :
