Ejemplo n.º 1
0
def plot_steps(learner=None,
               data=None,
               criterion="sum-of-squares",
               step=1,
               num_steps=1000,
               log_scale=True,
               label=""):
    """
    plots the training and test error for a learner.
    data is the 
    learner_class is the class of the learning algorithm
    criterion gives the evaluation criterion plotted on the y-axis
    step specifies how many steps are run for each point on the plot
    num_steps is the number of points to plot
    
    """
    plt.ion()
    plt.xlabel("step")
    plt.ylabel("Average " + criterion + " error")
    if log_scale:
        plt.xscale('log')  #plt.semilogx()  #Makes a log scale
    else:
        plt.xscale('linear')
    if data is None:
        data = Data_from_file('data/holiday.csv',
                              num_train=19,
                              target_index=-1)
        #data = Data_from_file('data/SPECT.csv', target_index=0)
        # data = Data_from_file('data/mail_reading.csv', target_index=-1)
        # data = Data_from_file('data/carbool.csv', target_index=-1)
    random.seed(None)  # reset seed
    if learner is None:
        learner = Linear_learner(data)
    train_errors = []
    test_errors = []
    for i in range(1, num_steps + 1, step):
        test_errors.append(
            data.evaluate_dataset(data.test, learner.predictor, criterion))
        train_errors.append(
            data.evaluate_dataset(data.train, learner.predictor, criterion))
        learner.display(2, "Train error:", train_errors[-1], "Test error:",
                        test_errors[-1])
        learner.learn(num_iter=step)
    plt.plot(range(1, num_steps + 1, step),
             train_errors,
             ls='-',
             c='k',
             label="training errors")
    plt.plot(range(1, num_steps + 1, step),
             test_errors,
             ls='--',
             c='k',
             label="test errors")
    plt.legend()
    plt.pause(0.001)
    plt.show(block=True)
    learner.display(1, "Train error:", train_errors[-1], "Test error:",
                    test_errors[-1])
Ejemplo n.º 2
0
def plot_prediction(
        learner=None,
        data=None,
        minx=0,
        maxx=5,
        step_size=0.01,  # for plotting
        label="function"):
    plt.ion()
    plt.xlabel("x")
    plt.ylabel("y")
    if data is None:
        data = Data_from_file('data/simp_regr.csv',
                              prob_test=0,
                              boolean_features=False,
                              target_index=-1)
    if learner is None:
        learner = Linear_learner(data, squashed=False)
    learner.learning_rate = 0.001
    learner.learn(100)
    learner.learning_rate = 0.0001
    learner.learn(1000)
    learner.learning_rate = 0.00001
    learner.learn(10000)
    learner.display(
        1, "function learned is", learner.predictor_string(), "error=",
        data.evaluate_dataset(data.train, learner.predictor, "sum-of-squares"))
    plt.plot([e[0] for e in data.train], [e[-1] for e in data.train],
             "bo",
             label="data")
    plt.plot(list(arange(minx, maxx, step_size)),
             [learner.predictor([x]) for x in arange(minx, maxx, step_size)],
             label=label)
    plt.legend()
    plt.draw()
Ejemplo n.º 3
0
def plot_polynomials(data=None,
                     learner_class=Linear_learner,
                     max_degree=5,
                     minx=0,
                     maxx=5,
                     num_iter=100000,
                     learning_rate=0.0001,
                     step_size=0.01,   # for plotting
                     ):
    plt.ion()
    plt.xlabel("x")
    plt.ylabel("y")
    if data is None:
        data = Data_from_file('data/simp_regr.csv', prob_test=0,
                              boolean_features=False, target_index=-1)
    plt.plot([e[0] for e in data.train], [e[-1] for e in data.train], "ko", label="data")
    x_values = list(arange(minx, maxx, step_size))
    line_styles = ['-', '--', '-.', ':']
    colors = ['0.5', 'k', 'k', 'k', 'k']
    for degree in range(max_degree):
        data_aug = Data_set_augmented(data, [power_feat(n) for n in range(1, degree + 1)],
                                      include_orig=False)
        learner = learner_class(data_aug, squashed=False)
        learner.learning_rate = learning_rate
        learner.learn(num_iter)
        learner.display(1, "For degree", degree,
                        "function learned is", learner.predictor_string(),
                        "error=", data.evaluate_dataset(data.train, learner.predictor, "sum-of-squares"))
        ls = line_styles[degree % len(line_styles)]
        col = colors[degree % len(colors)]
        plt.plot(x_values, [learner.predictor([x]) for x in x_values], linestyle=ls, color=col,
                 label="degree=" + str(degree))
        plt.legend(loc='upper left')
        plt.draw()
Ejemplo n.º 4
0
def test(**args):
    data = Data_from_file('data/SPECT.csv', target_index=0)
    # data = Data_from_file('data/mail_reading.csv', target_index=-1)
    # data = Data_from_file('data/carbool.csv', target_index=-1)
    learner = Linear_learner(data, **args)
    learner.learn()
    print("function learned is", learner.predictor_string())
    for ecrit in Data_set.evaluation_criteria:
        test_error = data.evaluate_dataset(data.test, learner.predictor, ecrit)
        print("    Average", ecrit, "error is", test_error)