Exemplo n.º 1
0
 def __init__(self):
     self.datos = Datos()
     self.datos.datosConEnterosNormalizados()
     self.entradaDeseada = self.datos.Datos
     self.salidaDeseada = self.datos.Resultado
     X = self.datos.Datos
     y = self.datos.Resultado
     C = 1.0
     self.models = (svm.SVC(kernel='linear', C=C),
               svm.LinearSVC(C=C),
               svm.SVC(kernel='rbf', gamma=0.7, C=C),
               svm.SVC(kernel='poly', degree=3, C=C))
     titles = ('SVC with linear kernel',
               'LinearSVC (linear kernel)',
               'SVC with RBF kernel',
               'SVC with polynomial (degree 3) kernel')
Exemplo n.º 2
0
 def GenerarModelos(self):
     self.datos = Datos()
     self.datos.datosConEnterosNormalizados()
     self.entradaDeseada = self.datos.Datos
     self.salidaDeseada = self.datos.Resultado
     X = self.datos.Datos
     y = self.datos.Resultado
     X,X_test,y,y_test =  train_test_split(X, y, test_size=.8,
                                                 random_state=0)
     C = 1.0  # SVM regularization parameter
     self.models = (svm.SVC(kernel='linear', C=C),
               svm.SVC(kernel='rbf', gamma=0.7, C=C))
     
     titles = ('SVC with linear kernel',
               'SVC with RBF kernel')
     
     for key in (0,1,2,3):
         print(titles[key])
         model = self.models[key].fit(X, y)
         score = model.score(X_test,y_test)
         Y_resultado = model.decision_function(X_test)
         fpr, tpr, _ = roc_curve(y_test, Y_resultado)
         roc_auc = auc(fpr, tpr)
         plt.figure()
         lw = 2
         plt.plot(fpr, tpr, color='darkorange',
                  lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
         plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
         plt.xlim([0.0, 1.0])
         plt.ylim([0.0, 1.05])
         plt.xlabel('False Positive Rate')
         plt.ylabel('True Positive Rate')
         plt.title(" {} ---->  precisión: {}".format(titles[key], score))
         plt.legend(loc="lower right")
         plt.show()
         
         score = model.score(X,y)
         print(score)
         self.generarMatrizDeConfusion(model)
Exemplo n.º 3
0
 def GenerarDatosParaPrueba(self):
     datos = Datos()
     datos.datosConEnterosNormalizados()
     self.entradaDeseada = datos.Datos
     elementos = np.random.permutation(math.ceil(len(self.entradaDeseada)))
     self.x0.setText(str(self.entradaDeseada[elementos[0]][0]))
     self.x1.setText(str(self.entradaDeseada[elementos[0]][1]))
     self.x2.setText(str(self.entradaDeseada[elementos[0]][2]))
     self.x3.setText(str(self.entradaDeseada[elementos[0]][3]))
     self.x4.setText(str(self.entradaDeseada[elementos[0]][4]))
     self.x5.setText(str(self.entradaDeseada[elementos[0]][5]))
     self.x6.setText(str(self.entradaDeseada[elementos[0]][6]))
     self.x7.setText(str(self.entradaDeseada[elementos[0]][7]))
     self.x8.setText(str(self.entradaDeseada[elementos[0]][8]))
     self.x9.setText(str(self.entradaDeseada[elementos[0]][9]))
     self.x10.setText(str(self.entradaDeseada[elementos[0]][10]))
     self.x11.setText(str(self.entradaDeseada[elementos[0]][11]))
     self.x12.setText(str(self.entradaDeseada[elementos[0]][12]))
     self.x13.setText(str(self.entradaDeseada[elementos[0]][13]))
     self.x14.setText(str(self.entradaDeseada[elementos[0]][14]))
     self.x15.setText(str(self.entradaDeseada[elementos[0]][15]))
     self.x16.setText(str(self.entradaDeseada[elementos[0]][16]))
     self.x17.setText(str(self.entradaDeseada[elementos[0]][17]))
     self.x18.setText(str(self.entradaDeseada[elementos[0]][18]))
Exemplo n.º 4
0
 def ConsultaAG(self):
     entrada = [
         float(self.x0.text()),
         float(self.x1.text()),
         float(self.x2.text()),
         float(self.x3.text()),
         float(self.x4.text()),
         float(self.x5.text()),
         float(self.x6.text()),
         float(self.x7.text()),
         float(self.x8.text()),
         float(self.x9.text()),
         float(self.x10.text()),
         float(self.x11.text()),
         float(self.x12.text()),
         float(self.x13.text()),
         float(self.x14.text()),
         float(self.x15.text()),
         float(self.x16.text()),
         float(self.x17.text()),
         float(self.x18.text())
     ]
     datos = Datos()
     entradaNormalizada = datos.Convertir19A55(entrada)
     algoritmoGenetico = RedNeuronal()
     Y_resultado, y = algoritmoGenetico.ProbarModeloAlgoritmosGeneticos()
     salida, a, b, c = algoritmoGenetico.RedNeuronal1CapaOculta(
         entradaNormalizada)
     resultado = "Positivo" if round(salida) == 1 else "Negativo"
     _translate = QtCore.QCoreApplication.translate
     self.label_45.setText(
         _translate(
             "MainWindow",
             "<html><head/><body><p><span style=\" font-size:12pt;\">" +
             resultado + "  " + str(salida) +
             "</span></p><p><br/></p></body></html>"))
Exemplo n.º 5
0
class SVM(object):
    def __init__(self):
        self.datos = Datos()
        self.datos.datosConEnterosNormalizados()
        self.entradaDeseada = self.datos.Datos
        self.salidaDeseada = self.datos.Resultado
        X = self.datos.Datos
        y = self.datos.Resultado
        C = 1.0
        self.models = (svm.SVC(kernel='linear', C=C),
                  svm.LinearSVC(C=C),
                  svm.SVC(kernel='rbf', gamma=0.7, C=C),
                  svm.SVC(kernel='poly', degree=3, C=C))
        titles = ('SVC with linear kernel',
                  'LinearSVC (linear kernel)',
                  'SVC with RBF kernel',
                  'SVC with polynomial (degree 3) kernel')
    
    def GenerarModelos(self):
        self.datos = Datos()
        self.datos.datosConEnterosNormalizados()
        self.entradaDeseada = self.datos.Datos
        self.salidaDeseada = self.datos.Resultado
        X = self.datos.Datos
        y = self.datos.Resultado
        X,X_test,y,y_test =  train_test_split(X, y, test_size=.8,
                                                    random_state=0)
        C = 1.0  # SVM regularization parameter
        self.models = (svm.SVC(kernel='linear', C=C),
                  svm.SVC(kernel='rbf', gamma=0.7, C=C))
        
        titles = ('SVC with linear kernel',
                  'SVC with RBF kernel')
        
        for key in (0,1,2,3):
            print(titles[key])
            model = self.models[key].fit(X, y)
            score = model.score(X_test,y_test)
            Y_resultado = model.decision_function(X_test)
            fpr, tpr, _ = roc_curve(y_test, Y_resultado)
            roc_auc = auc(fpr, tpr)
            plt.figure()
            lw = 2
            plt.plot(fpr, tpr, color='darkorange',
                     lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
            plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
            plt.xlim([0.0, 1.0])
            plt.ylim([0.0, 1.05])
            plt.xlabel('False Positive Rate')
            plt.ylabel('True Positive Rate')
            plt.title(" {} ---->  precisión: {}".format(titles[key], score))
            plt.legend(loc="lower right")
            plt.show()
            
            score = model.score(X,y)
            print(score)
            self.generarMatrizDeConfusion(model)
            #filename = titles[key]+'PruebaDatosRetinopatia.sav'
            #pickle.dump(self.models[key], open(filename, 'wb'))
    
    def generarMatrizDeConfusion(self,model ,porcentajePrueba = 0):
        self.matrizDeConfusion = [[0,0],[0,0]]
        if porcentajePrueba == 0:
            elementos = len(self.entradaDeseada)
        else:
            elementos = np.random.permutation(math.ceil(len(self.entradaDeseada)*porcentajePrueba)) # lista desordenada para el test
        self.error=0
        for j in range(elementos):
            entrada = self.entradaDeseada[j]
            salidaEsperada = self.salidaDeseada[j]
            salida = model.predict([entrada])[0];
            self.error += 0.5*(salida - salidaEsperada)**2
            self.matrizDeConfusion[round(salida)][salidaEsperada] += 1
            #print("salida {} salidadeseada {}".format(salida,salidaEsperada))
            
        print("matriz de confusion")
        print(self.matrizDeConfusion[0])
        print(self.matrizDeConfusion[1])
        
        self.precision =  (self.matrizDeConfusion[0][0] + self.matrizDeConfusion[1][1] )/elementos
        self.specificity = self.matrizDeConfusion[0][0] / (self.matrizDeConfusion[0][0] + self.matrizDeConfusion[1][0])
        self.sensitivity = self.matrizDeConfusion[1][1] / (self.matrizDeConfusion[1][1] + self.matrizDeConfusion[0][1])
        
        print(self.precision,self.sensitivity,self.specificity)
            
        
    def probarKernelLineal(self):
        X = self.datos.Datos
        y = self.datos.Resultado
        loaded_model_SVM = pickle.load(open("../SVM/SVC with linear kernelPruebaDatosRetinopatia.sav", 'rb'))
        Y_resultado = loaded_model_SVM.decision_function(X)
        fprSVM, tprSVM, _ = roc_curve(y, Y_resultado)
        self.roc_aucSVM = auc(fprSVM, tprSVM)
        self.generarMatrizDeConfusion(loaded_model_SVM)
        
    def PredictKernelLineal(self,entrada):
        loaded_model_SVM = pickle.load(open("../SVM/SVC with linear kernelPruebaDatosRetinopatia.sav", 'rb'))
        Y_resultado = loaded_model_SVM.predict([entrada])[0]
        return Y_resultado
Exemplo n.º 6
0
 def __init__(self):
     self.datos = Datos()
     self.datos.datosConEnterosNormalizados()
     self.entradaDeseada = self.datos.Datos
     self.salidaDeseada = self.datos.Resultado
Exemplo n.º 7
0
class Plot_roc:
    def __init__(self):
        self.datos = Datos()
        self.datos.datosConEnterosNormalizados()
        self.entradaDeseada = self.datos.Datos
        self.salidaDeseada = self.datos.Resultado

    def plot_roc_completa(self):
        X = self.datos.Datos
        y = self.datos.Resultado

        # cargar el modelo de SVM
        loaded_model_SVM = pickle.load(
            open("../SVM/SVC with linear kernelPruebaDatosRetinopatia.sav",
                 'rb'))
        Y_resultado = loaded_model_SVM.decision_function(X)
        fprSVM, tprSVM, _ = roc_curve(y, Y_resultado)
        roc_aucSVM = auc(fprSVM, tprSVM)

        #Cargar el modelo de Red neuronal entrenada por backpropagation
        backpropagation = RedNeuronal()
        Y_resultado, y = backpropagation.ProbarModeloBackpropagation()
        fprBP, tprBP, _ = roc_curve(y, Y_resultado)
        roc_aucBP = auc(fprBP, tprBP)

        #Cargar el modelo de red neuronal entrenada por algoritmos genéticos
        algoritmoGenetico = RedNeuronal()
        Y_resultado, y = algoritmoGenetico.ProbarModeloAlgoritmosGeneticos()
        fprAG, tprAG, _ = roc_curve(y, Y_resultado)
        roc_aucAG = auc(fprAG, tprAG)

        plt.figure()
        lw = 2
        plt.plot(fprAG,
                 tprAG,
                 color='darkorange',
                 lw=lw,
                 label='Algoritmos Genéticos (área = %0.2f)' %
                 roc_aucAG)  #plot roc Algoritmos genéticos

        plt.plot(fprBP,
                 tprBP,
                 label='Backpropagation (área = {0:0.2f})'
                 ''.format(roc_aucBP),
                 color='deeppink',
                 linestyle=':',
                 linewidth=4)  #plot roc Backpropagation

        plt.plot(fprSVM,
                 tprSVM,
                 label='SVM (área = {0:0.2f})'
                 ''.format(roc_aucSVM),
                 color='navy',
                 linestyle='-.',
                 linewidth=2)

        plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('Ratio Falsos Positivos')
        plt.ylabel('Ratio Verdaderos Positivos')
        plt.title("Comparación")
        plt.legend(loc="lower right")
        plt.show()