KNN - 不使用 sklearn 而使用 numpy 的算法

问题描述 投票:0回答:0

我最近开始上机器学习课,所以有一些误解我想弄清楚。 任务是编写适合解决分类问题(多类)的 KNN_classifier。只允许使用 Numpy,不允许使用 sklearn。对象之间的欧氏距离将被使用 这就是有线索的图案

import numpy as np

class KNN_classifier:
    def __init__(self, n_neighbors: int, **kwargs):
        self.K = n_neighbors

    def fit(self, x: np.array, y: np.array):
# TODO: write the .fit() method of the KNN_classifier class
# This function accepts two arrays as input:
# - # - x (feature set, nxm dimension array, n is the number of objects, m is the dimension of the feature vector)
# - y (training labels, one-dimensional array of dimension n)
# This function does not return anything, it must adjust the internal parameters of the model for further use
# Think about what is the learning process of this particular algorithm?
# What does this algorithm do at the moment when it has received a training sample?
# Implement this logic in code
        pass

    def predict(self, x: np.array):
        predictions = []
# TODO: write a method.predict(x) of the KNN_classifier class
# This method accepts a single array x as input. Array x is a two-dimensional array of objects for which a prediction is required
# # At the output of this function, we want to get a one-dimensional array of predictions, of dimension x.shape[0] (that is, for each object of the array x, we made our prediction)
# Remember how the KNN algorithm makes a prediction?
# Implement this logic in code
        predictions = np.array(predictions)
        return predictions

你能帮我写一个算法吗,至少给我正确的思考方向?

python-3.x numpy machine-learning knn
© www.soinside.com 2019 - 2024. All rights reserved.