Ejemplo n.º 1
0
def get_sensor():
    row = str(request.args.get('row'))
    pos = str(request.args.get('pos'))
    resp = dynamo_client.get_item(
        TableName='SoilSensor',
        Key={
            'Row': {'S': row},
            'PositionInRow': {'S': pos}
        }
    )
    soil_data = np.array([float(i['N']) for i in resp['Item']['payload']['M']['moistureSensor']['L']]).reshape(-1, 1)
    light_data = np.array([float(i['N']) for i in resp['Item']['payload']['M']['lightSensor']['L']]).reshape(-1, 1)
    x = np.arange(len(soil_data)).reshape(-1, 1)
    predictive_range = np.arange(len(soil_data), len(soil_data) + 72).reshape(-1, 1)

    soil_kernel = kernels.ExpSineSquared()
    soil_gp = GaussianProcessRegressor(kernel=soil_kernel)
    soil_gp.fit(x, soil_data)
    print(f"Soil MLL: {soil_gp.log_marginal_likelihood_value_}")
    predicted_soil = soil_gp.predict(predictive_range)

    light_kernel = kernels.ExpSineSquared()
    light_gp = GaussianProcessRegressor(kernel=light_kernel)
    light_gp.fit(x, light_data)
    print(f"Light MLL: {light_gp.log_marginal_likelihood_value_}")
    predicted_light = light_gp.predict(predictive_range)
    resp = {
        "soil_observed": soil_data.flatten().tolist(),
        "soil_predicted": predicted_soil.flatten().tolist(),
        "light_observed": light_data.flatten().tolist(),
        "light_predicted": predicted_light.flatten().tolist()
    }

    return json.dumps(resp, indent=4)
Ejemplo n.º 2
0
def make_exp_sine_squared(n_dim_obs=5, n_dim_lat=0, T=1, **kwargs):
    """Make precision matrices using a temporal sine kernel."""
    from regain.bayesian.gaussian_process_ import sample as samplegp
    from sklearn.gaussian_process import kernels

    L, K_HO = make_ell(n_dim_obs, n_dim_lat)

    periodicity = kwargs.get("periodicity", np.pi)
    length_scale = kwargs.get("length_scale", 2)
    epsilon = kwargs.get("epsilon", 0.5)
    sparse = kwargs.get("sparse", True)
    temporal_kernel = kernels.ExpSineSquared(periodicity=periodicity,
                                             length_scale=length_scale)(
                                                 np.arange(T)[:, None])

    u = samplegp(temporal_kernel, p=n_dim_obs * (n_dim_obs - 1) // 2)[0]
    K, K_obs = [], []
    for uu in u.T:
        theta = squareform(uu)

        if sparse:
            # sparsify
            theta[np.abs(theta) < epsilon] = 0

        theta += np.diag(np.sum(np.abs(theta), axis=1) + 0.01)
        K.append(theta)

        assert is_pos_def(theta)
        theta_observed = theta - L
        assert is_pos_def(theta_observed)
        K_obs.append(theta_observed)

    thetas = np.array(K)
    return thetas, np.array(K_obs), np.array([L] * T)
def main():
    # Specify type_ and region of data
    type_ = 'organic'
    region = 'WestTexNewMexico'

    # Specify the kernel functions; please see the paper for the rationale behind the choices
    kernel = Kernels.ExpSineSquared(length_scale=20., periodicity=365.) \
        + 0.8 * Kernels.RationalQuadratic(alpha=20., length_scale=80.) \
        + Kernels.WhiteKernel(.2)

    # Fit gp model and plot
    run_gp(kernel, n_restarts_optimizer=10, type_=type_, region=region)
Ejemplo n.º 4
0
 'k_Constant':
 lambda: kers.ConstantKernel(),
 'k_WN':
 lambda: kers.WhiteKernel(),
 'k_RBF':
 lambda: kers.RBF(),
 'k_RQ':
 lambda: kers.RationalQuadratic(),
 'k_mat0':
 lambda: kers.Matern(nu=0.5),
 'k_mat1':
 lambda: kers.Matern(nu=1.5),
 'k_mat2':
 lambda: kers.Matern(nu=2.5),
 'k_sine':
 lambda: kers.ExpSineSquared(),
 #now combination kernels
 'k1':
 lambda: kers.Sum(kers.ConstantKernel(), kers.ExpSineSquared()),
 'k2':
 lambda: kers.Product(kers.ConstantKernel(), kers.ExpSineSquared()),
 'k3':
 lambda: kers.Sum(
     kers.ConstantKernel(),
     kers.Product(kers.ConstantKernel(), kers.ExpSineSquared())),
 'k4':
 lambda: kers.Sum(kers.ConstantKernel(),
                  kers.Product(kers.RBF(), kers.ExpSineSquared())),
 'k5':
 lambda: kers.Sum(
     kers.Product(kers.ConstantKernel(), kers.RBF()),
Ejemplo n.º 5
0
def _periodic_kernel(X, Y=None, inverse_width=1):
    return kernels.ExpSineSquared(length_scale=inverse_width)(X, Y=Y)