Python sklearn.gaussian_process.kernels.WhiteKernel() Examples

The following are 5 code examples of sklearn.gaussian_process.kernels.WhiteKernel(). 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_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 #2
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 #3
Source File: learners.py    From M-LOOP with MIT License 5 votes vote down vote up
def create_gaussian_process(self):
        '''
        Create the initial Gaussian process.
        '''
        if self.cost_has_noise:
            gp_kernel = skk.RBF(length_scale=self.length_scale) + skk.WhiteKernel(noise_level=self.noise_level)
        else:
            gp_kernel = skk.RBF(length_scale=self.length_scale)
        if self.update_hyperparameters:
            self.gaussian_process = skg.GaussianProcessRegressor(kernel=gp_kernel,n_restarts_optimizer=self.hyperparameter_searches)
        else:
            self.gaussian_process = skg.GaussianProcessRegressor(kernel=gp_kernel,optimizer=None) 
Example #4
Source File: calibration_gaussian_emulator.py    From CityEnergyAnalyst with MIT License 4 votes vote down vote up
def gaussian_emulator(locator, config):
    """
    Thi is a Gaussian process linear emulator. It is used to create a surrogate model of CEA whose
    output is either rmse or cvrmse

    for more details on the work behind this please check:
    Rysanek A., Fonseca A., Schlueter, A. Bayesian calibration of Dyanmic building Energy Models. Applied Energy 2017.

    :param locator: pointer to location of CEA files
    :param samples: matrix m x n with samples simulated from CEA. m are the number of input variables [0,1]
    :param cv_rmse: array with results of cv_rmse after running n samples.
    :param building_name: name of building whose calibration process is being acted upon
    :return:
           file with database of emulator stored in locator.get_calibration_cvrmse_file(building_name)

    """
    # INITIALIZE TIMER
    t0 = time.clock()

    # Local variables
    building_name = config.single_calibration.building
    building_load = config.single_calibration.load
    with open(locator.get_calibration_problem(building_name, building_load),'r') as input_file:
        problem = pickle.load(input_file)
    samples_norm = problem["samples_norm"]
    target = problem["cv_rmse"]

    # Kernel with parameters given in GPML book for the gaussian surrogate models. The hyperparameters are optimized so you can get anything here.
    k1 = 5**2 * RBF(length_scale=1e-5)  # long term smooth rising trend RBF: radio basis functions (you can have many, this is one).
    k2 = 5**2 * RBF(length_scale=0.000415) * ExpSineSquared(length_scale=3.51e-5, periodicity=0.000199)  # seasonal component
    # medium term irregularity
    k3 = 316**2 * RationalQuadratic(length_scale=3.54, alpha=1e+05)
    k4 = 316**2 * RBF(length_scale=4.82) + WhiteKernel(noise_level=0.43)  # noise terms
    kernel = k1 + k2 + k3 + k4

    # give the data to the regressor.
    gp = GaussianProcessRegressor(kernel=kernel, alpha=1e-7, normalize_y=True, n_restarts_optimizer=2)
    gp.fit(samples_norm, target) # then fit the gp to your observations and the minmax. It takes 30 min - 1 h.

    # this is the result
    joblib.dump(gp, locator.get_calibration_gaussian_emulator(building_name, building_load))

    time_elapsed = time.clock() - t0
    print('done - time elapsed: %d.2f seconds' % time_elapsed) 
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