五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

基于歐式距離的聚類算法的Kmeans作業(yè)

2023-05-19 14:08 作者:玟玟的大寶貝  | 我要投稿

訪問【W(wǎng)RITE-BUG數(shù)字空間】_[內(nèi)附完整源碼和文檔]

基于歐式距離的聚類算法,其認為兩個目標的距離越近,相似度越大。 該實驗產(chǎn)生的點為二維空間中的點。

環(huán)境配置

java環(huán)境,使用原生的Java UI組件JPanel和JFrame

算法原理

基于歐式距離的聚類算法,其認為兩個目標的距離越近,相似度越大。

該實驗產(chǎn)生的點為二維空間中的點。

歐式距離

n維空間中的兩個點X,Y

$dist(X, Y) = \sqrt{\sum_{i = 1}^{n} (x_{i} - y_{i})^{2}}$

算法過程

  1. 選擇k,聚類的數(shù)量。

  2. 選擇k個點作為聚類中心。

  3. 對每個樣本點計算到k個聚類中心的距離,采用的是歐氏距離,將其分類到距離最近的類別中。

  4. 根據(jù)每個類別,計算被分類在該類別中的所有點的中心。

  5. 如果計算出來的中心和聚類中心相同,則退出循環(huán),否則以新的計算出來的中心為每個聚類的聚類中心,不斷重復3 - 4步。

核心代碼

設定K

/*Step按鈕的監(jiān)聽器*/ jButton2.addActionListener(new ActionListener() { ? ?public void actionPerformed(ActionEvent ae) { ? ? ? ?painting.assign(); ? ? ? ?painting.updateCentroids(); ? ? ? ?/*算法終止的話讓按鈕變灰并提示算法結(jié)束*/ ? ? ? ?if (painting.stop(num++)) { ? ? ? ? ? ?jButton2.setText("End"); ? ? ? ? ? ?jButton2.setEnabled(false); ? ? ? ?} ? ? ? ?painting.repaint(); ? ?} });

計算歐式距離

/*歐式距離*/ double Euc(Point p1, Point p2) { ? ?double distance = 0.0; ? ?for (int i = 0; i < Dimension; ++i) ? ? ? ?distance += (p1.x[i] - p2.x[i]) * (p1.x[i] - p2.x[i]); ? ?return Math.sqrt(distance); }

更新中心點

/*更新中心點*/ void updateCentroid(int clusterNum) { ? ?//將newCluster數(shù)組的那個中心點置空 ? ?for (int i = 0; i < Dimension; ++i) ? ? ? ?newCluster[clusterNum].x[i] = 0; ? ?int clusterSize = 0; ? ?for (int i = 0; i < Nodes; ++i) ? ? ? ?if (p[i].cluster == clusterNum) { ? ? ? ? ? ?//這個簇中有多少點 ? ? ? ? ? ?clusterSize++; ? ? ? ? ? ?for (int j = 0; j < Dimension; ++j) ? ? ? ? ? ? ? ?newCluster[clusterNum].x[j] += p[i].x[j]; ? ? ? ?} ? ?if (clusterSize == 0) ? ? ? ?return; ? ?for (int i = 0; i < Dimension; ++i) ? ? ? ?newCluster[clusterNum].x[i] /= (double) clusterSize; }

計算每個點的分類

/*分配數(shù)據(jù)點到哪個簇*/ void assignPoint(int x) { ? ?double minDistance = 99999999; ? ?int nodeClassify = 1; ? ?for (int i = 0; i < K; ++i) { ? ? ? ?//計算歐式距離 ? ? ? ?double newDistance = Euc(p[x], newCluster[i]); ? ? ? ?if (newDistance < minDistance) { ? ? ? ? ? ?minDistance = newDistance; ? ? ? ? ? ?nodeClassify = i; ? ? ? ?} ? ?} ? ?p[x].cluster = nodeClassify; }

判斷終止條件

/*判斷算法是否終止*/ Boolean stop(int currentTime) { ? ?//超過迭代次數(shù) ? ?if (currentTime > range) { ? ? ? ?int num = 1; ? ? ? ?System.out.println("超過迭代次數(shù)"); ? ? ? ?for (Point i : oldCluster) { ? ? ? ? ? ?System.out.println("中心點" + num + "坐標:(" + i.x[0] + "," + i.x[1] + ")"); ? ? ? ? ? ?num++; ? ? ? ?} ? ? ? ?return true; ? ?} ? ?/*如果每一個中心點都與上一次的中心點相同,則算法終止,否則更新oldCentroid*/ ? ?for (int i = 0; i < K; ++i) ? ? ? ?if (!samePoint(oldCluster[i], newCluster[i])) { ? ? ? ? ? ?for (int j = 0; j < K; ++j) ? ? ? ? ? ? ? ?copy(oldCluster[j], newCluster[j]); ? ? ? ? ? ?return false; ? ? ? ?} ? ?int num = 1; ? ?System.out.println("迭代完成"); ? ?for (Point i : oldCluster) { ? ? ? ?System.out.println("中心點" + num + "坐標:(" + i.x[0] + "," + i.x[1] + ")"); ? ? ? ?num++; ? ?} ? ?return true; }


基于歐式距離的聚類算法的Kmeans作業(yè)的評論 (共 條)

分享到微博請遵守國家法律
颍上县| 北票市| 正宁县| 济源市| 揭阳市| 金阳县| 习水县| 富平县| 巫山县| 招远市| 平果县| 南漳县| 宝丰县| 揭阳市| 永康市| 彭泽县| 洛扎县| 溆浦县| 诏安县| 汝南县| 乃东县| 临西县| 天峻县| 湖南省| 高邑县| 成都市| 太仓市| 盖州市| 囊谦县| 密云县| 桃源县| 澳门| 满城县| 任丘市| 东兰县| 富平县| 西贡区| 东海县| 疏附县| 垣曲县| 即墨市|