Python sklearn.gaussian_process.kernels.DotProduct() Examples

The following are 5 code examples of sklearn.gaussian_process.kernels.DotProduct(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.gaussian_process.kernels , or try the search function .
Example #1
Source File: test_gpr.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_gpr_correct_error_message():
    X = np.arange(12).reshape(6, -1)
    y = np.ones(6)
    kernel = DotProduct()
    gpr = GaussianProcessRegressor(kernel=kernel, alpha=0.0)
    assert_raise_message(np.linalg.LinAlgError,
                         "The kernel, %s, is not returning a "
                         "positive definite matrix. Try gradually increasing "
                         "the 'alpha' parameter of your "
                         "GaussianProcessRegressor estimator."
                         % kernel, gpr.fit, X, y) 
Example #2
Source File: test_learn.py    From CatKit with GNU General Public License v3.0 5 votes vote down vote up
def test_learning(self):
        """General testing for catkit.learn"""
        with FingerprintDB(data_path) as fp:
            X = fp.get_fingerprints(params=np.arange(20) + 1)
            y = fp.get_fingerprints(params=['Ef']).T[0]

        X0, X1, y0, y1 = train_test_split(X, y, test_size=0.6, shuffle=True)

        kernel = DotProduct() + WhiteKernel()
        gp = GaussianProcessRegressor(
            kernel=kernel, optimizer=optimizer,
            n_restarts_optimizer=0, alpha=0)
        gp.fit(X0, y0)

        # This is an ugly way to define default properties
        # The 3rd argument is for a certain number of global
        # optimization steps using the basinhopping algorithm
        optimizer.__defaults__ = (True, 'L-BFGS-B', 3)

        gp = GaussianProcessRegressor(
            kernel=kernel, optimizer=optimizer,
            n_restarts_optimizer=0, alpha=0)
        gp.fit(X0, y0)

        samples = online_learning(
            X, y, [0, 1, 2], factors=[1, 1], nsteps=3, plot=True)

        test_array = np.array([0, 1, 2, 24, 15, 23])
        np.testing.assert_allclose(samples, test_array) 
Example #3
Source File: test_regression.py    From fklearn with Apache License 2.0 5 votes vote down vote up
def test_gp_regression_learner():
    df_train = pd.DataFrame({
        'id': ["id1", "id2", "id3", "id4"],
        'x1': [10.0, 13.0, 10.0, 13.0],
        "x2": [0, 1, 1, 0],
        'y': [2.3, 4.0, 100.0, -3.9]
    })

    df_test = pd.DataFrame({
        'id': ["id4", "id4", "id5", "id6"],
        'x1': [12.0, 1000.0, -4.0, 0.0],
        "x2": [1, 1, 0, 1],
        'y': [1.3, -4.0, 0.0, 49]
    })

    from sklearn.gaussian_process.kernels import RBF, WhiteKernel, DotProduct

    kernel = RBF() + WhiteKernel() + DotProduct()

    learner = gp_regression_learner(features=["x1", "x2"],
                                    target="y",
                                    kernel=kernel,
                                    alpha=0.1,
                                    extra_variance="fit",
                                    return_std=True,
                                    extra_params=None,
                                    prediction_column="prediction")

    predict_fn, pred_train, log = learner(df_train)

    pred_test = predict_fn(df_test)

    expected_col_train = df_train.columns.tolist() + ["prediction", "prediction_std"]
    expected_col_test = df_test.columns.tolist() + ["prediction", "prediction_std"]

    assert Counter(expected_col_train) == Counter(pred_train.columns.tolist())
    assert Counter(expected_col_test) == Counter(pred_test.columns.tolist())
    assert (pred_test.columns == pred_train.columns).all()
    assert "prediction" in pred_test.columns 
Example #4
Source File: test_gpr.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_gpr_correct_error_message():
    X = np.arange(12).reshape(6, -1)
    y = np.ones(6)
    kernel = DotProduct()
    gpr = GaussianProcessRegressor(kernel=kernel, alpha=0.0)
    assert_raise_message(np.linalg.LinAlgError,
                         "The kernel, %s, is not returning a "
                         "positive definite matrix. Try gradually increasing "
                         "the 'alpha' parameter of your "
                         "GaussianProcessRegressor estimator."
                         % kernel, gpr.fit, X, y) 
Example #5
Source File: learn.py    From CatKit with GNU General Public License v3.0 4 votes vote down vote up
def online_learning(X, y, samples, factors=[1.0, 1.0], nsteps=40, plot=False):
    """A simple utility for performing online learning. The main
    components required are a regression method and a scoring
    technique.

    Currently, the scoring methodology and regressor are baked in.
    These need to be made modular.

    Minimum 3 samples are required for 3 fold cross validation.
    """
    ids = np.arange(len(y))

    kernel = DotProduct() + WhiteKernel()
    regressor = GaussianProcessRegressor(
        kernel=kernel, n_restarts_optimizer=5, alpha=0)

    step = 0
    while step < nsteps:
        X0 = X[samples]
        y0 = y[samples]

        regressor.fit(X0, y0)
        yp, ys = regressor.predict(X, return_std=True)

        # Provides some form of normalization.
        # Multiples denote relative importance
        yp_scale = preprocessing.scale(yp) * factors[0]
        ys_scale = preprocessing.scale(ys) * factors[1]
        score = ys_scale - yp_scale
        srt = np.argsort(score)[::-1]

        for s in srt:
            if s not in samples:
                samples = np.concatenate([samples, [s]])
                break

        if plot:
            mae = np.round(mean_absolute_error(yp, y), 3)
            n = len(samples)

            fig, ax = plt.subplots(figsize=(6, 4))
            ax.plot(ids, y, 'o', zorder=0)
            ax.errorbar(ids, yp, yerr=ys, fmt='o', zorder=1)
            ax.plot(samples, y[samples], 'o', zorder=3)
            xlim = ax.get_xlim()
            ylim = ax.get_ylim()
            ax.text(xlim[0] / 9.0, ylim[0] / 9.0, mae)
            plt.tight_layout()
            plt.savefig('./online-learning-RBF-{}.png'.format(n))
            plt.close()

        step += 1

    return samples