def test_vectors_and_arrays(self): X = np.random.normal(size=(10, 3)) Y = np.random.normal(size=(10, 2)) model = SelectiveRegularization(unpenalized_inds=[0], penalized_model=Ridge(), fit_intercept=True) self.assertEqual(model.fit(X, Y).coef_.shape, (2, 3)) self.assertEqual(model.fit(X, Y[:, 0]).coef_.shape, (3, ))
def test_can_use_sample_weights(self): n = 100 + np.random.choice(500) d = 5 + np.random.choice(20) n_inds = np.random.choice(np.arange(2, d - 2)) inds = np.random.choice(d, n_inds, replace=False) alpha = np.random.uniform(0.5, 1.5) X = np.random.normal(size=(n, d)) y = np.random.normal(size=(n, )) sample_weight = np.random.choice([1, 2], n) # create an extra copy of rows with weight 2 X_aug = X[sample_weight == 2, :] y_aug = y[sample_weight == 2] model = SelectiveRegularization(unpenalized_inds=inds, penalized_model=Ridge(), fit_intercept=True) coef = model.fit(X, y, sample_weight=sample_weight).coef_ coef2 = model.fit(np.vstack((X, X_aug)), np.concatenate( (y, y_aug))).coef_ np.testing.assert_allclose(coef, coef2)
def test_can_pass_through_attributes(self): X = np.random.normal(size=(10, 3)) y = np.random.normal(size=(10, )) model = SelectiveRegularization(unpenalized_inds=[0], penalized_model=LassoCV(), fit_intercept=True) # _penalized_inds is only set during fitting with self.assertRaises(AttributeError): inds = model._penalized_inds # cv exists on penalized model old_cv = model.cv model.cv = 2 model.fit(X, y) # now we can access _penalized_inds assert np.array_equal(model._penalized_inds, [1, 2]) # check that we can read the cv attribute back out from the underlying model assert model.cv == 2