コード例 #1
0
 def test_LearnerSCCS_confidence_intervals(self):
     lrn = ConvSCCS(n_lags=self.n_lags, penalized_features=[])
     coeffs, _ = lrn.fit(self.features, self.labels, self.censoring)
     p_features, p_labels, p_censoring = lrn._preprocess_data(
         self.features, self.labels, self.censoring)
     confidence_intervals = lrn._bootstrap(p_features,
                                           p_labels, p_censoring,
                                           np.hstack(coeffs), 5, .90)
     for i, c in enumerate(coeffs):
         self.assertTrue(
             np.all(confidence_intervals.lower_bound[i] <= c),
             "lower bound of the confidence interval\
                                should be <= coeffs at index %i" % i)
         self.assertTrue(
             np.all(c <= confidence_intervals.upper_bound[i]),
             "upper bound of the confidence interval\
                                should be >= coeffs at index %i" % i)
     # Same with 0 lags
     n_lags = np.zeros_like(self.n_lags, dtype='uint64')
     lrn = ConvSCCS(n_lags=n_lags, penalized_features=[])
     coeffs, _ = lrn.fit(self.features, self.labels, self.censoring)
     p_features, p_labels, p_censoring = lrn._preprocess_data(
         self.features, self.labels, self.censoring)
     confidence_intervals = lrn._bootstrap(p_features,
                                           p_labels, p_censoring,
                                           np.hstack(coeffs), 5, .90)
     for i, c in enumerate(coeffs):
         self.assertTrue(
             np.all(confidence_intervals.lower_bound[i] <= c),
             "lower bound of the confidence interval\
                                should be <= coeffs at index %i" % i)
         self.assertTrue(
             np.all(c <= confidence_intervals.upper_bound[i]),
             "upper bound of the confidence interval\
                                should be >= coeffs at index %i" % i)
コード例 #2
0
 def test_LearnerSCCS_score(self):
     lrn = ConvSCCS(n_lags=self.n_lags,
                    penalized_features=[],
                    random_state=self.seed)
     lrn.fit(self.features, self.labels, self.censoring)
     self.assertEqual(lrn.score(),
                      lrn.score(self.features, self.labels, self.censoring))
コード例 #3
0
 def test_LearnerSCCS_fit_KFold_CV(self):
     lrn = ConvSCCS(n_lags=self.n_lags,
                    penalized_features=np.arange(self.n_features),
                    random_state=self.seed,
                    C_tv=1e-1,
                    C_group_l1=1e-1)
     lrn.fit(self.features, self.labels, self.censoring)
     score = lrn.score()
     tv_range = (-5, -1)
     groupl1_range = (-5, -1)
     lrn.fit_kfold_cv(self.features,
                      self.labels,
                      self.censoring,
                      C_tv_range=tv_range,
                      C_group_l1_range=groupl1_range,
                      n_cv_iter=4)
     self.assertTrue(lrn.score() <= score)
コード例 #4
0
 def test_LearnerSCCS_fit(self):
     seed = 42
     n_lags = np.repeat(2, 2).astype('uint64')
     sim = SimuSCCS(n_cases=800,
                    n_intervals=10,
                    n_features=2,
                    n_lags=n_lags,
                    verbose=False,
                    seed=seed,
                    exposure_type='multiple_exposures')
     features, _, labels, censoring, coeffs = sim.simulate()
     lrn = ConvSCCS(n_lags=n_lags,
                    penalized_features=[],
                    tol=0,
                    max_iter=10,
                    random_state=seed)
     estimated_coeffs, _ = lrn.fit(features, labels, censoring)
     np.testing.assert_almost_equal(np.hstack(estimated_coeffs),
                                    np.hstack(coeffs),
                                    decimal=1)
コード例 #5
0
# print("C_tv : %f \n" % cv_track.best_model['C_tv'])
# print("C_group_l1 : %f \n" % cv_track.best_model['C_group_l1'])
# cv_track.plot_cv_report(35, 45)
# plt.show()
# confidence_intervals = cv_track.best_model['confidence_intervals']

# using the parameters resulting from cross-validation
learner = ConvSCCS(n_lags=n_lags.astype('uint64'),
                   penalized_features=np.arange(n_features),
                   random_state=42,
                   C_tv=270.2722840570933,
                   C_group_l1=5216.472772625124)

_, confidence_intervals = learner.fit(features,
                                      labels,
                                      censoring,
                                      confidence_intervals=True,
                                      n_samples_bootstrap=20)

# Plot estimated parameters
# get bootstrap confidence intervals
refitted_coeffs = confidence_intervals['refit_coeffs']
lower_bound = confidence_intervals['lower_bound']
upper_bound = confidence_intervals['upper_bound']

n_rows = int(np.ceil(n_features / 2))
remove_last_plot = (n_features % 2 != 0)

fig, axarr = plt.subplots(n_rows, 2, sharex=True, sharey=True, figsize=(10, 6))
y = confidence_intervals['refit_coeffs']
lb = confidence_intervals['lower_bound']