예제 #1
0
def BagFISonecv(zipFilePath, file_train, file_test, parameters,
                num_classifiers):
    # General parameters
    t_norm = parameters[3]
    max_size_of_premise = parameters[5]
    association_method = parameters[11]
    aggregation_method = parameters[12]

    # Gathering parameters
    # Formulation parameters:
    par_area, par_over, par_pcd = toolfis.get_formulation_parameters(
        parameters)

    # 1. Lecture & Fuzzification
    out1 = toolfis.lecture_fuz_one_cv(zipFilePath, file_train, file_test,
                                      parameters)
    ux_train, cbin_train = out1[0]
    ux_test, cbin_test = out1[1]
    sizes_attributes, premises_by_attribute, ref_attributes, premises_contain_negation = out1[
        2]
    freq_classes = out1[3]

    outputs_trees = []
    pars = [
        max_size_of_premise, t_norm, par_area, par_over, par_pcd,
        association_method, aggregation_method, freq_classes
    ]
    classifiers = []
    successful_classifiers = 0

    # ================================= GENERATION CLASSIFIERS =========================================================

    for i in range(num_classifiers):
        new_data, genesis_data = toolfis.create_data(
            ref_attributes, sizes_attributes, premises_by_attribute,
            premises_contain_negation, ux_train, cbin_train)
        exit_flag, salida = toolfis.inference_fuzzy(new_data, pars)
        # salida: [[premisesFinales, weights, method], [Train_prediction]]

        if exit_flag:
            successful_classifiers += 1
            outputs_trees.append(salida[1])  # Output Train

            # Transformation premises relative2absolute:
            converter = dict(zip(range(len(genesis_data[0])), genesis_data[0]))
            absolute_models = []
            for j in salida[0]:
                relative_premises = j[0]
                absolute_premises = toolfis.relative2absolute(
                    relative_premises, converter)
                absolute_models.append(
                    [absolute_premises, j[1],
                     j[2]])  # premises absolutes, Weights, name_method
            classifiers.append(absolute_models)

    # ======================================= PACKING IN ONE MODEL =================================================

    if not classifiers:
        return ["Any of %i classifiers was successful" % num_classifiers], [0]

    num_classes = cbin_train.shape[1]
    indexes_premises_byclass = []
    unique_indexes_byclass = []
    for i in range(num_classes):
        container_aux = []
        for j in classifiers:
            container_aux.append(j[i][0])

        list_premises_container = list(chain(*container_aux))
        unique_indexes = list(set(list_premises_container))
        unique_indexes_byclass.append(unique_indexes)
        unique_premises = toolfis.calculation_premises(unique_indexes,
                                                       ux_train, t_norm)
        indexes_premises_byclass.append([unique_indexes, unique_premises])

    exito, output_collective = toolfis.classifiers_aggregation(
        indexes_premises_byclass, cbin_train, 'MQR', freq_classes)

    if not exito:
        report, metrics = ["Final aggregation was fail"], [0]
    else:
        premises_weights_names, estimation_classes = output_collective

        final_premises_classes = []
        for i in range(len(premises_weights_names)):  # x cada clase
            final_premises_classes.append(premises_weights_names[i][0])

        f6 = Evaluation(premises_weights_names, final_premises_classes,
                        freq_classes)
        metrics_train = f6.eval_train(cbin_train, estimation_classes)
        metrics_test = f6.eval_test(cbin_test, ux_test, t_norm)

        results_cv = summary_cv_bagfis(successful_classifiers, metrics_train,
                                       metrics_test)
        report = toolfis.template_results(results_cv)

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_test[4]
            ], successful_classifiers
        ]

    return report, metrics
예제 #2
0
def random_fis_one_cv(zipFilePath, file_train, file_test,
                      parameters_classifiers, cv_i, clf_n):

    # General parameters
    cv_i += 1
    print()
    print('Clf = %i, CV = %i' % (clf_n, cv_i))
    successful_classifiers = 0
    classifiers = []
    outputs_tree_train_bin = []
    outputs_tree_test_bin = []
    outputs_tree_train = []
    outputs_tree_test = []
    partial_metrics_rules = []
    container_ac_train = []
    container_ac_test = []
    parameters = [parameters_classifiers[0]] + parameters_classifiers[1]
    t_norm = parameters[3]
    max_size_of_premise = parameters[5]
    association_method = parameters[11]
    aggregation_method = parameters[12]
    aux_blb = []

    # Gathering parameters
    # Formulation parameters:
    par_area, par_over, par_pcd = toolfis.get_formulation_parameters(
        parameters)

    # 1. Lecture & Fuzzification
    out1 = toolfis.lecture_fuz_one_cv(zipFilePath, file_train, file_test,
                                      parameters, cv_i)
    ux_train, cbin_train = out1[0]
    ux_test, cbin_test = out1[1]
    sizes_attributes, premises_by_attribute, ref_attributes, premises_contain_negation = out1[
        2]
    freq_classes = out1[3]

    pars = [
        max_size_of_premise, t_norm, par_area, par_over, par_pcd,
        association_method, aggregation_method, freq_classes
    ]

    # ================================= GENERATION CLASSIFIERS =========================================================

    param = GlobalParameter()
    for i in range(parameters[-4]):

        ux_train_blb, new_y_bin_blb, index_oob = toolfis.data_blb(
            ux_train, cbin_train)

        classifiers_blb = []

        n_blb = list(range(param.blb))

        for blb_i in n_blb:
            new_data_blb, genesis_data_blb = toolfis.create_data(
                ref_attributes, sizes_attributes, premises_by_attribute,
                premises_contain_negation, ux_train_blb, new_y_bin_blb)
            exit_flag_blb, out_model_blb = toolfis.inference_fuzzy(
                new_data_blb,
                pars,
                info=(str(blb_i), str(i + 1)),
                ensemble='RandomFIS')
            #  out_model = [premises_weights_names, train_bin_prediction, estimation_classes(u_estimation)]

            if exit_flag_blb:
                # successful_classifiers += 1
                #  Transformation premises relative2absolute:
                converter_blb = dict(
                    zip(range(len(genesis_data_blb[0])), genesis_data_blb[0]))
                absolute_model_blb = []
                end_premises_classes_blb = []
                for j in out_model_blb[0]:  # by Class
                    relative_premises_blb = j[0]
                    absolute_premises_blb = toolfis.relative2absolute(
                        relative_premises_blb, converter_blb)
                    end_premises_classes_blb.append(
                        absolute_premises_blb
                    )  # For estimation metrics rules and premises
                    absolute_model_blb.append(
                        [absolute_premises_blb, j[1],
                         j[2]])  # premises absolutes, Weights, name_method
                classifiers_blb.append(absolute_model_blb)

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []

        for ci in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in classifiers_blb:
                container_aux.append(j[ci][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(
                unique_indexes, ux_train_blb, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        success, out_model = toolfis.classifiers_aggregation(
            indexes_premises_byclass,
            new_y_bin_blb,
            'MQR',
            freq_classes,
            info=('All models', str(i + 1)))

        if success:
            successful_classifiers += 1
            absolute_model = []
            end_premises_classes = []
            for j in out_model[0]:  # by Class
                absolute_premises = j[0]
                end_premises_classes.append(
                    absolute_premises
                )  # For estimation metrics rules and premises
                absolute_model.append(
                    [absolute_premises, j[1],
                     j[2]])  # premises absolutes, Weights, name_method
            classifiers.append(absolute_model)
            # Metrics Train
            container_ac_train.append(
                eval_metrics(new_y_bin_blb, out_model[1])[0])

            outputs_tree_train.append(
                eval_classifier_one(absolute_model, ux_train,
                                    t_norm))  # Out U by class train
            outputs_tree_train_bin.append(
                decision_class(outputs_tree_train[-1], freq_classes))

            # Metrics Test
            outputs_tree_test.append(
                eval_classifier_one(absolute_model, ux_test,
                                    t_norm))  # Out U by class test
            outputs_tree_test_bin.append(
                decision_class(outputs_tree_test[-1], freq_classes))
            container_ac_test.append(
                round(
                    eval_metrics(cbin_test, outputs_tree_test_bin[-1])[0], 2))

            aux_metrics = metricas_rules_premises(end_premises_classes)
            partial_metrics_rules.append(hstack(aux_metrics))

    if classifiers:
        return ux_train, ux_test, cbin_train, cbin_test, freq_classes, parameters_classifiers, classifiers, cv_i,\
               outputs_tree_train, outputs_tree_train_bin, container_ac_train,\
               outputs_tree_test, outputs_tree_test_bin, partial_metrics_rules
    elif not classifiers:
        return [
            "Any of %i classifiers was successful" % successful_classifiers
        ], [0], cv_i
예제 #3
0
def random_fis_one_cv(zipFilePath, file_train, file_test,
                      parameters_classifiers, cv_i, clf_n, folder__tree_output,
                      time_ini):

    # General parameters
    p_method_agg = GlobalParameter()
    method_aggreg_classf = p_method_agg.method_aggregation
    cv_i += 1
    # print ('Clf = %i, CV = %i' % (clf_n, cv_i))
    successful_classifiers = 0
    classifiers = []
    outputs_tree_train_bin = []
    outputs_tree_test_bin = []
    outputs_tree_train = []
    outputs_tree_test = []
    partial_metrics_rules = []
    container_ac_train = []
    container_train = []
    container_ac_test = []
    container_test = []
    parameters = [parameters_classifiers[0]] + parameters_classifiers[1]
    t_norm = parameters[3]
    max_size_of_premise = parameters[5]
    association_method = parameters[11]
    aggregation_method = parameters[12]
    aux_blb = []

    # Gathering parameters
    # Formulation parameters:
    par_area, par_over, par_pcd = toolfis.get_formulation_parameters(
        parameters)
    p_blb = GlobalParameter()

    # 0. Divide data
    train_clt, test_clt = toolfis.divide_data(zipFilePath, file_train,
                                              file_test)
    matrix_train_clt = np.array(train_clt)
    matrix_test_clt = np.array(test_clt)

    for nc in range(matrix_train_clt.shape[0]):

        # 1. Lecture & Fuzzification
        out1 = toolfis.lecture_fuz_one_cv_v2(matrix_train_clt[nc],
                                             matrix_test_clt[nc], parameters)
        ux_train, cbin_train = out1[0]
        ux_test, cbin_test = out1[1]
        sizes_attributes, premises_by_attribute, ref_attributes, premises_contain_negation = out1[
            2]
        freq_classes = out1[3]

        pars = [
            max_size_of_premise, t_norm, par_area, par_over, par_pcd,
            association_method, aggregation_method, freq_classes
        ]

        # ===== Classifiers =====
        i_blb = 0
        classifiers_blb = []
        n_blb = list(range(p_blb.blb))
        for blb_i in n_blb:
            i_blb += 1

            new_data_blb, genesis_data_blb = toolfis.create_data_v2(
                ref_attributes, sizes_attributes, premises_by_attribute,
                premises_contain_negation, ux_train, cbin_train)

            exit_flag_blb, out_model_blb = toolfis.inference_fuzzy\
                (new_data_blb, pars, info=(str(i_blb), str(nc)), ensemble='RandomFIS')
            #  out_model = [premises_weights_names, train_bin_prediction, estimation_classes(u_estimation)]

            if exit_flag_blb:
                # successful_classifiers += 1
                #  Transformation premises relative2absolute:
                converter_blb = dict(
                    zip(range(len(genesis_data_blb[0])), genesis_data_blb[0]))
                absolute_model_blb = []
                end_premises_classes_blb = []
                for j in out_model_blb[0]:  # by Class
                    relative_premises_blb = j[0]
                    absolute_premises_blb = toolfis.relative2absolute(
                        relative_premises_blb, converter_blb)
                    end_premises_classes_blb.append(
                        absolute_premises_blb
                    )  # For estimation metrics rules and premises
                    absolute_model_blb.append(
                        [absolute_premises_blb, j[1],
                         j[2]])  # premises absolutes, Weights, name_method
                classifiers_blb.append(absolute_model_blb)

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for ci in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in classifiers_blb:
                container_aux.append(j[ci][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(
                unique_indexes, ux_train, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        success, out_model = toolfis.classifiers_aggregation(
            indexes_premises_byclass,
            cbin_train,
            'MQR',
            freq_classes,
            info=('All models', str(nc)))

        if success:
            successful_classifiers += 1
            absolute_model = []
            end_premises_classes = []
            for j in out_model[0]:  # by Class
                absolute_premises = j[0]
                end_premises_classes.append(
                    absolute_premises
                )  # For estimation metrics rules and premises
                absolute_model.append(
                    [absolute_premises, j[1],
                     j[2]])  # premises absolutes, Weights, name_method
            classifiers.append(absolute_model)

            # Metrics Train
            outputs_tree_train.append(
                eval_classifier_one(absolute_model, ux_train,
                                    t_norm))  # Out U by class train
            outputs_tree_train_bin.append(
                decision_class(outputs_tree_train[-1], freq_classes))
            container_ac_train.append(
                eval_metrics(cbin_train, out_model[1])[0])
            container_train.append(cbin_train)

            # Metrics Test
            outputs_tree_test.append(
                eval_classifier_one(absolute_model, ux_test,
                                    t_norm))  # Out U by class test
            outputs_tree_test_bin.append(
                decision_class(outputs_tree_test[-1], freq_classes))
            container_ac_test.append(
                round(
                    eval_metrics(cbin_test, outputs_tree_test_bin[-1])[0], 2))
            container_test.append(cbin_test)

            aux_metrics = metricas_rules_premises(end_premises_classes)
            partial_metrics_rules.append(hstack(aux_metrics))

        if not classifiers:
            return [
                "Any of %i classifiers was successful" % successful_classifiers
            ], [0], cv_i

    if method_aggreg_classf == 0:

        #  ============================== SPECIALIST EVALUATION ================================================

        metrics_train = eval_metrics(np.vstack(container_train),
                                     np.vstack(outputs_tree_train_bin))
        metrics_test = eval_metrics(np.vstack(container_test),
                                    np.vstack(outputs_tree_test_bin))

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        metrics_rules = aux_metrics_rules

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1], metrics_rules[2]
        ]

        report = toolfis.template_results('', results)

        return report, metrics, cv_i

    elif method_aggreg_classf == 1:

        #  ============================== AVERAGE AGGREGATION ================================================

        #  Evaluation Train
        u_by_class = estimation_average(outputs_tree_train)
        train_decision = decision_class(u_by_class, freq_classes)
        metrics_train = eval_metrics(cbin_train, train_decision)

        #  Evaluation Test_average
        u_by_class = estimation_average(outputs_tree_test)
        test_decision = decision_class(u_by_class, freq_classes)
        metrics_test = eval_metrics(cbin_test, test_decision)

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        metrics_rules = aux_metrics_rules

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1]
        ]

        report = toolfis.template_results('', results)

        return report, metrics, cv_i

    elif method_aggreg_classf == 2:

        #  ============================== WEIGHT AGGREGATION ================================================

        weight_by_classifier = estimation_weight(
            outputs_tree_train, cbin_train)  # Class train binario

        # Evaluation Train
        output_tree_train_aggregated = aggregation_classifiers_by_weight(
            weight_by_classifier, outputs_tree_train)
        train_decision = decision_class(output_tree_train_aggregated,
                                        freq_classes)
        metrics_train = eval_metrics(cbin_train, train_decision)

        # Evaluation Test
        output_tree_test_aggregated = aggregation_classifiers_by_weight(
            weight_by_classifier, outputs_tree_test)
        test_decision = decision_class(output_tree_test_aggregated,
                                       freq_classes)
        metrics_test = eval_metrics(cbin_test, test_decision)

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        constant = len(partial_metrics_rules)
        metrics_rules = aux_metrics_rules / constant

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1]
        ]

        report = toolfis.template_results('', results)
        return report, metrics, cv_i

    elif method_aggreg_classf == 3:

        #  ============================== WEIGHT_BY_CLASS AGGREGATION ================================================

        weight_class_by_classifier = estimation_weight_by_class(
            outputs_tree_train, cbin_train)  # Class train binario

        # Evaluation Train
        output_tree_train_aggregated = aggregation_classifiers_by_weight_class(
            weight_class_by_classifier, outputs_tree_train)
        train_decision = decision_class(output_tree_train_aggregated,
                                        freq_classes)
        metrics_train = eval_metrics(cbin_train, train_decision)

        # Evaluation Test
        output_tree_test_aggregated = aggregation_classifiers_by_weight_class(
            weight_class_by_classifier, outputs_tree_test)
        test_decision = decision_class(output_tree_test_aggregated,
                                       freq_classes)
        metrics_test = eval_metrics(cbin_test, test_decision)

        #  Evaluation rules
        aux_metrics_rules = sum(partial_metrics_rules, 0)
        constant = len(partial_metrics_rules)
        metrics_rules = aux_metrics_rules / constant

        metrics = [
            1,
            [
                metrics_train[0], metrics_test[0], metrics_train[1],
                metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0], metrics_test[0],
            metrics_train[1], metrics_test[1], metrics_rules[0],
            metrics_rules[1]
        ]

        report = toolfis.template_results('', results)
        return report, metrics, cv_i

    elif method_aggreg_classf == 4:

        #  ============================== PACKING ONE MODEL V.1 ================================================

        #  Evaluation Test_average
        u_by_class_ave = estimation_average(outputs_tree_test)
        test_decision_ave = decision_class(u_by_class_ave, freq_classes)
        metrics_test_ave = round(
            eval_metrics(cbin_test, test_decision_ave)[0], 2)

        voting_bin = voting(outputs_tree_test_bin)
        vot_bin = decision_class(voting_bin, freq_classes)
        metrics_test_vot = round(eval_metrics(cbin_test, vot_bin)[0], 2)

        # vec_dng = np.array(i_dng)
        # redu_trn = vec_dng.shape[0] / ux_train.shape[0]

        #  Evaluation rules
        aux_metrics_rules_ave = sum(partial_metrics_rules, 0)
        metrics_rules_ave = aux_metrics_rules_ave[0]
        aux_t0 = timeit.default_timer()
        time_finished = aux_t0 - time_ini

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'ac_teste_av.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_test_ave) + '\n')

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'ac_teste_vot.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_test_vot) + '\n')

        # folder__tree_output_test = os.path.join(folder__tree_output, 'reducao_trn.txt')
        # with open(folder__tree_output_test, 'a') as f:
        #     f.write(str(redu_trn) + '\n')

        folder__tree_output_test = os.path.join(folder__tree_output,
                                                'regras_teste_10CV.txt')
        with open(folder__tree_output_test, 'a') as f:
            f.write(str(metrics_rules_ave) + '\n')

        if cv_i == 10:
            folder__tree_output_test = os.path.join(folder__tree_output,
                                                    'time_teste_10CV.txt')
            with open(folder__tree_output_test, 'a') as f:
                f.write(str(time_finished) + '\n')

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for i in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in classifiers:
                container_aux.append(j[i][0])

            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(
                unique_indexes, ux_train, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        exito, output_collective = toolfis.classifiers_aggregation(
            indexes_premises_byclass,
            cbin_train,
            'MQR',
            freq_classes,
            info=('batches', 'All'))

        if exito:
            premises_weights_names = output_collective[0]
            estimation_classes = output_collective[1]

            final_premises_classes = []
            for i in range(len(premises_weights_names)):  # x cada clase
                final_premises_classes.append(premises_weights_names[i][0])

            F6 = Evaluation(premises_weights_names, final_premises_classes,
                            freq_classes)
            metrics_train = F6.eval_train(cbin_train, estimation_classes)
            metrics_test = F6.eval_test(cbin_test, ux_test, t_norm)
            metrics_rules = metrics_test[4]

            metrics = [
                1,
                [
                    metrics_train[0] * 100, metrics_test[0] * 100,
                    metrics_train[1], metrics_test[1], metrics_rules
                ], successful_classifiers
            ]
            results = [
                successful_classifiers, metrics_train[0] * 100,
                metrics_test[0] * 100, metrics_train[1], metrics_test[1],
                metrics_rules[0], metrics_rules[1], metrics_rules[2]
            ]

            report = toolfis.template_results('', results)
            return report, metrics, cv_i

    elif method_aggreg_classf == 5:

        #  ============================== PACKING MODEL v.2 ================================================

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for i in range(
                num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            container_w = []
            for j in classifiers:
                container_aux.append(j[i][0])
                container_w.append(j[i][1].tolist())

            list_premises_container = list(chain(*container_aux))
            list_w_container = sum(list(chain(*container_w)), [])
            container_dic = dict(zip(list_premises_container,
                                     list_w_container))
            rule_duplicate = list(list_duplicates(list_premises_container))
            for rule in rule_duplicate:
                aver_w = sum(array(list_w_container)[rule[1]]) / len(rule[1])
                container_dic[rule[0]] = aver_w
            unique_indexes = list(container_dic.keys())
            aux_unique_w = array(container_dic.values()) / sum(
                container_dic.values())
            unique_w = aux_unique_w.reshape(aux_unique_w.shape[0], 1)
            indexes_premises_byclass.append([unique_indexes, unique_w, 'MQR'])

        output_classifier = eval_classifier_one(indexes_premises_byclass,
                                                ux_train,
                                                t_norm)  # Out U by class train
        output_bin = decision_class(output_classifier, freq_classes)

        final_premises_classes = []
        for i in range(len(indexes_premises_byclass)):  # x cada clase
            final_premises_classes.append(indexes_premises_byclass[i][0])

        F6 = Evaluation(indexes_premises_byclass, final_premises_classes,
                        freq_classes)
        metrics_train = F6.eval_train(cbin_train, output_bin)
        metrics_test = F6.eval_test(cbin_test, ux_test, t_norm)
        metrics_rules = metrics_test[4]

        metrics = [
            1,
            [
                metrics_train[0] * 100, metrics_test[0] * 100,
                metrics_train[1], metrics_test[1], metrics_rules
            ], successful_classifiers
        ]
        results = [
            successful_classifiers, metrics_train[0] * 100,
            metrics_test[0] * 100, metrics_train[1], metrics_test[1],
            metrics_rules[0], metrics_rules[1]
        ]

        report = toolfis.template_results('', results)
        return report, metrics, cv_i
예제 #4
0
def random_fis_one_cv(zipFilePath, file_train, file_test, parameters_classifiers, cv_i, clf_n):

    # General parameters
    cv_i += 1
    print()
    print('Clf = %i, CV = %i' % (clf_n, cv_i))
    successful_classifiers = 0
    classifiers = []
    outputs_tree_train_bin = []
    outputs_tree_test_bin = []
    outputs_tree_train = []
    outputs_tree_test = []
    partial_metrics_rules = []
    container_ac_train = []
    container_ac_test = []
    parameters = [parameters_classifiers[0]] + parameters_classifiers[1]
    t_norm = parameters[3]
    max_size_of_premise = parameters[5]
    association_method = parameters[11]
    aggregation_method = parameters[12]
    aux_blb = []

    # Gathering parameters
    # Formulation parameters:
    par_area, par_over, par_pcd = toolfis.get_formulation_parameters(parameters)

    # 1. Lecture & Fuzzification
    out1 = toolfis.lecture_fuz_one_cv(zipFilePath, file_train, file_test, parameters, cv_i)
    ux_train, cbin_train = out1[0]
    ux_test, cbin_test = out1[1]
    sizes_attributes, premises_by_attribute, ref_attributes, premises_contain_negation = out1[2]
    freq_classes = out1[3]

    pars = [max_size_of_premise, t_norm, par_area, par_over, par_pcd,
            association_method, aggregation_method, freq_classes]

# ================================= GENERATION CLASSIFIERS =========================================================


    param = GlobalParameter()
    for i in range(parameters[-4]):
        # Aqui ele fuzifica e faz a bag of little bootstrap?
        # Nao, ele ja entra fuzzificado
        ux_train_blb, new_y_bin_blb, index_oob = toolfis.data_blb(ux_train, cbin_train)

        classifiers_blb = []

        n_blb = list(range(param.blb))
        # TODO: Parei aqui. Aqui é onde ocorre a montagem do classificador SENFIS
        for blb_i in n_blb:
            # TODO: Aqui é onde ele cria os subsets do SENFIS. blb = bag of little bootstrap
            new_data_blb, genesis_data_blb = toolfis.create_data(ref_attributes, sizes_attributes,
                                                                 premises_by_attribute,
                                                                 premises_contain_negation,
                                                                 ux_train_blb, new_y_bin_blb)
            # genesis = [columns, random_features, sub_premises_by_attribute]  # the most important is "columns"
            # sub_data = [new_ux, new_y_bin, new_ref_attributes, new_premises_by_attribute,
                # sub_sizes_attributes, sub_premises_contain_negation]
            # genesis e subdata sao retornos do create_data

            # TODO: Aqui é onde ele faz o autofis, eu acho.
            exit_flag_blb, out_model_blb = toolfis.inference_fuzzy(new_data_blb, pars, info=(str(blb_i), str(i+1)),
                                                                   ensemble='RandomFIS')
            #  out_model = [premises_weights_names, train_bin_prediction, estimation_classes(u_estimation)]
            # Aqui ele imprime no console "Aggregation finished" pra cada sub classificador

            if exit_flag_blb:
                # successful_classifiers += 1
                #  Transformation premises relative2absolute:
                converter_blb = dict(zip(range(len(genesis_data_blb[0])), genesis_data_blb[0]))
                absolute_model_blb = []
                end_premises_classes_blb = []
                for j in out_model_blb[0]:  # by Class
                    relative_premises_blb = j[0]
                    absolute_premises_blb = toolfis.relative2absolute(relative_premises_blb, converter_blb)
                    end_premises_classes_blb.append(absolute_premises_blb)  # For estimation metrics rules and premises
                    absolute_model_blb.append([absolute_premises_blb, j[1], j[2]])  # premises absolutes, Weights, name_method
                classifiers_blb.append(absolute_model_blb)
        #Aqui no numero de classificadores ja da pra ver que nem todos eles restam. de 10 classificadores só 8 conseguiram ser criados.

        num_classes = cbin_train.shape[1]
        indexes_premises_byclass = []
        for ci in range(num_classes):  # debo de colocar aqui el numero de clases
            container_aux = []
            for j in classifiers_blb:
                container_aux.append(j[ci][0])
            # Aqui ele começa a remover as premissas redundantes entre os sub classificadores
            list_premises_container = list(chain(*container_aux))
            unique_indexes = list(set(list_premises_container))
            unique_premises = toolfis.calculation_premises(unique_indexes, ux_train_blb, t_norm)
            indexes_premises_byclass.append([unique_indexes, unique_premises])

        # O passo de agregação dos classificadores ocorre aqui. tudo junto. Não é exatamente o mesmo do autoFIS
        success, out_model = toolfis.classifiers_aggregation(indexes_premises_byclass, new_y_bin_blb, 'MQR',
                                                             freq_classes, info=('All models', str(i+1)))
                                                            #  No final ele printa a mensagem "All models aggregation finished"
                                                            # Na vdd isso me parece um pruning das agregações
        # Essa parte aqui do final é inutil. Só tem q extrair como fazer o .predict no passo do eval_metrics
        # O método acho que é o eval_classifier_one
        if success:
            successful_classifiers += 1
            absolute_model = []
            end_premises_classes = []
            for j in out_model[0]:  # by Class
                absolute_premises = j[0]
                end_premises_classes.append(absolute_premises)  # For estimation metrics rules and premises
                absolute_model.append([absolute_premises, j[1], j[2]])  # premises absolutes, Weights, name_method
            classifiers.append(absolute_model)
            # Metrics Train
            container_ac_train.append(eval_metrics(new_y_bin_blb, out_model[1])[0])

            outputs_tree_train.append(eval_classifier_one(absolute_model, ux_train, t_norm))  # Out U by class train
            outputs_tree_train_bin.append(decision_class(outputs_tree_train[-1], freq_classes))

            # Metrics Test
            outputs_tree_test.append(eval_classifier_one(absolute_model, ux_test, t_norm))  # Out U by class test
            outputs_tree_test_bin.append(decision_class(outputs_tree_test[-1], freq_classes))
            container_ac_test.append(round(eval_metrics(cbin_test, outputs_tree_test_bin[-1])[0], 2))

            aux_metrics = metricas_rules_premises(end_premises_classes)
            partial_metrics_rules.append(hstack(aux_metrics))

    if classifiers:
        return ux_train, ux_test, cbin_train, cbin_test, freq_classes, parameters_classifiers, classifiers, cv_i,\
               outputs_tree_train, outputs_tree_train_bin, container_ac_train,\
               outputs_tree_test, outputs_tree_test_bin, partial_metrics_rules
    elif not classifiers:
        return ["Any of %i classifiers was successful" % successful_classifiers], [0], cv_i