Exemplo n.º 1
0
def generate_heatmap(ax: Axes,
                     population_size: [int],
                     mutation_rate: [float],
                     ast: AST,
                     acceptable: float,
                     name_of_problem: str,
                     name_of_file: str,
                     values: [float] = None) -> None:
    """
    Generate heatmap to compare performance in function of population size and mutation rate

    :param ax: An Axes
    :param population_size: Population sizes to use
    :param mutation_rate: Mutation rates to use
    :param ast: AST initialized
    :param acceptable: Acceptable difference to reach
    :param name_of_problem: Name of problem of heatmap generated
    :param name_of_file: Name of file
    :param values: (Optional) in case its needed
    :return: None, alter ax
    """
    logging.basicConfig(level=logging.INFO)

    matrix = list()

    for size in population_size:
        logging.info("Population size used: {}".format(size))
        generations = list()
        for m_rate in mutation_rate:
            logging.info("==> Mutation rate used: {}".format(m_rate))
            ast_to_be_used = deepcopy(ast)
            ast_to_be_used.mutation_rate = m_rate
            ast_to_be_used = ast_to_be_used.generate_individual()
            environment = GAEngine(ast_to_be_used)
            if values is not None:
                result = environment.run_to_reach(0,
                                                  acceptable,
                                                  size,
                                                  values=values)
            else:
                result = environment.run_to_reach(0, acceptable, size)
            generations.append(result.get_generations()[-1])
        matrix.append(generations.copy())

    matrix = np.array(matrix)
    mut = mutation_rate.copy()
    pop = population_size.copy()
    mut[0] = "Mutation rate:\n{}".format(mut[0])
    pop[0] = "Population\nsize:\n{}".format(pop[0])
    show_matrix(ax,
                matrix, (mut, pop),
                "{}: Number of iteration\n".format(name_of_problem),
                20,
                25,
                color_map="Reds")

    plt.savefig("../results/heatmap_{}.png".format(name_of_file))
Exemplo n.º 2
0
            c = "r"

        line2 = ax2.plot(costs,
                         label="MSE{}".format(iteration),
                         linestyle="--",
                         linewidth=2.5,
                         c=c)
        lines = lines + line + line2

    ax.set_ylabel("Learning Curve", fontsize=FONT_SIZE)
    ax.set_xlabel("Epochs", fontsize=FONT_SIZE)
    ax.set_title("{} Network on Iris\n".format(type_net), fontsize=TITLE_SIZE)
    ax.grid()

    ax2.set_ylabel("Cost", fontsize=FONT_SIZE)
    ax2.grid()

    labels = [l.get_label() for l in lines]
    ax2.legend(lines, labels, fontsize=FONT_SIZE, loc="center right")

    show_matrix(ax3, c_m,
                (classes, ["Predicted\n{}".format(iris) for iris in classes]),
                "Confusion Matrix of Test Set\n", FONT_SIZE, TITLE_SIZE)

    print("Accuracy:\t{}".format(accuracy(c_m)))
    print("Precision:\t{}".format(precision(c_m)))
    print("Recall:\t{}".format(recall(c_m)))
    print("f1-score:\t{}".format(f1_score(c_m)))

    plt.savefig("../results/{}_on_iris{}.png".format(filename, k_fold))
            result = environment.run_to_equilibrium(size, EQUILIBRIUM)
            generations.append(result.get_generations()[-1])
            wights.append(result.individual.multi_fitness[0])
            values.append(result.individual.multi_fitness[1])
        matrix1.append(generations.copy())
        matrix2.append(wights)
        matrix3.append(values)

    matrix1 = np.array(matrix1)
    matrix2 = np.array(matrix2)
    matrix3 = np.array(matrix3)
    pop = [50, 150, 250, 350, 450]
    mut = [0, 0.05, 0.15, 0.3, 0.7]
    show_matrix(ax2,
                matrix2, (mut, pop),
                "Weight in knapsack\n",
                20,
                25,
                color_map="Blues")
    show_matrix(ax3,
                matrix3, (mut, pop),
                "Value in knapsack\n",
                20,
                25,
                color_map="Greens")

    mut[0] = "Mutation rate:\n{}".format(mut[0])
    pop[0] = "Population\nsize:\n{}".format(pop[0])
    show_matrix(ax1,
                matrix1, (mut, pop),
                "Number of iteration\n",
                20,
Exemplo n.º 4
0
def train_evaluate(architecture: dict, dataset_name: str) -> NeuralNetwork:
    """
    Train and evaluate a Network

    :param architecture: Architecture of NeuralNetwork (above)
    :param dataset_name: Dataset to use
    :return: Trained Neural Network
    """
    # import dataset
    dataset = import_data("../data/{}.data".format(dataset_name))

    dataset = oversample(dataset)
    more_info = "(oversample)"

    labels, encoding = one_hot_encoding(dataset[-1])
    classes = list(encoding.keys())
    dataset = np.delete(dataset, -1, 0)

    dataset = np.delete(dataset, [0], 0)

    # Initialize network
    logging.info("Input size: {}\tOutput size: {}".format(
        dataset.shape[0], len(encoding)))
    network = NeuralNetwork(dataset.shape[0], architecture["INTERNAL_LAYERS"],
                            len(encoding), architecture["ACT_FUNCS"],
                            architecture["LR"])

    # Define Trainer
    trainer = StandardTrainer(dataset, labels.T, TRAIN_SIZE)

    fig = plt.figure(figsize=FIG_SIZE)
    fig.subplots_adjust(wspace=0.3)
    ax = fig.add_subplot(121)
    ax2 = ax.twinx()
    ax3 = fig.add_subplot(122)

    trained, (learn, costs) = trainer.train(network,
                                            epochs=EPOCHS,
                                            repeat=True)

    prediction = trainer.evaluate(trained)

    c_m = confusion_matrix(prediction, trainer.get_labels())

    line = ax.plot(learn, label="Learning Curve", linewidth=2.5, c="b")

    line2 = ax2.plot(costs, label="MSE", linestyle="--", linewidth=2.5, c="r")
    lines = line + line2

    ax.set_ylabel("Learning Curve", fontsize=FONT_SIZE)
    ax.set_xlabel("Epochs", fontsize=FONT_SIZE)
    ax.set_title("Network on {}\n".format(dataset_name), fontsize=TITLE_SIZE)
    ax.grid()

    ax2.set_ylabel("Cost", fontsize=FONT_SIZE)
    ax2.grid()

    labels = [l.get_label() for l in lines]
    ax2.legend(lines, labels, fontsize=FONT_SIZE, loc="center right")

    show_matrix(
        ax3, c_m,
        (classes, ["Predicted\n{}".format(a_class) for a_class in classes]),
        "Confusion Matrix of Test Set\n", FONT_SIZE, TITLE_SIZE)

    measures = {
        "accuracy": accuracy(c_m),
        "precision": precision(c_m),
        "recall": recall(c_m),
        "f1_score": f1_score(c_m)
    }

    print("Summary on {}:\n".format(dataset))
    report_results(c_m)

    plt.savefig("../results/Network_on_{}{}.png".format(
        dataset_name, more_info))

    return trained
Exemplo n.º 5
0
X_MAX = Y_MAX = 50
FIG_SIZE = (28, 12)
FONT_SIZE = 20
TITLE_SIZE = 30
np.random.seed(2)

if __name__ == '__main__':
    dataset = import_data("../../data/iris.data")[4]
    classes = np.unique(dataset)
    labels, _ = one_hot_encoding(dataset)
    prediction, _ = one_hot_encoding(
        np.random.choice(["a", "b", "c"], size=labels.shape[0], replace=True))
    matrix = confusion_matrix(prediction.T, labels.T)
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=FIG_SIZE)
    show_matrix(ax1, matrix, ([classes[0], classes[1], classes[2]], [
        "Predicted\n" + classes[0], "Predicted\n" + classes[1],
        "Predicted\n" + classes[2]
    ]), "Confusion matrix of a iris dataset\n", FONT_SIZE, TITLE_SIZE)
    measures = np.zeros((3, 4))
    ax2.matshow(measures, cmap="Greys")
    to_show = np.zeros((3, 4))
    to_show[0][0] = round(accuracy(matrix), 4)
    to_show[1][0] = np.nan
    to_show[2][0] = np.nan
    _precision = precision(matrix)
    to_show[0][1] = round(_precision[0], 4)
    to_show[1][1] = round(_precision[1], 4)
    to_show[2][1] = round(_precision[2], 4)
    _recall = recall(matrix)
    to_show[0][2] = round(_recall[0], 4)
    to_show[1][2] = round(_recall[1], 4)
    to_show[2][2] = round(_recall[2], 4)
POPULATION_SIZE = [10, 50, 100, 300, 500, 1000]
MUTATION_RATE = [0, 0.1, 0.2, 0.3, 0.4]

seed(math.log(2))

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    _, ax = plt.subplots(figsize=(12, 13))
    matrix = list()
    for size in POPULATION_SIZE:
        logging.info("Population size used: {}".format(size))
        generations = list()
        for m_rate in MUTATION_RATE:
            logging.info("==> Mutation rate used: {}".format(m_rate))
            environment = GAEngine(WordGuesser(m_rate, WORD_TO_GUESS))
            result = environment.run_to_reach(SCORE, 0.0, size)
            generations.append(result.get_generations()[-1])
        matrix.append(generations.copy())
    matrix = np.array(matrix)
    mut = MUTATION_RATE.copy()
    mut[0] = "Mutation rate: {}".format(mut[0])
    pop = POPULATION_SIZE.copy()
    pop[0] = "Population\nsize:\n{}".format(pop[0])
    show_matrix(ax,
                matrix, (mut, pop),
                "Word guesser (word: algorithm)\n",
                20,
                25,
                color_map="Reds")
    plt.savefig("../results/algorithm_heatmap.png")
Exemplo n.º 7
0
N = int(1e5)
X_MIN = Y_MIN = -50
X_MAX = Y_MAX = 50
FIG_SIZE = (24, 12)
FONT_SIZE = 20
TITLE_SIZE = 30
np.random.seed(2)


if __name__ == '__main__':
    labels = Circle(30, (0, 0), (X_MIN, X_MAX), (Y_MIN, Y_MAX)).training_set(N)[2]
    prediction = np.abs(np.random.randn(N))
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=FIG_SIZE)
    matrix = confusion_matrix(prediction, labels)
    show_matrix(ax1, matrix, (["Outside Circle", "Inside Circle"],
                ["Predicted\nOutside", "Predicted\nInside"]), "Confusion matrix of a circle\n",
                FONT_SIZE, TITLE_SIZE, add_label=np.array([["TP: ", "FN: "], ["FP: ", "TN: "]]))
    measures = np.zeros((2, 4))
    ax2.matshow(measures, cmap="Greys")
    to_show = np.zeros((2, 4))
    to_show[0][0] = round(accuracy(matrix), 4)
    _precision = precision(matrix)
    to_show[0][1] = round(_precision[0], 4)
    to_show[1][1] = round(_precision[1], 4)
    _recall = recall(matrix)
    to_show[0][2] = round(_recall[0], 4)
    to_show[1][2] = round(_recall[1], 4)
    _f1_score = f1_score(matrix)
    to_show[0][3] = round(_f1_score[0], 4)
    to_show[1][3] = round(_f1_score[1], 4)
    annotate(ax2, np.array(to_show), 25, np.array([["Accuracy:\n", "Precision\noutside:\n", "Recall\noutside:\n",