机器学习: KNN--python

K-NN算法详解及应用
本文介绍了机器学习中常见的K-NN分类算法原理及其Python实现。K-NN是一种直观的有监督学习方法,通过计算待测样本与训练集样本间的距离来分类。文中给出了基于鸢尾花数据集的具体实例。

今天介绍机器学习中比较常见的一种分类算法,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 datasets
import numpy as np
import operator


def 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_K



iris = datasets.load_iris()
x_data = iris.data
y_label = iris.target
class_name = iris.target_names

n_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.8

train_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_label


t = (p_label == test_y)
acc = t.sum()*1.0/len(test_y)

print "the accuracy is: ", acc

转载于:https://www.cnblogs.com/mtcnn/p/9412388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值