Sklearn kNN usage with a user defined metric

You pass a metric as metric param, and additional metric arguments as keyword paramethers to NN constructor: >>> def mydist(x, y): … return np.sum((x-y)**2) … >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]) >>> nbrs = NearestNeighbors(n_neighbors=4, algorithm=’ball_tree’, … metric=”pyfunc”, func=mydist) >>> nbrs.fit(X) NearestNeighbors(algorithm=’ball_tree’, leaf_size=30, metric=”pyfunc”, n_neighbors=4, radius=1.0) … Read more

Getting TypeError: ‘(slice(None, None, None), 0)’ is an invalid key

Since you are trying to access directly as array, you are getting that issue. Try this: from sklearn.impute import SimpleImputer imputer = SimpleImputer(missing_values = np.nan, strategy = ‘mean’,verbose=0) imputer = imputer.fit(X.iloc[:, 1:3]) X.iloc[:, 1:3] = imputer.transform(X.iloc[:, 1:3]) Using iloc/loc will resolve the issue.

Millions of 3D points: How to find the 10 of them closest to a given point?

Million points is a small number. The most straightforward approach works here (code based on KDTree is slower (for querying only one point)). Brute-force approach (time ~1 second) #!/usr/bin/env python import numpy NDIM = 3 # number of dimensions # read points into array a = numpy.fromfile(‘million_3D_points.txt’, sep=’ ‘) a.shape = a.size / NDIM, NDIM … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)