rakha asyrofi
/
KNN_coba
machine learning mbed
Revision 0:b78c322e81d0, committed 2018-04-14
- Comitter:
- asyrofi
- Date:
- Sat Apr 14 04:02:05 2018 +0000
- Commit message:
- semoga bisa
Changed in this revision
diff -r 000000000000 -r b78c322e81d0 dataset.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dataset.h Sat Apr 14 04:02:05 2018 +0000 @@ -0,0 +1,220 @@ +// tamaño de la base de datos +// Size of the database to account for the accuracy test + +#define FI 100 +#define CO 4 + +// clases de la base de datos en alfanumerico +// Classes alphanumeric database +char* clasesSt[] = {"Setosa", "Versicolor", "Virginica"}; + +// atributos: conjunto de entrenamiento +// Attributes: training set +char* nomAtr[CO]={"Largo sepalo", "Ancho sepalo", "Largo petalo", "Ancho petalo"}; + +double atributos[FI][CO] = { + {4.4, 3, 1.3, 0.2}, + {7.6, 3, 6.6, 2.1}, + {6.5, 2.8, 4.6, 1.5}, + {5.6, 2.8, 4.9, 2}, + {7.2, 3, 5.8, 1.6}, + {5.5, 2.3, 4, 1.3}, + {5, 2, 3.5, 1}, + {5.5, 2.4, 3.8, 1.1}, + {5.4, 3.9, 1.7, 0.4}, + {5.5, 4.2, 1.4, 0.2}, + {5.8, 2.8, 5.1, 2.4}, + {4.6, 3.6, 1, 0.2}, + {4.8, 3.1, 1.6, 0.2}, + {6.9, 3.1, 4.9, 1.5}, + {5.8, 2.7, 5.1, 1.9}, + {5.7, 3.8, 1.7, 0.3}, + {6.4, 2.8, 5.6, 2.2}, + {6.1, 2.6, 5.6, 1.4}, + {6, 3.4, 4.5, 1.6}, + {6.7, 3.1, 4.7, 1.5}, + {5.1, 3.5, 1.4, 0.2}, + {6.3, 2.8, 5.1, 1.5}, + {6.3, 2.9, 5.6, 1.8}, + {6.7, 3.3, 5.7, 2.1}, + {4.9, 3, 1.4, 0.2}, + {6.9, 3.2, 5.7, 2.3}, + {5, 3.5, 1.6, 0.6}, + {5.1, 2.5, 3, 1.1}, + {4.8, 3, 1.4,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.2, 6, 1.8}, + {5.1, 3.8, 1.6, 0.2}, + {7.1, 3, 5.9, 2.1}, + {6, 2.9, 4.5, 1.5}, + {6.4, 3.2, 4.5, 1.5}, + {4.7, 3.2, 1.6, 0.2}, + {6.4, 2.7, 5.3, 1.9}, + {6.8, 3.2, 5.9, 2.3}, + {5, 3.5, 1.3, 0.3}, + {6.9, 3.1, 5.1, 2.3}, + {5.5, 2.5, 4, 1.3}, + {5.7, 4.4, 1.5, 0.4}, + {4.6, 3.1, 1.5, 0.2}, + {4.9, 3.1, 1.5, 0.1}, + {5.6, 3, 4.5, 1.5}, + {5.6, 3, 4.1, 1.3}, + {4.7, 3.2, 1.3, 0.2}, + {5, 3.2, 1.2, 0.2}, + {7.7, 2.8, 6.7, 2}, + {5.7, 2.8, 4.5, 1.3}, + {5.4, 3.4, 1.5, 0.4}, + {4.6, 3.2, 1.4, 0.2}}; + +int clasesNo[FI] = { + 0, + 2, + 1, + 2, + 2, + 1, + 1, + 1, + 0, + 0, + 2, + 0, + 0, + 1, + 2, + 0, + 2, + 2, + 1, + 1, + 0, + 2, + 2, + 2, + 0, + 2, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 2, + 2, + 1, + 1, + 2, + 0, + 0, + 0, + 1, + 0, + 1, + 2, + 1, + 2, + 1, + 1, + 2, + 1, + 1, + 2, + 1, + 0, + 0, + 2, + 1, + 2, + 2, + 2, + 0, + 2, + 2, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 2, + 0, + 0, + 1, + 2, + 0, + 2, + 0, + 2, + 1, + 1, + 0, + 2, + 2, + 0, + 2, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 2, + 1, + 0, + 0}; + + + + +
diff -r 000000000000 -r b78c322e81d0 funcionesKNN.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/funcionesKNN.cpp Sat Apr 14 04:02:05 2018 +0000 @@ -0,0 +1,162 @@ +//#include <HardwareSerial.h> // solo para Debug + +/*------------------------------------------ +funciones para el computo del algoritmo KNN +--------------------------------------------*/ +//#include "dataset.h" +#include "funcionesKNN.h" +#include <math.h> + +/*---------------------------------------------- +calculo de distancia euclidiana entre dos puntos +------------------------------------------------*/ +double distanciaEuclidiana(double pt1[], double pt2[], int co){ + int i; + double suma = 0; + for(i=0; i<co; i++){ + suma = pow(pt1[i] - pt2[i], 2) + suma; + } + return sqrt(suma); +} + + +/*--------------------------------------------------------------- +calculo de distancia euclidiana entre un punto y la base de datos +----------------------------------------------------------------*/ +void todasDistEuclid(double pt1[], double aux[], double** atributos, int fi, int co){ + int i = 0, j = 0; + double pt2[co]; + + // falta esto + for(i = 0; i<fi; i++){ + // extraer un punto de la base de datos + for(j=0; j<co; j++){ + pt2[j] = atributos[i][j]; + } + + aux[i] = distanciaEuclidiana(pt1, pt2, co); + } +} + +/*---------------------------------------------------------------- +Ordenamiento de las distancias de menor a mayor +----------------------------------------------------------------*/ +void ordena(double datos[], int clases[], int clasesNo[], int fi){ + int i =1, j = 1, f = 1, temp[2]; + + //crear una copia de las clases originales + for(i=0; i<fi; i++){ + clases[i] = clasesNo[i]; + } + + // ordenar + for(i=1; (i<=fi)&&f; i++){ + f = 0; + for(j=0; j<(fi-1); j++){ + if(datos[j+1] < datos[j]){ // orden descendente >, ascendente < + temp[0] = datos[j]; temp[1] = clases[j]; + datos[j] = datos[j+1]; clases[j] = clases[j+1]; + datos[j+1] = temp[0]; clases[j+1] = temp[1]; + f = 1; + } + } + } +} + +/*---------------------------------------------------------------- +Extraer los N primeros +----------------------------------------------------------------*/ +void extraeKPrimeros(double datos[], double kPrimeros[], int clases[], + int kClases[], int k){ + + for(int i=0; i<k; i++){ + kPrimeros[i] = datos[i]; + kClases[i] = clases[i]; + } +} + +/*---------------------------------------------------------------- +Calcula la clase mas frecuente: Moda +----------------------------------------------------------------*/ +int claseMasFrecuente(int clases[], int k){ + + int cont = 0, cont2 = 0, pos = 0, num = 0, i = 0; + int frec[k], mayor = 0, posmayor = 0, aux[k]; + + // inicializar el contador de frecuencias + for(i=0; i<k; i++){ + frec[k] = 0; + } + + // comprobar las repeticiones de cada numero + for(cont=0; cont<k; cont++){ + num = clases[cont]; + pos = cont; + + for(cont2 = 0; cont2<k; cont2++){ + if(clases[cont2] == num){ + aux[pos]++; + } + } + } + + mayor =aux[0]; + posmayor = 0; + + for(cont=0; cont<k; cont++){ + if(aux[cont] > mayor){ + posmayor = cont; + mayor = aux[cont]; + } + } + + return clases[posmayor]; +} + + +/*---------------------------------------------------------------- +Calcula la clase de un conjunto de atributos usando KNN +----------------------------------------------------------------*/ +int clasificaKNN(double** datos, int clasesNum[], double dato[], int k, int fi, int co){ + double distancias[fi], kPrimeros[k]; + int clases[fi]; + int kClases[k]; + + todasDistEuclid(dato, distancias, datos, fi, co); + ordena(distancias, clases, clasesNum, fi); + extraeKPrimeros(distancias, kPrimeros, clases, kClases, k); + + // calculo de la moda + int cont = 0, cont2 = 0, pos = 0, num = 0, i = 0; + int frec[k], mayor = 0, posmayor = 0, aux[k]; + + // inicializar el contador de frecuencias + for(i=0; i<k; i++){ + frec[k] = 0; + } + + // comprobar las repeticiones de cada numero + for(cont=0; cont<k; cont++){ + num = clases[cont]; + pos = cont; + + for(cont2 = 0; cont2<k; cont2++){ + if(clases[cont2] == num){ + aux[pos]++; + } + } + } + + mayor =aux[0]; + posmayor = 0; + + for(cont=0; cont<k; cont++){ + if(aux[cont] > mayor){ + posmayor = cont; + mayor = aux[cont]; + } + } + + return clases[posmayor]; +} +
diff -r 000000000000 -r b78c322e81d0 funcionesKNN.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/funcionesKNN.h Sat Apr 14 04:02:05 2018 +0000 @@ -0,0 +1,43 @@ +/*---------------------------------------------------------------- +funciones para el computo del algoritmo KNN +----------------------------------------------------------------*/ +#define FI 100 +#define CO 4 + +/*---------------------------------------------------------------- +calculo de distancia euclidiana entre dos puntos +-----------------------------------------------------------------*/ +double distanciaEuclidiana(double pt1[], double pt2[], int co); + +/*---------------------------------------------------------------- +calculo de distancia euclidiana entre un punto y la base de datos +----------------------------------------------------------------*/ +void todasDistEuclid(double pt1[], double aux[], double** atributos, int fi, int co); + +/*---------------------------------------------------------------- +Ordenamiento de las distancias de menor a mayor conservando clase +----------------------------------------------------------------*/ +void ordena(double datos[], int clases[], int clasesNo[], int fi); + +/*---------------------------------------------------------------- +Extraer los N primeros +----------------------------------------------------------------*/ +void extraeKPrimeros(double datos[], double kPrimeros[], int clases[], + int kClases[], int k); + +/*---------------------------------------------------------------- +Calcula la clase mas frecuente: Moda +----------------------------------------------------------------*/ +int claseMasFrecuente(int clases[], int k); + +/*---------------------------------------------------------------- +Mapea una clase en valor entero a un string +----------------------------------------------------------------*/ +char* obtieneClaseString(int claseint); + +/*---------------------------------------------------------------- +Calcula la clase de un conjunto de atributos usando KNN +----------------------------------------------------------------*/ +int clasificaKNN(double** datos, int clasesNum[], double dato[], int k, int fi, int co); + +
diff -r 000000000000 -r b78c322e81d0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Apr 14 04:02:05 2018 +0000 @@ -0,0 +1,108 @@ +#include "mbed.h" +#include "dataset.h" +#include "funcionesKNN.h" +#include <string> +Serial pc(USBTX, USBRX); +#define FI 100 // filas de la base de datos +#define CO 4 // numero de caracteristicas + +// variables +/*char* clasesSt[3]; // strings con las clases +char* nomAtr[CO]; // strings con los nombres de atributos +double atributos[FI][CO]; // atributos +int clasesNo[FI]; // clases +*/ +double dato[CO]; // dato a clasificar +long actual = 0, previo = 0; + +//----------------------------------------------------------------------- +// Extrae caracteres dados los identificadores y los convierte a double +//----------------------------------------------------------------------- +void extraeDatos(string str, double dato[], char dlm[]){ + + string auxstr = ""; + int posdlm[5], tamstr = 0; + int i= 0, j = 0; + + // buscar caracteres de identificacion + posdlm[0] = str.indexOf(dlm[0]); // ( + posdlm[1] = str.indexOf(dlm[1]); // , + posdlm[2] = str.indexOf(dlm[1], posdlm[1] + 1); // , + posdlm[3] = str.indexOf(dlm[1], posdlm[2] + 1); // , + posdlm[4] = str.indexOf(dlm[2]); // ) + + for(j=0; j<CO; j++){ + // extraer datos numericos + for(i=posdlm[j]+1; i<posdlm[j+1]; i++){ + auxstr = auxstr + str[i]; + } + + //convertir a double + int y = auxstr.length(); + char buf[y+1]; + auxstr.toCharArray(buf, y+1); + dato[j] = atof(buf); + + //Serial.println(dato[j],3); + auxstr=""; // reset del auxiliar + } +} + +//----------------------------------------------------------------------- +// Impresion de confirmacion +//----------------------------------------------------------------------- +void imprime(double dato[], int clase){ + + pc.printf("-----------------------------------------------\n"); + pc.printf("%i",(int16_t)nomAtr[0]); pc.printf(": "); pc.printf("%i",(int16_t)dato[0],3); pc.printf(" cms\n"); + pc.printf("%i",(int16_t)nomAtr[1]); pc.printf(": "); pc.printf("%i",(int16_t)dato[1],3); pc.printf(" cms\n"); + pc.printf("%i",(int16_t)nomAtr[2]); pc.printf(": "); pc.printf("%i",(int16_t)dato[2],3); pc.printf(" cms\n"); + pc.printf("%i",(int16_t)nomAtr[3]); pc.printf(": "); pc.printf("%i",(int16_t)dato[3],3); pc.printf(" cms\b"); + pc.printf(" >> Clase: "); pc.printf("%i\n",(int16_t)clasesSt[clase]); +} + +//----------------------------------------------------------------------- +// Configuracion del microcontrolador +//----------------------------------------------------------------------- +int main() +{ + pc.baud(9600); + //Serial.begin(9600); + wait(0.1); + pc.printf("===============================================\n"); + pc.printf("=== KKN Classification ===\n"); + pc.printf("===============================================\n"); + wait(0.05); + + //----------------------------------------------------------------------- +// Programa principal +//----------------------------------------------------------------------- + while(1) + { + + // recibir los datos a clasificar + if(Serial.available()) + { + + // recibir una cadena hasta que se tenga el '\n' + string str = Serial.readStringUntil('\n'); + extraeDatos(str, dato, "{,}"); + + // tic + previo = millis(); + + // encontrar la clase, con 5 vecinos cercanos + int cl = clasificaKNN((double**)atributos, clasesNo, dato, 11, FI, CO); + pc.printf("%i\n",(int16_t)cl); + + // tac + actual = millis(); + + // imprimir + imprime(dato, cl); + pc.printf("Tiempo: "); pc.printf("%i\n",(int16_t)actual-previo); pc.printf(" mS\n"); + } + } +} + +
diff -r 000000000000 -r b78c322e81d0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Apr 14 04:02:05 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa \ No newline at end of file
diff -r 000000000000 -r b78c322e81d0 util.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util.h Sat Apr 14 04:02:05 2018 +0000 @@ -0,0 +1,224 @@ +// tamaño de la base de datos +struct datos{ + + //filas y columnas + const int fi; + const int co; + + // cantidad de clases + const int clases; + + // clases de la base de datos en alfanumerico + String clasesSt = {"Setosa", "Versicolor", "Virginica"}; + + // atributos: conjunto de entrenamiento + String nomAtr[CO]={"Largo sepalo", "Ancho sepalo", "Largo petalo", "Ancho petalo"}; + + double atributos[FI][CO] = { + {4.4, 3, 1.3, 0.2}, + {7.6, 3, 6.6, 2.1}, + {6.5, 2.8, 4.6, 1.5}, + {5.6, 2.8, 4.9, 2}, + {7.2, 3, 5.8, 1.6}, + {5.5, 2.3, 4, 1.3}, + {5, 2, 3.5, 1}, + {5.5, 2.4, 3.8, 1.1}, + {5.4, 3.9, 1.7, 0.4}, + {5.5, 4.2, 1.4, 0.2}, + {5.8, 2.8, 5.1, 2.4}, + {4.6, 3.6, 1, 0.2}, + {4.8, 3.1, 1.6, 0.2}, + {6.9, 3.1, 4.9, 1.5}, + {5.8, 2.7, 5.1, 1.9}, + {5.7, 3.8, 1.7, 0.3}, + {6.4, 2.8, 5.6, 2.2}, + {6.1, 2.6, 5.6, 1.4}, + {6, 3.4, 4.5, 1.6}, + {6.7, 3.1, 4.7, 1.5}, + {5.1, 3.5, 1.4, 0.2}, + {6.3, 2.8, 5.1, 1.5}, + {6.3, 2.9, 5.6, 1.8}, + {6.7, 3.3, 5.7, 2.1}, + {4.9, 3, 1.4, 0.2}, +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2}, + {6.7, 3.1, 4.4, 1.4}, + {6.5, 3.2, 5.1, 2}, + {4.5, 2.3, 1.3, 0.3}, + {7.2, 3.2, 6, 1.8}, + {5.1, 3.8, 1.6, 0.2}, + {7.1, 3, 5.9, 2.1}, + {6, 2.9, 4.5, 1.5}, + {6.4, 3.2, 4.5, 1.5}, + {4.7, 3.2, 1.6, 0.2}, + {6.4, 2.7, 5.3, 1.9}, + {6.8, 3.2, 5.9, 2.3}, + {5, 3.5, 1.3, 0.3}, + {6.9, 3.1, 5.1, 2.3}, + {5.5, 2.5, 4, 1.3}, + {5.7, 4.4, 1.5, 0.4}, + {4.6, 3.1, 1.5, 0.2}, + {4.9, 3.1, 1.5, 0.1}, + {5.6, 3, 4.5, 1.5}, + {5.6, 3, 4.1, 1.3}, + {4.7, 3.2, 1.3, 0.2}, + {5, 3.2, 1.2, 0.2}, + {7.7, 2.8, 6.7, 2}, + {5.7, 2.8, 4.5, 1.3}, + {5.4, 3.4, 1.5, 0.4}, + {4.6, 3.2, 1.4, 0.2}}; + + int clasesNo[FI] = { + 0, + 2, + 1, + 2, + 2, + 1, + 1, + 1, + 0, + 0, + 2, + 0, + 0, + 1, + 2, + 0, + 2, + 2, + 1, + 1, + 0, + 2, + 2, + 2, + 0, + 2, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 2, + 2, + 1, + 1, + 2, + 0, + 0, + 0, + 1, + 0, + 1, + 2, + 1, + 2, + 1, + 1, + 2, + 1, + 1, + 2, + 1, + 0, + 0, + 2, + 1, + 2, + 2, + 2, + 0, + 2, + 2, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 2, + 0, + 0, + 1, + 2, + 0, + 2, + 0, + 2, + 1, + 1, + 0, + 2, + 2, + 0, + 2, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 2, + 1, + 0, + 0}; +}; + + + + + +