Пример #1
0
def main():
    np.random.seed(42)
    X_train = np.array([
        [-3.0],
        [-1.0],
        [0.0],
        [1.0],
        [2.0],
        [4.0],
    ])
    Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.2
    num_test = 10000
    X_test = np.linspace(-5, 5, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test)

    num_trees = 100
    depth_max = 5
    size_min_leaf = 2
    ratio_sampling = 0.8
    replace_samples = False
    num_features = 1
    split_random_location = True

    trees = trees_generic_trees.get_generic_trees(
        X_train, Y_train, num_trees, depth_max, size_min_leaf, ratio_sampling, replace_samples, num_features, split_random_location
    )

    mu, sigma = trees_common.predict_by_trees(X_test, trees)

    utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu, sigma, Y_test, path_save=PATH_SAVE, str_postfix='cos')
Пример #2
0
def main():
    time_start = time.time()

    np.random.seed(42)
    X_train = np.array([
        [-3.0],
        [-1.0],
        [0.0],
        [1.0],
        [2.0],
        [4.0],
    ])
    Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.2
    num_test = 10000
    X_test = np.linspace(-5, 5, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test)

    num_trees = 100
    depth_max = 5
    size_min_leaf = 2
    num_features = 1

    trees = trees_random_forest.get_random_forest(
        X_train, Y_train, num_trees, depth_max, size_min_leaf, num_features
    )

    mu, sigma = trees_common.predict_by_trees(X_test, trees)

    time_end = time.time()
    print('time consumed: {:.4f}'.format(time_end - time_start))

    utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu, sigma, Y_test, path_save=PATH_SAVE, str_postfix='cos')
Пример #3
0
def main(str_cov):
    np.random.seed(42)
    X_train = np.array([
        [-3.0],
        [-1.0],
        [3.0],
        [1.0],
        [2.0],
    ])
    Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.2
    num_test = 200
    X_test = np.linspace(-3, 3, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test)

    nu, mu, sigma, Sigma = tp.predict_with_optimized_hyps(X_train,
                                                          Y_train,
                                                          X_test,
                                                          str_cov=str_cov,
                                                          fix_noise=False,
                                                          debug=True)
    utils_plotting.plot_gp_via_distribution(X_train,
                                            Y_train,
                                            X_test,
                                            mu,
                                            sigma,
                                            Y_test,
                                            path_save=PATH_SAVE,
                                            str_postfix='cos_' + str_cov)
def main():
    num_train = 200
    num_test = 1000
    X_train = np.random.randn(num_train, 1) * 5.0
    Y_train = np.cos(X_train) + 10.0
    X_test = np.linspace(-10, 10, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test) + 10.0

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train,
                                                      Y_train,
                                                      X_test,
                                                      debug=True)
    utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu,
                                            sigma, Y_test,
                                            'test_optimized_many_points')
Пример #5
0
def main(fun_prior, str_prior):
    X_train = np.array([
        [-3.0],
        [-2.0],
        [-1.0],
    ])
    Y_train = np.cos(X_train) + 2.0
    num_test = 200
    X_test = np.linspace(-3, 6, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test) + 2.0

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train,
                                                      Y_train,
                                                      X_test,
                                                      prior_mu=fun_prior)
    utils_plotting.plot_gp_via_distribution(
        X_train, Y_train, X_test, mu, sigma, Y_test, PATH_SAVE,
        'optimized_prior_{}'.format(str_prior))
Пример #6
0
def main():
    X_train = np.array([
        [-3],
        [-1],
        [1],
        [2],
    ])
    Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.1
    num_test = 200
    X_test = np.linspace(-3, 3, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test)
    hyps = {
        'signal': 0.5,
        'lengthscales': 0.5,
        'noise': 0.02,
    }
    mu, sigma, Sigma = gp.predict_with_hyps(X_train, Y_train, X_test, hyps)
    utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu, sigma, Y_test, path_save=PATH_SAVE, str_postfix='cos')
Пример #7
0
def main(scale, str_postfix):
    X_train = np.array([
        [-3.0],
        [-2.0],
        [-1.0],
        [2.0],
        [1.2],
        [1.1],
    ])
    Y_train = np.cos(X_train) * scale
    num_test = 200
    X_test = np.linspace(-3, 3, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test) * scale

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train,
                                                      Y_train,
                                                      X_test,
                                                      fix_noise=False,
                                                      debug=True)
    utils_plotting.plot_gp_via_distribution(
        X_train, Y_train, X_test, mu, sigma, Y_test, PATH_SAVE,
        'test_optimized_{}_y'.format(str_postfix))
Пример #8
0
def test_plot_gp_via_distribution():
    dim_X = 1
    dim_Y = 1
    num_train = 5
    num_test = 10

    X_train = np.zeros((num_train, dim_X))
    Y_train = np.ones((num_train, dim_Y))
    X_test = np.zeros((num_test, dim_X))
    mu = np.zeros((num_test, dim_Y))
    sigma = np.zeros((num_test, dim_Y))
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test, mu,
                                                1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test, mu,
                                                np.arange(0, num_test))
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test, 1,
                                                sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test,
                                                np.arange(0, num_test), sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, 1, mu, sigma)

    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train,
                                                np.arange(0, num_test), mu,
                                                sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, 1, X_test, mu, sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                np.arange(0, num_train),
                                                X_test, mu, sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(1, Y_train, X_test, mu, sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(np.arange(0, num_test),
                                                Y_train, X_test, mu, sigma)

    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(np.zeros((num_train, 2)),
                                                Y_train, X_test, mu, sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train,
                                                np.zeros((num_train, 2)), mu,
                                                sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, np.ones(
            (num_train, 2)), X_test, mu, sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, np.ones((10, 1)),
                                                X_test, mu, sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test,
                                                np.zeros((num_test, 2)), sigma)

    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test, mu,
                                                np.zeros((num_test, 2)))
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test, mu,
                                                np.zeros((11, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train, Y_train, X_test,
                                                np.zeros((11, 1)), sigma)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                Y_test=np.arange(0, num_test))
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                Y_test=np.zeros((num_test, 2)))

    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                Y_test=np.zeros((20, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                Y_test=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                path_save=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                str_postfix=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                str_x_axis=1)

    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                str_y_axis=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                use_tex=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                draw_zero_axis=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                pause_figure='abc')
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                time_pause='abc')

    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                range_shade='abc')
    with pytest.raises(AssertionError) as error:
        package_target.plot_gp_via_distribution(X_train,
                                                Y_train,
                                                X_test,
                                                mu,
                                                sigma,
                                                colors='abc')