def test_pipeline_glm(make_gaus_data): X, y, w = make_gaus_data glm = GeneralisedLinearModel(Gaussian(), LinearBasis(onescol=True), random_state=randstate) estimators = [('PCA', PCA()), ('SLM', glm) ] pipe = Pipeline(estimators) pipe.fit(X, y) Ey = pipe.predict(X) assert smse(y, Ey) < 0.1
def test_gridsearch_glm(make_gaus_data): X, y, Xs, ys = make_gaus_data glm = GeneralizedLinearModel(Gaussian(), LinearBasis(onescol=True), random_state=1, maxiter=100) param_dict = {'batch_size': [10, 20]} estimator = GridSearchCV(glm, param_dict, verbose=1, n_jobs=-1) estimator.fit(X, y) Ey = estimator.predict(Xs) assert len(ys) == len(Ey) # we just want to make sure this all runs
def test_glm(make_data): X, y, w = make_data basis = LinearBasis(onescol=False) lhood = Gaussian() params = glm.learn(X, y, lhood, [1.], basis, []) Ey, _, _, _ = glm.predict_meanvar(X, lhood, basis, *params) assert rsquare(Ey, y) > 0.9 basis = LinearBasis(onescol=False) + RandomRBF(nbases=10, Xdim=X.shape[1]) params = glm.learn(X, y, lhood, [1.], basis, [1.]) Ey, _, _, _ = glm.predict_meanvar(X, lhood, basis, *params) assert rsquare(Ey, y) > 0.9
def __init__(self, onescol=True, var=1., regulariser=1., maxiter=3000, batch_size=10, alpha=0.01, beta1=0.9, beta2=0.99, epsilon=1e-8, random_state=None, nstarts=500): basis = LinearBasis(onescol=onescol, regularizer=Parameter(regulariser, Positive())) super().__init__(likelihood=Gaussian(Parameter(var, Positive())), basis=basis, maxiter=maxiter, batch_size=batch_size, updater=Adam(alpha, beta1, beta2, epsilon), random_state=random_state, nstarts=nstarts)
def __init__(self, kernel='rbf', nbases=50, lenscale=1., var=1., regulariser=1., ard=True, maxiter=3000, batch_size=10, alpha=0.01, beta1=0.9, beta2=0.99, epsilon=1e-8, random_state=None, nstarts=500): super().__init__(likelihood=Gaussian(Parameter(var, Positive())), basis=None, maxiter=maxiter, batch_size=batch_size, updater=Adam(alpha, beta1, beta2, epsilon), random_state=random_state, nstarts=nstarts) self._store_params(kernel, regulariser, nbases, lenscale, ard)
def __init__(self, lenscale=1., var_init=Parameter(1., Positive())): self.params = var_init self.gaus = Gaussian(var_init) self.unif = UnifGauss(lenscale)
# Log output to the terminal attached to this notebook logging.basicConfig(level=logging.INFO) # Load the data boston = load_boston() X = boston.data y = boston.target - boston.target.mean() folds = 5 (tr_ind, ts_ind) = list(KFold(len(y), n_folds=folds, shuffle=True))[0] # Make Basis and Likelihood N, D = X.shape lenscale = 10. nbases = 50 lenARD = lenscale * np.ones(D) lenscale_init = Parameter(lenARD, Positive()) base = LinearBasis(onescol=True) + RandomMatern32( Xdim=D, nbases=nbases, lenscale_init=lenscale_init) like = Gaussian() # Fit and predict the model glm = GeneralisedLinearModel(like, base, maxiter=6000) glm.fit(X[tr_ind], y[tr_ind]) Ey, Vy = glm.predict_moments(X[ts_ind]) # Score y_true = y[ts_ind] print("SMSE = {}, MSLL = {}".format(smse(y_true, Ey), msll(y_true, Ey, Vy, y[tr_ind])))