机器学习 KNN

# -*- coding:utf-8 -*-
import numpy as np


class KNN(object):
    def __init__(self, k):
        self.k = k

    def fit(self, x, y):
        self.x_train = np.asarray(x)
        self.y_train = np.asarray(y)

    def fetch_k_neighbors(self, x):
        distincts = [np.sum((x1 - x) ** 2) for x1 in self.x_train]
        distincts = list(zip(range(len(distincts)), distincts))
        distincts.sort(key=lambda t: t[1])
        top_k_list = distincts[:self.k]
        return list(map(lambda t: t[0], top_k_list))    

    def predict(self,test):
        result = []
        for x in test:
            neighbors = self.fetch_k_neighbors(x)  # 返回索引
			# 合并相同label,value计数
            label_count_dict = {}
            for neighbor_index in neighbors:
                label = self.y_train[neighbor_index]
                label_count_dict[label] = label_count_dict.get(label, 0) + 1
			# 根据value计数,找对大label
            max_label_count = 0
            for key, value in label_count_dict.items():
                if value > max_label_count:
                    max_label_count = value
                    max_label = key
            result.append(max_label)
        return result

if __name__ == '__main__':
    x = [[1, 1],
         [2, 1],
         [3, 1],
         [4, 1],
         [5, 2],
         [6, 2]]
    y = [0, 0, 0, 1, 1, 1]
    algo = KNN(k=3)
    algo.fit(x, y)
    test = [[2.5, 1.5], [9, 3], [3.6, 1.5]]
    print("预测值y的值:\n{0}".format(algo.predict(test)))

你可能感兴趣的:(机器学习)