机器学习: 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
阅读全文
0 0
- Python机器学习---KNN
- KNN--python--机器学习
- 机器学习: KNN--python
- python --机器学习--kNN
- 机器学习一,KNN python
- 机器学习 Python kNN算法
- 机器学习python之KNN
- Python机器学习应用--KNN
- python 机器学习实战KNN
- python KNN算法 机器学习
- Python机器学习(二)--kNN算法实现
- 机器学习之Knn&&python实践
- 机器学习之PYTHON实现KNN算法
- python 机器学习之kNN算法
- Python机器学习实战kNN分类算法
- 4.2KNN实例1--python机器学习
- python 机器学习KNN分类算法
- 【机器学习】使用python实现kNN算法
- LeetCode-Add Two Numbers
- 周中训练笔记4——乌鲁木齐网络赛&沈阳网络赛
- flume架构及源码解析
- 《Netty学习》(四)ChannelHandler学习
- 周末训练笔记(二)
- 机器学习: KNN--python
- Mybatis中#和$的区别
- quartz学习笔记4:lock
- 国电南瑞并网光伏系统发电性能评估项目开发总结
- DrawerLayout的侧拉布局添加的权限
- LeetCode-longest-palindromic-substring
- 《统计学习方法》笔记二
- 日暮途远,故吾倒行而逆施之.
- Pyhton 注释(单行、多行注释符号使用方法及规范)