在这一部分书中的例子是电影的分类
- kNN理论,如何用距离来分类
- python导入数据
- 陷阱/隐患
2.1 以距离度量分类
*kNN优点:高准确性,对异常值不敏感,对数据没有假定
*kNN缺点:计算消耗大,存储要求大
*处理:标称值(nominal value),数值
- We have labels for every data in training set.
- When we 're given a new piece of data without a label, we compare that new piece of data to the existing data, every piece of existing data.
- We then take the most similar pieces of data (the nearest neighbors) and look at their labels.
- We look at the top k most similar pieces of data from our known dataset; this is where k comes from (which is a integer and usually less than 20).
- Lastly, we take a majority vote from the k most similar pieces of data, and the majority is the new class we assign to the data we were asked to classify.
- 我们将训练集中的所有数据打上标签(知道如何分类)。
- 当我们得到一个没有打上标签数据时,我们将新数据与每一条已知数据进行比较。
- 我们得到最相似的数据,然后观察他们的标签。
- 我们从我们一直到的数据集中得到最相似的k个数据;这就是k的由来(k为一个一般小于20的整数)。
- 我们从k个最相似的数据中得到的大多数投票就是是我们分配给要求分类的新数据的新类别(class)。
例子——>电影分类
- 分类动作电影和爱情电影。
- 观察6部电影的亲吻和打架的次数。
*一部没有看过的电影,决定种类?

- 计算每一个电影到“?”的距离(距离的计算会在之后的内容中提到)。
- 将电影按照距离降序排列找到k-nearest的电影。假设k=3。得到三部电影HNRD,BW和CM。
- kNN算法认为这三部的大多数分类就是电影“?”的分类。我们知道这三部电影都是爱情片,所以我们也认为“?”是爱情片。
kNN的基本步骤
- 收集
- 准备:距离计算要求数值,数据格式的结构化要求。
- 分析
- 训练:不适用于kNN
- 测试:计算错误率
- 使用
网友评论