91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 4KB
    文件類型: .c
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2021-05-26
  • 語言: C/C++
  • 標簽: kmeans,c??

資源簡介

K-means聚類算法c語言實現(xiàn)。代碼正確,有詳細注釋,歡迎下載參考!

資源截圖

代碼片段和文件信息

#include???
#include???
#include???
#include???
#include???
??
#define?N?11??
#define?K?3??
??
typedef?struct??
{??
????float?x;??
????float?y;??
}Point;??

int?belongCluster[N];??///??判斷每個點屬于哪個簇??
??
Point?point[N]?=?{??
????{2.0?10.0}??
????{2.0?5.0}??
????{8.0?4.0}??
????{5.0?8.0}??
????{7.0?5.0}??
????{6.0?4.0}??
????{1.0?2.0}??
????{4.0?9.0}??
????{7.0?3.0}??
????{1.0?3.0}??
????{3.0?9.0}??
};??
??
Point?clusterCenter[K];??///??保存每個簇的中心點??
??
float?getDistance(Point?point1?Point?point2)??
{??
????float?d;??
????d?=?sqrt((point1.x?-?point2.x)?*?(point1.x?-?point2.x)?+?(point1.y?-?point2.y)?*?(point1.y?-?point2.y));??
????return?d;??
}??
??
///?計算每個簇的中心點??
void?getCenter(int?belongCluster[N])??
{??
????Point?tep;??
????int?i?j?count?=?0;??
????for(i?=?0;?i?????{??
????????count?=?0;??
????????tep.x?=?0.0;???///?每算出一個簇的中心點值后清0??
????????tep.y?=?0.0;??
????????for(j?=?0;?j?????????{??
????????????if(i?==?belongCluster[j])??
????????????{??
????????????????count++;??
????????????????tep.x?+=?point[j].x;??
????????????????tep.y?+=?point[j].y;??
????????????}??
????????}??
????????tep.x?/=?count;??
????????tep.y?/=?count;??
????????clusterCenter[i]?=?tep;??
????}??
????for(i?=?0;?i?????{??
????????printf(“The?new?centerCluster?point?of?%d?is?:?\t(?%f?%f?)\n“?i+1?clusterCenter[i].x?clusterCenter[i].y);??
????}??
}??
??
///?計算平方誤差函數(shù),在每個簇中每個點與中心點的距離平方和??
float?getE()??
{??
????int?i?j;??
????float?cnt?=?0.0?sum?=?0.0;??
????for(i?=?0;?i?????{??
????????for(j?=?0;?j?????????{??
????????????if(i?==?belongCluster[j])??
????????????{??
????????????????cnt?=?(point[j].x?-?clusterCenter[i].x)?*?(point[j].x?-?clusterCenter[i].x)?+?(point[j].y?-?clusterCenter[i].y)?*?(point[j].y?-?clusterCenter[i].y);??
????????????????sum?+=?cnt;??
????????????}??
????????}??
????}??
????return?sum;??
}??
??
///?把N個點聚類,標出每個點屬于哪個中心??
void?cluster()??
{??
????int?i?j?q;??
????float?min;??
????float?distance[N][K];??
????for(i?=?0;?i?

評論

共有 條評論

相關(guān)資源