机器学习: KNN--python

来源:互联网 发布:redis数据存储在哪里 编辑:程序博客网 时间:2024/06/06 18:33

今天介绍机器学习中比较常见的一种分类算法,K-NN,NN 就是 Nearest Neighbors, 也就是最近邻的意思,这是一种有监督的分类算法,给定一个 test sample, 计算这个 test sample 与 training set
里每个 training sample 的距离,选择离 test sample 最近的 K 个,然后通过投票选择这 K 个样本中,属于哪类的最多,那么这个 test sample 就属于哪类。K-NN 比较简单直观,也很好理解,一般需要考虑的就是设置 K 的大小,以及如何计算样本之间的距离,比较常用的是欧式距离。下面给出一段简单的代码,说明这个算法的使用。

from sklearn import datasetsimport numpy as npimport operatordef Knn_Classify (x, Train_data, labels, k):    N_sample = Train_data.shape[0]    diff_mat = np.tile(x, (N_sample, 1)) - Train_data    Sq_diffmat = diff_mat **2    Sq_dis = Sq_diffmat.sum(axis = 1)    Dis = Sq_dis ** 0.5    Index = Dis.argsort()    C_count = {}    for i in range (k):        votelabel = labels[Index[i]]        C_count[votelabel] = C_count.get(votelabel, 0) + 1    Sort_K = sorted(C_count.iteritems(),        key = operator.itemgetter(1), reverse=True)    return Sort_Kiris = datasets.load_iris()x_data = iris.datay_label = iris.targetclass_name = iris.target_namesn_sample = len(x_data)np.random.seed(0)index = np.random.permutation(n_sample)x_data = x_data[index]y_label = y_label[index]ratio = 0.8train_x = x_data[ : int(ratio * n_sample)]train_y = y_label[ : int(ratio * n_sample)]test_x = x_data[int(ratio * n_sample) :]test_y = y_label[int(ratio * n_sample) : ]n_test = len(test_x)p_label = np.zeros((len(test_y)))for i in range (n_test):    in_x = test_x [i, :]    target_label = test_y [i]    predict_value = Knn_Classify(in_x, train_x, train_y, 5)    p_label[i] = predict_value[0][0]#    print "the predict label is: ", predict_value#    print "the target_label is: ", target_labelt = (p_label == test_y)acc = t.sum()*1.0/len(test_y)print "the accuracy is: ", acc
原创粉丝点击