def test_coxreg_serialize_and_compare(self): """Test serialization (cereal/pickle) of Cox Regression.""" np.random.seed(123) n_samples, n_features = 100, 5 w0 = np.random.randn(n_features) features, times, censoring = SimuCoxReg( w0, n_samples=n_samples, verbose=False, seed=1234).simulate() model = ModelCoxRegPartialLik() model.fit(features, times, censoring) pickled = pickle.loads(pickle.dumps(model)) self.assertTrue(model._model.compare(pickled._model))
def test_set_model(self): """...Test set_model of saga, should only accept childs of ModelGeneralizedLinear""" # We try to pass a ModelCoxRegPartialLik which is not a generalized # linear model to SAGA to check that the error is raised msg = '^SAGA accepts only childs of `ModelGeneralizedLinear`$' with self.assertRaisesRegex(ValueError, msg): w = weights_sparse_gauss(n_weights=2, nnz=0) X, T, C = SimuCoxReg(w).simulate() model = ModelCoxRegPartialLik().fit(X, T, C) SAGA().set_model(model) msg = '^SAGA accepts only childs of `ModelGeneralizedLinear`$' with self.assertRaisesRegex(RuntimeError, msg): w = weights_sparse_gauss(n_weights=2, nnz=0) X, T, C = SimuCoxReg(w).simulate() model = ModelCoxRegPartialLik().fit(X, T, C) saga = SAGA() saga._solver.set_model(model._model)
def test_ModelCoxRegPartialLik(self): """...Numerical consistency check of loss and gradient for Cox Regression """ np.random.seed(123) n_samples, n_features = 100, 5 w0 = np.random.randn(n_features) features, times, censoring = SimuCoxReg( w0, n_samples=n_samples, verbose=False, seed=1234).simulate() model = ModelCoxRegPartialLik() model.fit(features, times, censoring) model_spars = ModelCoxRegPartialLik() model_spars.fit(csr_matrix(features), times, censoring) self.run_test_for_glm(model, model_spars)