Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <math.h>
- #define MAX 2500
- #define MAX_CENT 20
- double eucladiana(double centroid[][MAX_CENT], int nCentroid, double objects[][MAX_CENT], int nObject, int nAtributos){
- double resp = 0.0;
- double resta = 0.0;
- int i, j;
- for(i = 0; i < nAtributos; i++){
- resta = centroid[nCentroid][i] - objects[nObject][i];
- resp += pow(resta, 2);
- }
- return sqrt(resp);
- }
- void calculaDistancias(double distancias[][MAX], double objects[][MAX_CENT], int nObjects, double centroid[][MAX_CENT], int fil, int col){
- for(int i=0; i<fil; i++)
- for(int j=0; j<nObjects; j++)
- distancias[i][j] = eucladiana(centroid, i, objects, j, col);
- }
- void clustering(int newCluster[][MAX], int nClusters, int nObjects, double distancias[][MAX]){
- bool menor = false;
- int i, j, k, indice;
- for(i=0; i<nObjects; i++){
- for(j=0; j<nClusters-1; j++){
- for(k=j+1; k<nClusters; k++){
- if (distancias[j][i] < distancias[k][i]){
- indice = j;
- menor = true;
- }
- else{
- menor = false;
- break;
- }
- }
- if (menor == true) break;
- else {
- indice = k;
- }
- }
- newCluster[indice][i] = 1;
- }
- }
- void copyCluster(int copy[][MAX],int original[][MAX],int filas,int columnas){
- for(int i=0;i<filas;i++){
- for(int j=0; j<columnas;j++){
- copy[i][j] = original[i][j];
- }
- }
- }
- void asignaNuevoCentroide(double centroid[][MAX_CENT],double objects[][MAX_CENT],int oldCluster[][MAX],int nClusters,int nObjects){
- double sumaX = 0,sumaY = 0,numDatos = 0;
- int i, j;
- for(i=0;i<nClusters;i++){
- for(j=0;j<nObjects;j++){
- if(oldCluster[i][j] == 1){
- numDatos++;
- sumaX += objects[j][0]; //X
- sumaY += objects[j][1]; //Y
- }
- }
- centroid[i][0]= sumaX/numDatos;
- centroid[i][1]= sumaY/numDatos;
- sumaX =0;sumaY=0;numDatos =0 ;
- }
- }
- int clusterDiferente(int newCluster[][MAX], int oldCluster[][MAX],int nClusters,int nObjects){
- int diferentes = 0;
- for(int i=0; i<nClusters;i++){
- for(int j=0;j<nObjects;j++)
- if(newCluster[i][j] != oldCluster[i][j])
- return 1;
- }
- return 0;
- }
- void inicializaCluster(int cluster[][MAX], int nClusters, int nObjects){
- for(int i=0; i < nClusters; i++)
- for(int j=0; j < nObjects; j++)
- cluster[i][j] = 0;
- }
- void imprimeCluster(int cluster[][MAX], int nClusters, int nObjects){
- for (int a = 0; a < nClusters; a++){
- for (int b = 0; b < nObjects; b++)
- printf("%d ", cluster[a][b]);
- printf("\n");
- }
- printf("\n");
- }
- void imprimeCentroid(double centroid[][MAX_CENT], int nClusters, int nAtributos){
- for(int i=0; i < nClusters; i++){
- for(int j=0; j < nAtributos; j++)
- printf("%.2lf ", centroid[i][j]);
- printf("\n");
- }
- printf("\n");
- }
- int main(){
- int n = 2;
- int k = 2;
- int i = 0, j = 0;
- int atributo;
- char nombreArchivo[100];
- FILE *archivo;
- scanf("%s",nombreArchivo);
- archivo = fopen(nombreArchivo , "r");
- scanf("%d",&k);
- scanf("%d",&n);
- int oldCluster[MAX_CENT][MAX] = {0,0}, newCluster[MAX_CENT][MAX] = {0,0};
- double objects[MAX][MAX_CENT] = {0,0}, centroid[MAX][MAX_CENT] = {0,0}, distancias[MAX_CENT][MAX] = {0.0,0.0};
- while(fscanf(archivo,"%d,", &atributo) != EOF){
- objects[i][j] = atributo;
- j++;
- if (j == n){
- i++;
- j=0;
- }
- }
- int nObjects = i;
- //Si el numero de centroides es mayor que el numero de objetos no se puede.
- for(int i=0; i<k; i++)
- for(int j=0; j<n; j++)
- centroid[i][j] = objects[i][j];
- calculaDistancias(distancias, objects, nObjects, centroid, k, n);
- clustering(newCluster, k, nObjects, distancias);
- while(clusterDiferente(newCluster, oldCluster, k, nObjects)){
- copyCluster(oldCluster, newCluster, k, nObjects);
- imprimeCluster(oldCluster, k, nObjects);
- asignaNuevoCentroide(centroid, objects, oldCluster, k, nObjects);
- calculaDistancias(distancias, objects, nObjects, centroid, k, n);
- inicializaCluster(newCluster, k, nObjects);
- clustering(newCluster, k, nObjects, distancias);
- imprimeCluster(newCluster, k, nObjects);
- }
- imprimeCentroid(centroid, k, n);
- int centro;
- FILE *salida;
- salida= fopen("out.csv","w");
- for(int a = 0 ; a<nObjects;a++ ){
- for(int b = 0;b<n;b++){
- fprintf(salida,"%.0lf,",objects[a][b]);
- }
- for(int c = 0 ; c<k;c++){
- if(newCluster[c][a]==1){
- centro = c;
- break;
- }
- }
- fprintf(salida,"%d\n",centro);
- }
- // Falta de Tume el Csv
- /*for (int a = 0; a < k; a++){
- for (int b = 0; b < n; b++)
- printf("%.2lf ", centroid[a][b]);
- printf("\n");
- }
- printf("\n");
- for (int a = 0; a < k; a++){
- for (int b = 0; b < nObjects; b++)
- printf("%.2lf ", distancias[a][b]);
- printf("\n");
- }
- printf("\n");
- for (int a = 0; a < k; a++){
- for (int b = 0; b < nObjects; b++)
- printf("%d ", newCluster[a][b]);
- printf("\n");
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement