Last active
December 16, 2015 01:09
-
-
Save mfrazi/51553ad271d915069eb6 to your computer and use it in GitHub Desktop.
V 0.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
const int data_total=6; | |
const int max_data=3; | |
/*double data_training[data_total][max_data]={ | |
{1,1,0,0}, | |
{0,0,0,1}, | |
{1,0,0,0}, | |
{0,0,1,1} | |
//{2,3,1,0}, | |
//{1,9,0,1}, | |
//{0,1,2,1}, | |
//{9,1,1,5} | |
};*/ | |
double data_training[data_total][max_data]={ | |
{1.1,1.7,1.8}, | |
{0,0,0}, | |
{0,0.5,1.5}, | |
{1,0,0}, | |
{0.5,0.5,0.5}, | |
{1,1,1} | |
}; | |
const int cluster=2; | |
double centroid[cluster][max_data]; | |
double euclidean_distance(int a, int b); | |
int main(){ | |
int initial_centroid[cluster]; | |
int cluster_group[data_total]; | |
for(int i=0; i<cluster; i++) | |
scanf("%d", &initial_centroid[i]); | |
for(int i=0; i<cluster; i++) | |
for(int j=0; j<max_data; j++) | |
centroid[i][j]=data_training[initial_centroid[i]-1][j]; | |
// for(int i=0; i<cluster; i++){ | |
// for(int j=0; j<max_data; j++) | |
// printf("%lf ", centroid[i][j]); | |
// printf("\n"); | |
// } | |
for(int i=0; i<data_total; i++) | |
cluster_group[i]=0; | |
bool flag=true; | |
//int xx=1; | |
while(flag){ | |
// Save centroid for each cluster | |
double tmp_centroid[cluster][max_data]; | |
for(int i=0; i<cluster; i++) | |
for(int j=0; j<max_data; j++) | |
tmp_centroid[i][j] = centroid[i][j]; | |
// Assigning all point to the closest centroid | |
for(int i=0; i<data_total; i++){ | |
int tmp=0; | |
double tmp2=9999999; | |
for(int j=0; j<cluster; j++){ | |
double tmp3=euclidean_distance(i, j); | |
// printf("tmp3 = %lf\n", tmp3); | |
if(tmp2 > tmp3){ | |
tmp2 = tmp3; | |
tmp = j+1; | |
} | |
} | |
cluster_group[i] = tmp; | |
// printf("tmp = %d\n", tmp); | |
} | |
// Recompute the centroid of each cluster | |
for(int i=0; i<cluster; i++){ | |
for(int j=0; j<max_data; j++){ | |
int cnt = 0; | |
double tmp = 0; | |
for(int k=0; k<data_total; k++){ | |
if(cluster_group[k]-1==i){ | |
tmp += data_training[k][j]; | |
cnt ++; | |
} | |
} | |
centroid[i][j] = tmp/cnt; | |
// printf("%lf %d\n", tmp, cnt); | |
} | |
} | |
// Check centroid | |
bool flag_for = true; | |
int i, j; | |
for(i=0; i<cluster; i++){ | |
for(j=0; j<max_data; j++) | |
if(tmp_centroid[i][j] != centroid[i][j]){ | |
flag_for = false; | |
break; | |
} | |
if(!flag_for) | |
break; | |
} | |
if(i==cluster && j==max_data) | |
flag=false; | |
} | |
for(int i=0; i<data_total; i++) | |
printf("Point %3d => Group %d\n", i, cluster_group[i]); | |
for(int i=0; i<cluster; i++){ | |
printf("Centroid ke-%d :", i+1); | |
for(int j=0; j<max_data; j++) | |
printf("%lf ", centroid[i][j]); | |
printf("\n"); | |
} | |
return 0; | |
} | |
double euclidean_distance(int a, int b){ | |
double total=0; | |
for(int i=0; i<max_data; i++) | |
total += pow((data_training[a][i]-centroid[b][i]), 2); | |
return sqrt(total); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment