search
HomeDatabaseMysql Tutorial形象理解K-Means算法

形象理解K-Means算法

Jun 07, 2016 pm 04:37 PM
k-meansTaskunderstandalgorithmteacher

前段时间老师给我的任务是让我使用MapReduces和Spark分别实现K-means算法来比较MapReduces和Spark。首先问题是K-means算法是什么? K-means算法的中心思想其实就是迭代,通过不断的迭代,使聚类效果达到局部最优,为什么我们说局部最优呢?因为K-means算法的

前段时间老师给我的任务是让我使用MapReduces和Spark分别实现K-means算法来比较MapReduces和Spark。首先问题是K-means算法是什么?

K-means算法的中心思想其实就是迭代,通过不断的迭代,使聚类效果达到局部最优,为什么我们说局部最优呢?因为K-means算法的效果的优劣性和最初选取的中心点是有莫大关系的,我们只能在初始中心点的基础上达到局部最优解。K-means算法是基于距离的聚类算法,采用距离作为相似性的评价指标,即认为两个对象的距离越近,其相似度越大。该算法认为簇是由距离靠近的对象组成的,因此把得到紧凑且独立的簇作为最终目标。我感觉总的来说就是物以类聚。

对于聚类问题,我们事先并不知道给定的一个训练数集到底有哪些类别(即没有指定类标签),而是根据需要设置指定个数类标签的数量(但不知道具体的类标签是什么),然后通过K-means算法将具有相同特征,或者基于一定规则认为某一些对象相似,与其它一些组明显的不同的数据聚集到一起,自然形成分组。之后,我们可以根据每一组的数据的特点,给定一个合适的类标签(当然,可能给出类标签对实际应用没有实际意思,例如可能我们就想看一下聚类得到的各个数据集的相似性)。

在这里我们首先说明一个概念:质心(Centroid)。质心可以认为就是一个样本点,或者可以认为是数据集中的一个数据点P,它是具有相似性的一组数据的中心,即该组中每个数据点到P的距离都比到其它质心的距离近(与其它质心相似性比较低)。

K个初始类聚类质心的选取对聚类结果具有较大的影响,因为在该算法第一步中是随机的选取任意k个对象作为初始聚类的质心,初始地代表一个聚类结果,当然这个结果一般情况不是合理的,只是随便地将数据集进行了一次随机的划分,具体进行修正这个质心还需要进行多轮的计算,来进一步步逼近我们期望的聚类结果:具有相似性的对象聚集到一个组中,它们都具有共同的一个质心。另外,因为初始质心选择的随机性,可能未必使最终的结果达到我们的期望,所以我们可以多次迭代,每次迭代都重新随机得到初始质心,直到最终的聚类结果能够满足我们的期望为止。

1. 首先输入k的值,即我们希望将数据集D = {P1, P2, …, Pn}经过聚类得到k个分类(分组)。

2. 从数据集D中随机选择k个数据点作为质心,质心集合定义为:Centroid = {Cp1, Cp2, …, Cpk},排除质心以后数据集O={O1, O2, …, Om}。

  1. 对集合O中每一个数据点Oi,计算Oi与Cpj(j=1, 2, …,k)的距离,得到一组距离Si={si1, si2, …, sik},计算Si中距离最小值,则该该数据点Oi就属于该最小距离值对应的质心。
  2. 每个数据点Oi都已经属于其中一个质心,然后根据每个质心所包含的数据点的集合,重新计算得到一个新的质心。

5. 如果新计算的质心和原来的质心之间的距离达到某一个设置的阈值(表示重新计算的质心的位置变化不大,趋于稳定,或者说收敛),可以认为我们进行的聚类已经达到期望的结果,算法终止。

6. 如果新质心和原来之心距离变化很大,需要迭代2~5步骤。

这是之前整理的一份,刚刚翻出来,现在贴出来,以便之后查看。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to analyze the execution plan of MySQL queryHow to analyze the execution plan of MySQL queryApr 29, 2025 pm 04:12 PM

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

How to use MySQL subquery to improve query efficiencyHow to use MySQL subquery to improve query efficiencyApr 29, 2025 pm 04:09 PM

Subqueries can improve the efficiency of MySQL query. 1) Subquery simplifies complex query logic, such as filtering data and calculating aggregated values. 2) MySQL optimizer may convert subqueries to JOIN operations to improve performance. 3) Using EXISTS instead of IN can avoid multiple rows returning errors. 4) Optimization strategies include avoiding related subqueries, using EXISTS, index optimization, and avoiding subquery nesting.

How to configure the character set and collation rules of MySQLHow to configure the character set and collation rules of MySQLApr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

How to uninstall MySQL and clean residual filesHow to uninstall MySQL and clean residual filesApr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

How to rename a database in MySQLHow to rename a database in MySQLApr 29, 2025 pm 04:00 PM

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),