knn 邻居数量k的选取_k个最近邻居和k表示聚类有什么区别

2023-12-22 08:30

本文主要是介绍knn 邻居数量k的选取_k个最近邻居和k表示聚类有什么区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

knn 邻居数量k的选取

Are you confused with the k-Nearest Neighbor (k-NN) and k-means clustering? Let’s try to understand the difference between k-NN and k-means in simple words with examples.

重新您与k近邻(K-NN)和K-means聚类困惑? 让我们尝试通过示例简单地理解k-NN和k-means之间的区别。

Let me introduce some major differences between them before going to the examples. Don’t worry, I won’t talk much !!

在介绍示例之前,让我介绍它们之间的一些主要区别。 别担心,我不会说太多!

  • k-NN is a supervised machine learning while k-means clustering is an unsupervised machine learning. Yes! You thought it correct, the dataset must be labeled if you want to use k-NN.

    k-NN是有监督的机器学习,而k-means聚类是无监督的机器学习。 是! 您认为正确,如果要使用k-NN,则必须标记数据集。

  • k-NN is versatile; it can be used for the classification and the regression problems as well. However, it is more widely used in classification problems in the industry. k-means is used for the clustering. But what is clustering?

    k-NN是通用的; 它也可以用于分类和回归问题。 但是,它更广泛地用于行业中的分类问题。 k均值用于聚类。 但是什么是集群?

Clustering is simply grouping the samples of the dataset in such a way that objects in the same group are more similar to each other than to those in other groups.

聚类只是将数据集的样本进行分组,以使同一组中的对象彼此之间的相似性高于其他组中的对象。

You can try to group the following into two: monkey, dog, apple, orange, tiger, and banana for better understanding! I am sure you did it right. Let’s move on.

您可以尝试将以下内容分为两类:猴子,狗,苹果,橙子,老虎和香蕉,以更好地理解! 我相信你做对了。 让我们继续前进。

  • k-NN is a lazy learning and non-parametric algorithm, i.e. it has no training phase (machine learning without training phase! Sounds weird!!) and no prediction power (again!!). It memorizes the data set (call it training if you want) and when a new test sample appears, it calculates(or selects) the label. Unlike k-NN, k-means has a model fitting and prediction power, which makes it an eager learner. In the training phase, the objective function is minimized, and the trained model predicts the label for test samples.

    k-NN是一种惰性学习和非参数算法,即它没有训练阶段(机器学习没有训练阶段!听起来很奇怪! )并且没有预测能力(再次! )。 它存储数据集(如果需要,可将其称为训练),并在出现新的测试样品时计算(或选择)标签。 与k-NN不同,k-means具有模型拟合和预测能力,这使其成为了一个渴望学习的人。 在训练阶段,目标函数被最小化,并且训练后的模型可以预测测试样本的标签。

Enough talk, let’s see the examples.

足够多的讨论,让我们看看示例。

KNN分类: (KNN for classification:)

We have a dataset of the houses in Kaiserslautern city with the floor area, distance from the city center, and whether it is costly or not (Something being costly is a subjective thing, though!). Here, we have two classes, class 1: Costly and class 2: Affordable.

我们拥有凯撒斯劳滕(Kaiserslautern)市房屋的数据集,其中包括建筑面积,距市中心的距离以及是否昂贵(尽管昂贵是主观的事情!) 。 在这里,我们有两个类,第1类:昂贵,而第2类:负担得起。

In the scatter plot below, red dots show expensive and blue dots show affordable houses. Now we want to know whether a house with a 190 m² area and 6 km away from the city center (green dot in the figure) is expensive or affordable based on the given dataset. In simple words, we want to predict the class of the new data sample.

在下面的散点图中,红点表示昂贵,蓝点表示负担得起的房屋。 现在,根据给定的数据集,我们想知道面积为190平方米,距市中心6公里(图中的绿色点)的房屋是昂贵的还是负担得起的。 简而言之,我们要预测新数据样本的类别。

Image for post
Image from Author
图片来自作者
Image for post
Image from Author
图片来自作者

If we set k=3, then k-NN will find 3 nearest data points (neighbors) as shown in the solid blue circle in the figure and labels the test point according to the majority votes. In our case, the house will be labeled as ‘Costly’.!! Pause !!Think about setting k=2!Yes, k must be an odd number to avoid an equal number of votes!

如果我们设置k = 3,则k-NN将找到3个最接近的数据点(邻居),如图中的实心蓝色圆圈所示,并根据多数选票标记测试点。 在我们的案例中,这所房子将被标记为“成本高”。 !! 暂停! 想想设置k = 2!是的, k必须是一个奇数以避免投票数相等!

Let’s set k=5 for the same test sample. Now the majority class in 5 nearest neighbors is ‘Affordable’ (dotted red circle)! You got the point, right? The answer varies according to the value of k. The best choice of k depends on the dataset.

让我们为相同的测试样本设置k = 5。 现在,在5个最近的邻居中,多数阶层是“负担得起的”(红色圆圈)! 你明白了吧? 答案根据k的值而变化。 k的最佳选择取决于数据集。

K表示聚类: (K means clustering:)

We have a similar dataset with more samples, but there is no label. It consists of only distance from the city center and the floor area of houses.

我们有一个具有更多样本的相似数据集,但没有标签。 它仅包括距市中心和房屋建筑面积的距离。

Image for post
Image from Author
图片来自作者

Let’s try to group the dataset into k=2 groups using k-means. The algorithm will initiate 2 centroids for 2 clusters. In each iteration of the training phase, centroids will move in such a way that the sum of the squared distance between data points and the cluster’s centroid is at the minimum. That’s it. The model is ready.

让我们尝试使用k均值将数据集分组为k = 2组。 该算法将为2个簇初始化2个质心。 在训练阶段的每次迭代中,质心将以这样的方式移动,即数据点和群集质心之间的平方距离之和最小。 而已。 模型已准备就绪。

Image for post
Image from Author
图片来自作者

We have to check if having two clusters in our dataset makes any sense or not. I would say the cluster in the left shows affordable houses and the cluster in the right shows expensive houses. Wait a minute !!The interpretation I made is OK, but I am not satisfied with it. Let’s set k=4 for the same dataset.

我们必须检查数据集中是否有两个聚类是否有意义。 我要说的是,左边的集群显示的是负担得起的房子,右边的集群显示的是昂贵的房子。 等一下 !! 我所作的解释还可以,但我对此并不满意。 让我们为相同的数据集设置k = 4。

Image for post
Image from Author
图片来自作者

Now we have four groups, you can call them cluster 1: ‘near & large’, cluster 2: ‘far & large’, cluster 3: ‘far & small’, cluster 4: ‘near & small’. You can try to extract some meaning from these clusters. I think houses in cluster 1 are super expensive and noisy while cluster 2 shows affordable and calm houses. cluster 3 shows cheap and calm houses and the houses in cluster 4 are affordable but noisy. You can try to cluster them into different values of k and see if it makes any logic.

现在我们有四个组,您可以将它们称为群集1:“近而大”,群集2:“远而大”,群集3:“远而小”,群集4:“近而小” 。 您可以尝试从这些群集中提取一些含义。 我认为第1组的房屋价格昂贵且嘈杂,而第2组的房屋价格合理且安静。 第3组显示便宜且平静的房屋,第4组中的房屋负担得起,但嘈杂。 您可以尝试将它们聚类为k的不同值,看看它是否构成逻辑。

The Elbow method can be used to choose the value of k. However, this method only suggests the range of the k and you have to choose the value of k according to your dataset. We have a new house with an area of 165 m² and a distance from the city center 6 km. Now model can calculate the distance of the new test point from all four centroids of four clusters and predict in which cluster the new house fits well.

肘法可用于选择k的值。 但是,此方法仅建议k的范围,您必须根据数据集选择k的值。 我们有一所新房子,面积为165平方米,距市中心6公里。 现在,模型可以从四个群集的所有四个质心计算新测试点的距离,并预测新房子适合哪个群集。

回归的KNN: (KNN for regression:)

Consider the same dataset we discussed in the k-NN for classification. Instead of classifying the new data sample, we want to calculate the price of the house.

Çonsider相同的数据集,我们在第k-NN分类讨论。 我们不想对新的数据样本进行分类,而是要计算房屋的价格。

Image for post
Image from Author
图片来自作者

You are right! Labels of the main dataset must be the price of the house instead of classes.

你是对的! 主数据集的标签必须是房屋的价格,而不是类别的价格。

Image for post
Image from Author
图片来自作者

To calculate the price of the test sample, the algorithm will find k number of nearest neighbors the same as in classification but instead of voting for the majority, it will find the weighted mean of the prices of neighbors according to their distances from the test sample.

为了计算测试样本的价格,该算法将找到k个最接近的邻居,其数量与分类中的相同,但无需投票赞成多数,而是根据邻居与测试样本的距离找到加权价格的平均值。

In the figure, the black dot shows the test sample and the other three dots in the blue circle are nearest neighbors. The predicted price of the test sample will be the weighted mean of the prices of three dots in the blue circle.

在图中,黑点表示测试样品,蓝色圆圈中的其他三个点是最近的邻居。 测试样本的预测价格将是蓝色圆圈中三个点的价格的加权平均值。

Remember, the meaning of the k in k-NN and k-means is totally different.

请记住,k在k-NN和k-means中的含义完全不同。

All in all, k-NN chooses k nearest neighbors to vote for majority in classification problems and calculates weighted mean of labels in regression problems. While k-Means groups the dataset into k number of groups.

总而言之,在分类问题中,k-NN选择k个最近的邻居对多数票进行投票,并在回归问题中计算标签的加权平均值。 虽然k均值将数据集分为k个组。

翻译自: https://medium.com/swlh/what-is-the-difference-between-k-nearest-neighbors-and-k-means-clustering-21343f00b35d

knn 邻居数量k的选取


http://www.taodudu.cc/news/show-8397982.html

相关文章:

  • 4.6 无监督学习—K-means算法
  • oj-宠物屋涂色-java
  • 机器学习之利用k-means算法对点云数据进行目标分割,提取其中的建筑物、房屋等
  • 剑指offer编程题(JAVA实现)——第29题:最小的K个数
  • 数据分析与机器学习实战(二)——聚类分析(以K-means聚类为例)
  • 机器学习系列-K-Means算法
  • k歌php源码,[宜配屋]听图阁
  • 维修类APP开发需要具备哪些常用功能呢?
  • 水电维修小程序开发具备哪些优势?
  • 上门维修APP软件开发|上门维修预约小程序开发
  • 计算机毕设选题Android安卓项目高校后勤报修app线上维修app (源码+讲解+文档报告+ppt)
  • 维修服务商城APP开发解决方案
  • 上门维修APP开发核心功能分析
  • 基于正玄变换的人脸姿态矫正(由正面人脸图像模拟出侧面人脸图像)
  • python 人脸检测_50行Python代码实现人脸检测功能
  • 基于TP-GAN的侧脸人像恢复
  • Opencv(C++)版,人脸检测
  • ClickHouse替换MySQL作为数仓APP层
  • 牛x!一款比传统数据库查询快近 200 倍的数据库,来看一看?
  • 牛!一个比传统数据库快 100-1000 倍的数据库
  • clickhouse 增量更新_每天十亿级数据更新,秒出查询结果,ClickHouse在携程酒店的应用...
  • clickhouse 增量更新_携程用ClickHouse轻松玩转每天十亿级数据更新
  • 业务现状描述
  • 测试人员定位bug的方法
  • 跟着团子学SAP EPPM:项目管理常见的集成场景(PPM\PS\MS Project)
  • 大数据业务模型和技术架构简图
  • 优秀的软件测试人员,都具备这些能力
  • 测试人员如何快速熟悉业务
  • 现在用高效洗钢板清消泡剂的人,都不知道它的真正功效在哪里
  • 循环污水处理用消泡剂有这几种添加方法,哪种是你没用过的
  • 这篇关于knn 邻居数量k的选取_k个最近邻居和k表示聚类有什么区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/523266

    相关文章

    Before和BeforeClass的区别及说明

    《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor

    Android学习总结之Java和kotlin区别超详细分析

    《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

    Linux中的more 和 less区别对比分析

    《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

    Java 关键字transient与注解@Transient的区别用途解析

    《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

    解读@ConfigurationProperties和@value的区别

    《解读@ConfigurationProperties和@value的区别》:本文主要介绍@ConfigurationProperties和@value的区别及说明,具有很好的参考价值,希望对大家... 目录1. 功能对比2. 使用场景对比@ConfigurationProperties@Value3. 核

    Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

    《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

    MySQL精准控制Binlog日志数量的三种方案

    《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

    关于Mybatis和JDBC的使用及区别

    《关于Mybatis和JDBC的使用及区别》:本文主要介绍关于Mybatis和JDBC的使用及区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、JDBC1.1、流程1.2、优缺点2、MyBATis2.1、执行流程2.2、使用2.3、实现方式1、XML配置文件

    exfat和ntfs哪个好? U盘格式化选择NTFS与exFAT的详细区别对比

    《exfat和ntfs哪个好?U盘格式化选择NTFS与exFAT的详细区别对比》exFAT和NTFS是两种常见的文件系统,它们各自具有独特的优势和适用场景,以下是关于exFAT和NTFS的详细对比... 无论你是刚入手了内置 SSD 还是便携式移动硬盘或 U 盘,都需要先将它格式化成电脑或设备能够识别的「文

    什么是ReFS 文件系统? ntfs和refs的优缺点区别介绍

    《什么是ReFS文件系统?ntfs和refs的优缺点区别介绍》最近有用户在Win11Insider的安装界面中发现,可以使用ReFS来格式化硬盘,这是不是意味着,ReFS有望在未来成为W... 数十年以来,Windows 系统一直将 NTFS 作为「内置硬盘」的默认文件系统。不过近些年来,微软还在研发一款名