Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MATLAB implementation of K-means clustering algorithm

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1. Overview of K-means clustering method

I used this clustering method in the process of mathematical modeling before, but I simply knew how to use the toolbox to cluster in matlab at that time, but I was not particularly clear about its principle. Recently, because I was learning pattern recognition, I came into contact with this clustering algorithm again, so I carefully studied its principle. After I figured it out, I realized it by hand with matlab, and the final result was good. Hey, hey, hey.

Simply put, K-means clustering is based on a given set of samples (x1, x2,... xn) (xi, I = 1,2,... After n are all vectors, suppose you want to aggregate them into m (eps | | abs (U1 (2penm)-U1 (2jm) > eps | | abs (U2 (1m)-U2 (1m)) > eps | | abs (U2 (2m)-U2 (2m) > eps)) m = mord1) % calculate the distance from all points to the two cluster centers for I = 1: ND (1Magi) = sqrt ((X (I)-U1 (1Magm)) ^ 2 + (Y (I)-U1 (2Magm)) ^ 2); endfor I = 1: ND (2Magi) = sqrt ((X (I)-U2 (1Magm)) ^ 2 + (Y (I)-U2 (2Magm)) ^ 2); endA = zeros (2N) % An is used to store the data points of the first category B = zeros (2Magin N);% B is used to store the data points of the second category for k = 1: n [MIN,index] = min (D (:, k)); if index = = 1% belongs to the first cluster center A (1jue k) = X (k); A (2jink) = Y (k) The else% points belong to the second clustering center B (1Magi k) = X (k); B (2Magi k) = Y (k); endendindexA = find (A (1grad:) ~ = 0);% find the point indexB = find (B (1Q:) ~ = 0) in the first category; and% find the point U1 (1jue mquist) = mean (A (1jre indexA)) in the second category. U1 (2menmindex1) = mean (A (2jingindexA)); U2 (1menmindex1) = mean (B (1lemindexB)); U2 (2memmquist 1) = mean (B (2menindexB));% update two cluster centers endfigure;plot (A (1m indexA), A (2m indexA),'* b');% hold onplot (B (1jue indexB), B (2jue indexB), 'oy') to make the first kind of points. Hold oncenterx = [U1 (1magm) U2 (1magm)]; centery = [U1 (2Powerm) U2 (2Powerm)]; plot (centerx, centery,'+ g');% draw two cluster centers xlabel ('X'); ylabel ('Y'); title ('data points after clustering'); disp ([the number of iterations is:', num2str (m)])

The classification results are as follows:

The 50 randomly generated points are divided into two types of iterations in only 4 steps, and from the above figure, the effect of classification is good. But the results of each run may be different because these points are randomly generated and there are no clear classification criteria.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report