def test_sum():

    # define rbf and dot product custom kernels, and set hyperparameters
    custom_rbf_kernel = generate_kernel(rbf, rbf_grad)
    custom_dot_prod_kernel = generate_kernel(dot_prod, dot_prod_grad)

    sum_custom_kernel = Sum(custom_dot_prod_kernel, custom_rbf_kernel)
    model = GaussianProcessRegressor(kernel=sum_custom_kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)

    sum_kernel = Sum(DotProduct(), RBF())
    model = GaussianProcessRegressor(kernel=sum_kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds2 = model.predict(X_test)

    assert (np.all(np.isclose(preds1, preds2)))
def test_kernel_no_params():
    # test a kernel with no parameters
    def cov(x, y):
        return np.exp(-np.linalg.norm(x - y))

    kernel = generate_kernel(cov)

    # predict with a model using the above kernel
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds = model.predict(X_test)

    assert (len(preds) == 10)
def test_product():

    # define rbf and dot product custom kernels, and set hyperparameters
    custom_rbf_kernel = generate_kernel(rbf,
                                        rbf_grad,
                                        length_scale=1,
                                        length_scale_bounds=(1e-5, 1e5))
    custom_dot_prod_kernel = generate_kernel(dot_prod,
                                             dot_prod_grad,
                                             sigma=1,
                                             sigma_bounds=(1e-5, 1e5))

    prod_custom_kernel = Product(custom_dot_prod_kernel, custom_rbf_kernel)
    model = GaussianProcessRegressor(kernel=prod_custom_kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)

    prod_kernel = Product(DotProduct(), RBF())
    model = GaussianProcessRegressor(kernel=prod_kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds2 = model.predict(X_test)

    assert (np.all(np.isclose(preds1, preds2)))
def test_generate_kernel():

    kernel = generate_kernel(rbf,
                             rbf_grad,
                             length_scale=1,
                             length_scale_bounds=(1e-5, 1e5),
                             metric=distance.jaccard)
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    print(model.predict(X_test))

    kernel = generate_kernel(rbf,
                             rbf_grad,
                             length_scale_bounds=(1e-5, 1e5),
                             metric=distance.jaccard)
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    print(model.predict(X_test))

    kernel = generate_kernel(rbf,
                             rbf_grad,
                             length_scale=1,
                             metric=distance.sqeuclidean)
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    print(model.predict(X_test))

    kernel = generate_kernel(rbf, rbf_grad, length_scale=1)
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    print(model.predict(X_test))

    kernel = generate_kernel(rbf, rbf_grad)
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    print(model.predict(X_test))
def test_exponentiation():

    # define an rbf custom kernel, and set hyperparameters
    custom_rbf_kernel = generate_kernel(rbf,
                                        rbf_grad,
                                        length_scale=1,
                                        length_scale_bounds=(1e-5, 1e5))

    exp_custom_kernel = Exponentiation(custom_rbf_kernel, 5)
    model = GaussianProcessRegressor(kernel=exp_custom_kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)

    exp_kernel = Exponentiation(RBF(), 5)
    model = GaussianProcessRegressor(kernel=exp_kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds2 = model.predict(X_test)

    assert (np.all(np.isclose(preds1, preds2)))
def test_constant():
    # define a custom kernel with the constant covariance function, and set hyperparameters
    kernel = generate_kernel(constant,
                             constant_grad,
                             const=1,
                             const_bounds=(1e-5, 1e5))

    # create a GPR with the above kernel and use it on the data
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)

    # do the same with a ConstantKernel, with the same parameters
    kernel = ConstantKernel()

    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds2 = model.predict(X_test)

    # assert that the two give almost the same predictions
    assert (np.all(np.isclose(preds1, preds2)))
def test_exp_sin_sq():

    # define a custom kernel with the exponential sin squared covariance function
    kernel = generate_kernel(exp_sin_sq,
                             exp_sin_sq_grad,
                             length_scale=1,
                             length_scale_bounds=(1e-5, 1e5),
                             periodicity=1,
                             periodicity_bounds=(1e-5, 1e5))

    # create a GPR with the above kernel and use it on the data
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)

    # do the same with an RBF, with the same length_scale and bounds
    kernel = ExpSineSquared()

    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds2 = model.predict(X_test)
def test_dot_prod():

    # define a custom kernel with the dot product covariance function, and set hyperparameters
    kernel = generate_kernel(dot_prod,
                             dot_prod_grad,
                             sigma=1,
                             sigma_bounds=(1e-5, 1e5))

    # create a GPR with the above kernel and use it on the data
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)

    # do the same with an RBF, with the same length_scale and bounds
    kernel = DotProduct()

    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds2 = model.predict(X_test)

    # assert that the two give almost the same predictions
    assert (np.all(np.isclose(preds1, preds2)))
Exemple #9
0
def test_sandbox():

    kernel = generate_kernel(rbf, rbf_grad)
    model = GaussianProcessRegressor(kernel=kernel, random_state=0)
    model.fit(X_train, Y_train)
    preds1 = model.predict(X_test)