Here is the predict function i wrote for Machine learning
``` def predict(dists, labels, k=1): """ Given a shape-(M, N) array of distances between M-unlabeled and N-labeled images, and N labels, predict a label for each of the M images based on its k-nearest neighbors. dists : numpy.ndarray `dists.shape` must be (M, N) where M is the number of examples you wish to predict labels for, and N is the number of labeled images used in the prediction labels : numpy.ndarray A shape-(N,) array of class-IDs, of labels for the N images. Rtns: y_pred : numpy.array` A shape-(M,) array of class-IDs, as predicted by the k-nearest neighbors. """ num_test = dists.shape[0] y_pred = np.zeros(num_test) for i in range(num_test): close_y = [] k_nearest_idxs = np.argsort(dists[i])[:k] close_y = labels[k_nearest_idxs] y_pred[i] = np.argmax(np.bincount(close_y)) return y_pred.astype(np.int64) ```
This is how I call this function with dists and labels as below:
d=np.array([[0., 1., 1., 1.]])
l=np.array([0, 1, 1, 0])
predict(d, l, 3)
Expected return value for this function is: array([0]) but my function returns array([1]) dists and lables are numpy arrays and k=3(default is 1)
It fails for other data as well but this is the first of the failure i seem to see.
Any suggestions.. Thx in Advance.
https://stackoverflow.com/questions/66913952/ml-predict-function-returning-wrong-value-predictdists-labels-k-1 April 02, 2021 at 11:07AM
没有评论:
发表评论