Exemplo n.º 1
0
def test_log_ML_from_lambda():
    g = 0.0
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel())
    model2 = gpmodel.GPRegressor(gpkernel.LinearKernel())
    X, mask = model._regularize(X_df, gamma=g, y=Y)
    model2.fit(X, Y)
    neg_ML = model._log_ML_from_gamma(g, X, Y)
    assert np.isclose(neg_ML, model2.ML)
Exemplo n.º 2
0
def test_predict():
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel(), gamma=-2)
    model.fit(X_df, Y)
    np.random.seed(1)
    X_test = np.random.random(size=(1, n_dims))
    X_test = pd.DataFrame(X_test, index=['A'])
    X_masked = X_test.transpose()[model._mask].transpose()
    Y_test = model.predict(X_test)
    check_model = gpmodel.GPRegressor(gpkernel.LinearKernel())
    check_model.fit(model.X, Y)
    Y_check = check_model.predict(X_masked)
    assert np.isclose(Y_test, Y_check).all()
Exemplo n.º 3
0
def test_linear_kernel():
    kern = gpkernel.LinearKernel()
    params = np.random.random(size=(1, ))
    vp = params[0]
    actual = X @ X.T * vp
    assert np.allclose(kern.cov(X, X, params), actual)
    assert np.allclose(kern.cov(xa, xb, params), actual[0, 1])
    assert np.allclose(kern.cov(X[0:2], X[2:], params),
                       actual[0:2, 2:])
    assert np.allclose(kern.cov(X, X), X @ X.T)
    n = kern.fit(X)
    assert n == 1
    assert np.allclose(kern.cov(hypers=params), actual)
Exemplo n.º 4
0
def test_regularize():
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel())
    pytest.raises(ValueError, model._regularize, X_df, gamma=0)
    X, mask = model._regularize(X_df, y=Y, gamma=0)
    assert np.isclose(X.values,
                      X_df.transpose()[mask].transpose().values).all()
    clf = linear_model.Lasso(alpha=np.exp(0))
    clf.fit(X_df, Y)
    not_zero = ~np.isclose(clf.coef_, 0)
    assert np.array_equal(not_zero, mask)
    new_mask = np.random.choice([True, False], size=len(X_df.columns))
    X, mask = model._regularize(X_df, mask=new_mask)
    assert np.array_equal(mask, new_mask)
    assert np.isclose(X.values,
                      X_df.transpose()[mask].transpose().values).all()
Exemplo n.º 5
0
from gpmodel import gpmodel

parser = argparse.ArgumentParser()
parser.add_argument('-k', '--kernel', required=True)
parser.add_argument('-type', '--type')
parser.add_argument('-g', '--hypers', nargs='*', required=False, type=float)

args = parser.parse_args()
if args.kernel == '3/2':
    kern = gpkernel.MaternKernel(nu='3/2')
elif args.kernel == '5/2':
    kern = gpkernel.MaternKernel(nu='5/2')
elif args.kernel == 'se':
    kern = gpkernel.SEKernel()
elif args.kernel == 'linear':
    kern = gpkernel.LinearKernel()
else:
    raise ValueError('Invalid kernel.')

if args.type == 'b2':
    np.random.seed(0)
    cov = np.array([[1.2, 0.1], [0.1, 1.2]])
    x1 = np.random.multivariate_normal(np.array([-3.0, -3.0]), cov, size=20)
    x2 = np.random.multivariate_normal(np.array([1.0, 1.0]), cov, size=20)
    x3 = np.random.multivariate_normal(np.array([0.0, 3.0]), cov, size=20)
    x4 = np.random.multivariate_normal(np.array([4.0, -4.0]), cov, size=20)
    X = np.concatenate((x1, x2, x3, x4))
    X = pd.DataFrame(X)
    X.index = [chr(i) for i in range(len(X))]
    X.columns = ['x1', 'x2']
    y1 = [1 for _ in range(40)]
Exemplo n.º 6
0
def test_dump_and_load():
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel(), gamma=-2)
    model.fit(X_df, Y)
    model.dump('test.pkl')
    model_2 = gpmodel.GPRegressor.load('test.pkl')
    assert model.gamma == model_2.gamma
Exemplo n.º 7
0
def test_fit():
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel(), gamma=0.1)
    np.random.seed(1)
    model.fit(X_df, Y)
    assert model.X.shape[1] == 19
    assert np.isclose(model.ML, 26.69784799760495)
Exemplo n.º 8
0
def test_init():
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel(), gamma=1)
    assert model._gamma_0 == 1
    model = gpmodel.LassoGPRegressor(gpkernel.LinearKernel())
    assert model._gamma_0 == 0