rakha asyrofi
/
KNN_coba
machine learning mbed
funcionesKNN.cpp@0:b78c322e81d0, 2018-04-14 (annotated)
- Committer:
- asyrofi
- Date:
- Sat Apr 14 04:02:05 2018 +0000
- Revision:
- 0:b78c322e81d0
semoga bisa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asyrofi | 0:b78c322e81d0 | 1 | //#include <HardwareSerial.h> // solo para Debug |
asyrofi | 0:b78c322e81d0 | 2 | |
asyrofi | 0:b78c322e81d0 | 3 | /*------------------------------------------ |
asyrofi | 0:b78c322e81d0 | 4 | funciones para el computo del algoritmo KNN |
asyrofi | 0:b78c322e81d0 | 5 | --------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 6 | //#include "dataset.h" |
asyrofi | 0:b78c322e81d0 | 7 | #include "funcionesKNN.h" |
asyrofi | 0:b78c322e81d0 | 8 | #include <math.h> |
asyrofi | 0:b78c322e81d0 | 9 | |
asyrofi | 0:b78c322e81d0 | 10 | /*---------------------------------------------- |
asyrofi | 0:b78c322e81d0 | 11 | calculo de distancia euclidiana entre dos puntos |
asyrofi | 0:b78c322e81d0 | 12 | ------------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 13 | double distanciaEuclidiana(double pt1[], double pt2[], int co){ |
asyrofi | 0:b78c322e81d0 | 14 | int i; |
asyrofi | 0:b78c322e81d0 | 15 | double suma = 0; |
asyrofi | 0:b78c322e81d0 | 16 | for(i=0; i<co; i++){ |
asyrofi | 0:b78c322e81d0 | 17 | suma = pow(pt1[i] - pt2[i], 2) + suma; |
asyrofi | 0:b78c322e81d0 | 18 | } |
asyrofi | 0:b78c322e81d0 | 19 | return sqrt(suma); |
asyrofi | 0:b78c322e81d0 | 20 | } |
asyrofi | 0:b78c322e81d0 | 21 | |
asyrofi | 0:b78c322e81d0 | 22 | |
asyrofi | 0:b78c322e81d0 | 23 | /*--------------------------------------------------------------- |
asyrofi | 0:b78c322e81d0 | 24 | calculo de distancia euclidiana entre un punto y la base de datos |
asyrofi | 0:b78c322e81d0 | 25 | ----------------------------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 26 | void todasDistEuclid(double pt1[], double aux[], double** atributos, int fi, int co){ |
asyrofi | 0:b78c322e81d0 | 27 | int i = 0, j = 0; |
asyrofi | 0:b78c322e81d0 | 28 | double pt2[co]; |
asyrofi | 0:b78c322e81d0 | 29 | |
asyrofi | 0:b78c322e81d0 | 30 | // falta esto |
asyrofi | 0:b78c322e81d0 | 31 | for(i = 0; i<fi; i++){ |
asyrofi | 0:b78c322e81d0 | 32 | // extraer un punto de la base de datos |
asyrofi | 0:b78c322e81d0 | 33 | for(j=0; j<co; j++){ |
asyrofi | 0:b78c322e81d0 | 34 | pt2[j] = atributos[i][j]; |
asyrofi | 0:b78c322e81d0 | 35 | } |
asyrofi | 0:b78c322e81d0 | 36 | |
asyrofi | 0:b78c322e81d0 | 37 | aux[i] = distanciaEuclidiana(pt1, pt2, co); |
asyrofi | 0:b78c322e81d0 | 38 | } |
asyrofi | 0:b78c322e81d0 | 39 | } |
asyrofi | 0:b78c322e81d0 | 40 | |
asyrofi | 0:b78c322e81d0 | 41 | /*---------------------------------------------------------------- |
asyrofi | 0:b78c322e81d0 | 42 | Ordenamiento de las distancias de menor a mayor |
asyrofi | 0:b78c322e81d0 | 43 | ----------------------------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 44 | void ordena(double datos[], int clases[], int clasesNo[], int fi){ |
asyrofi | 0:b78c322e81d0 | 45 | int i =1, j = 1, f = 1, temp[2]; |
asyrofi | 0:b78c322e81d0 | 46 | |
asyrofi | 0:b78c322e81d0 | 47 | //crear una copia de las clases originales |
asyrofi | 0:b78c322e81d0 | 48 | for(i=0; i<fi; i++){ |
asyrofi | 0:b78c322e81d0 | 49 | clases[i] = clasesNo[i]; |
asyrofi | 0:b78c322e81d0 | 50 | } |
asyrofi | 0:b78c322e81d0 | 51 | |
asyrofi | 0:b78c322e81d0 | 52 | // ordenar |
asyrofi | 0:b78c322e81d0 | 53 | for(i=1; (i<=fi)&&f; i++){ |
asyrofi | 0:b78c322e81d0 | 54 | f = 0; |
asyrofi | 0:b78c322e81d0 | 55 | for(j=0; j<(fi-1); j++){ |
asyrofi | 0:b78c322e81d0 | 56 | if(datos[j+1] < datos[j]){ // orden descendente >, ascendente < |
asyrofi | 0:b78c322e81d0 | 57 | temp[0] = datos[j]; temp[1] = clases[j]; |
asyrofi | 0:b78c322e81d0 | 58 | datos[j] = datos[j+1]; clases[j] = clases[j+1]; |
asyrofi | 0:b78c322e81d0 | 59 | datos[j+1] = temp[0]; clases[j+1] = temp[1]; |
asyrofi | 0:b78c322e81d0 | 60 | f = 1; |
asyrofi | 0:b78c322e81d0 | 61 | } |
asyrofi | 0:b78c322e81d0 | 62 | } |
asyrofi | 0:b78c322e81d0 | 63 | } |
asyrofi | 0:b78c322e81d0 | 64 | } |
asyrofi | 0:b78c322e81d0 | 65 | |
asyrofi | 0:b78c322e81d0 | 66 | /*---------------------------------------------------------------- |
asyrofi | 0:b78c322e81d0 | 67 | Extraer los N primeros |
asyrofi | 0:b78c322e81d0 | 68 | ----------------------------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 69 | void extraeKPrimeros(double datos[], double kPrimeros[], int clases[], |
asyrofi | 0:b78c322e81d0 | 70 | int kClases[], int k){ |
asyrofi | 0:b78c322e81d0 | 71 | |
asyrofi | 0:b78c322e81d0 | 72 | for(int i=0; i<k; i++){ |
asyrofi | 0:b78c322e81d0 | 73 | kPrimeros[i] = datos[i]; |
asyrofi | 0:b78c322e81d0 | 74 | kClases[i] = clases[i]; |
asyrofi | 0:b78c322e81d0 | 75 | } |
asyrofi | 0:b78c322e81d0 | 76 | } |
asyrofi | 0:b78c322e81d0 | 77 | |
asyrofi | 0:b78c322e81d0 | 78 | /*---------------------------------------------------------------- |
asyrofi | 0:b78c322e81d0 | 79 | Calcula la clase mas frecuente: Moda |
asyrofi | 0:b78c322e81d0 | 80 | ----------------------------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 81 | int claseMasFrecuente(int clases[], int k){ |
asyrofi | 0:b78c322e81d0 | 82 | |
asyrofi | 0:b78c322e81d0 | 83 | int cont = 0, cont2 = 0, pos = 0, num = 0, i = 0; |
asyrofi | 0:b78c322e81d0 | 84 | int frec[k], mayor = 0, posmayor = 0, aux[k]; |
asyrofi | 0:b78c322e81d0 | 85 | |
asyrofi | 0:b78c322e81d0 | 86 | // inicializar el contador de frecuencias |
asyrofi | 0:b78c322e81d0 | 87 | for(i=0; i<k; i++){ |
asyrofi | 0:b78c322e81d0 | 88 | frec[k] = 0; |
asyrofi | 0:b78c322e81d0 | 89 | } |
asyrofi | 0:b78c322e81d0 | 90 | |
asyrofi | 0:b78c322e81d0 | 91 | // comprobar las repeticiones de cada numero |
asyrofi | 0:b78c322e81d0 | 92 | for(cont=0; cont<k; cont++){ |
asyrofi | 0:b78c322e81d0 | 93 | num = clases[cont]; |
asyrofi | 0:b78c322e81d0 | 94 | pos = cont; |
asyrofi | 0:b78c322e81d0 | 95 | |
asyrofi | 0:b78c322e81d0 | 96 | for(cont2 = 0; cont2<k; cont2++){ |
asyrofi | 0:b78c322e81d0 | 97 | if(clases[cont2] == num){ |
asyrofi | 0:b78c322e81d0 | 98 | aux[pos]++; |
asyrofi | 0:b78c322e81d0 | 99 | } |
asyrofi | 0:b78c322e81d0 | 100 | } |
asyrofi | 0:b78c322e81d0 | 101 | } |
asyrofi | 0:b78c322e81d0 | 102 | |
asyrofi | 0:b78c322e81d0 | 103 | mayor =aux[0]; |
asyrofi | 0:b78c322e81d0 | 104 | posmayor = 0; |
asyrofi | 0:b78c322e81d0 | 105 | |
asyrofi | 0:b78c322e81d0 | 106 | for(cont=0; cont<k; cont++){ |
asyrofi | 0:b78c322e81d0 | 107 | if(aux[cont] > mayor){ |
asyrofi | 0:b78c322e81d0 | 108 | posmayor = cont; |
asyrofi | 0:b78c322e81d0 | 109 | mayor = aux[cont]; |
asyrofi | 0:b78c322e81d0 | 110 | } |
asyrofi | 0:b78c322e81d0 | 111 | } |
asyrofi | 0:b78c322e81d0 | 112 | |
asyrofi | 0:b78c322e81d0 | 113 | return clases[posmayor]; |
asyrofi | 0:b78c322e81d0 | 114 | } |
asyrofi | 0:b78c322e81d0 | 115 | |
asyrofi | 0:b78c322e81d0 | 116 | |
asyrofi | 0:b78c322e81d0 | 117 | /*---------------------------------------------------------------- |
asyrofi | 0:b78c322e81d0 | 118 | Calcula la clase de un conjunto de atributos usando KNN |
asyrofi | 0:b78c322e81d0 | 119 | ----------------------------------------------------------------*/ |
asyrofi | 0:b78c322e81d0 | 120 | int clasificaKNN(double** datos, int clasesNum[], double dato[], int k, int fi, int co){ |
asyrofi | 0:b78c322e81d0 | 121 | double distancias[fi], kPrimeros[k]; |
asyrofi | 0:b78c322e81d0 | 122 | int clases[fi]; |
asyrofi | 0:b78c322e81d0 | 123 | int kClases[k]; |
asyrofi | 0:b78c322e81d0 | 124 | |
asyrofi | 0:b78c322e81d0 | 125 | todasDistEuclid(dato, distancias, datos, fi, co); |
asyrofi | 0:b78c322e81d0 | 126 | ordena(distancias, clases, clasesNum, fi); |
asyrofi | 0:b78c322e81d0 | 127 | extraeKPrimeros(distancias, kPrimeros, clases, kClases, k); |
asyrofi | 0:b78c322e81d0 | 128 | |
asyrofi | 0:b78c322e81d0 | 129 | // calculo de la moda |
asyrofi | 0:b78c322e81d0 | 130 | int cont = 0, cont2 = 0, pos = 0, num = 0, i = 0; |
asyrofi | 0:b78c322e81d0 | 131 | int frec[k], mayor = 0, posmayor = 0, aux[k]; |
asyrofi | 0:b78c322e81d0 | 132 | |
asyrofi | 0:b78c322e81d0 | 133 | // inicializar el contador de frecuencias |
asyrofi | 0:b78c322e81d0 | 134 | for(i=0; i<k; i++){ |
asyrofi | 0:b78c322e81d0 | 135 | frec[k] = 0; |
asyrofi | 0:b78c322e81d0 | 136 | } |
asyrofi | 0:b78c322e81d0 | 137 | |
asyrofi | 0:b78c322e81d0 | 138 | // comprobar las repeticiones de cada numero |
asyrofi | 0:b78c322e81d0 | 139 | for(cont=0; cont<k; cont++){ |
asyrofi | 0:b78c322e81d0 | 140 | num = clases[cont]; |
asyrofi | 0:b78c322e81d0 | 141 | pos = cont; |
asyrofi | 0:b78c322e81d0 | 142 | |
asyrofi | 0:b78c322e81d0 | 143 | for(cont2 = 0; cont2<k; cont2++){ |
asyrofi | 0:b78c322e81d0 | 144 | if(clases[cont2] == num){ |
asyrofi | 0:b78c322e81d0 | 145 | aux[pos]++; |
asyrofi | 0:b78c322e81d0 | 146 | } |
asyrofi | 0:b78c322e81d0 | 147 | } |
asyrofi | 0:b78c322e81d0 | 148 | } |
asyrofi | 0:b78c322e81d0 | 149 | |
asyrofi | 0:b78c322e81d0 | 150 | mayor =aux[0]; |
asyrofi | 0:b78c322e81d0 | 151 | posmayor = 0; |
asyrofi | 0:b78c322e81d0 | 152 | |
asyrofi | 0:b78c322e81d0 | 153 | for(cont=0; cont<k; cont++){ |
asyrofi | 0:b78c322e81d0 | 154 | if(aux[cont] > mayor){ |
asyrofi | 0:b78c322e81d0 | 155 | posmayor = cont; |
asyrofi | 0:b78c322e81d0 | 156 | mayor = aux[cont]; |
asyrofi | 0:b78c322e81d0 | 157 | } |
asyrofi | 0:b78c322e81d0 | 158 | } |
asyrofi | 0:b78c322e81d0 | 159 | |
asyrofi | 0:b78c322e81d0 | 160 | return clases[posmayor]; |
asyrofi | 0:b78c322e81d0 | 161 | } |
asyrofi | 0:b78c322e81d0 | 162 |