コード例 #1
0
def formEnsembleClassifiers(training_class, training_data, max_attribute_values, k, max_run=5):

    num_training_data = len(training_class)

    ensemble_classifiers = []
    errors_Mi = []

    tuple_weights = [(1.0 / len(training_data)) for i in range(0, len(training_data))]
    #tuple_ids = [i for i in range(0, len(training_class))]

    for rk in range(0, k):
        run_emis = []
        run_classifiers = []
        run_predictions = []
        run_training_class = []

        current_run = 0
        while True:
            new_training_data = []
            new_training_class = []
            prefixed_weights = prefixScan(tuple_weights)
            for i in range(0, num_training_data):
                pick_id = drawRandomPD(prefixed_weights)#random.choice(tuple_ids)
                new_training_data.append(training_data[pick_id])
                new_training_class.append(training_class[pick_id])

            Mi = nb.makeClassifier(new_training_class, new_training_data, max_attribute_values)
            predicted_class = nb.predictClass(new_training_data, Mi)

            eMi = findMiError(predicted_class, new_training_class, tuple_weights)

            if eMi < 0.5:# and (errors_Mi != [] and eMi < min(errors_Mi)):
                ensemble_classifiers.append(Mi)
                errors_Mi.append(eMi)
                break

            run_emis.append(eMi)
            run_classifiers.append(Mi)
            run_predictions.append(predicted_class)
            run_training_class.append(new_training_class)

            current_run += 1
            #
            # if current_run == max_run:
            #     eMi = max(run_emis)
            #     min_run_id = run_emis.index(eMi)
            #     ensemble_classifiers.append(run_classifiers[min_run_id])
            #     predicted_class = run_predictions[min_run_id]
            #     new_training_class = run_training_class[min_run_id]
            #     errors_Mi.append(eMi)
            #     break

        print eMi

        new_tuple_weights = assignNewTupleWeights(tuple_weights, eMi, predicted_class, new_training_class)
        if new_tuple_weights == None:
            rk -= 1
            continue
        tuple_weights = new_tuple_weights
        #tuple_ids = makeNewTupleIds(tuple_weights, num_training_data)

    return ensemble_classifiers, errors_Mi