如何将预测函数泛化为k个最近邻居的任何k个?

时间:2018-07-21 21:19:44

标签: machine-learning

def fit(self,X_train,y_train):         self.X_train = X_train         self.y_train = y_train

def predict(self, X_test):
    predictions = []
    for row in X_test:
        label = self.closest(row)
        predictions.append(label)
    return predictions

def closest(self, row):
    # Distance from test point to first training point
    best_dist = euc(row, self.X_train[0]) # Get the first one
    best_index = 0 #index
    for i in range(1, len(self.X_train)): # Iterate over all other training points
        dist = euc(row, self.X_train[i])
        #print(dist)
        if dist < best_dist: # Found closer, update
            best_dist = dist
            best_index = i
    return self.y_train[best_index]

0 个答案:

没有答案