Example #1
0
 def test_balancing_get_weights_treed_single_label(self):
     Y = np.array([0] * 80 + [1] * 20)
     balancing = Balancing(strategy='weighting')
     init_params, fit_params = balancing.get_weights(
         Y, 'adaboost', None, None, None)
     self.assertTrue(np.allclose(fit_params['classifier:sample_weight'],
                                 np.array([0.4] * 80 + [1.6] * 20)))
Example #2
0
 def test_balancing_get_weights_treed_single_label(self):
     Y = np.array([0] * 80 + [1] * 20)
     balancing = Balancing(strategy='weighting')
     init_params, fit_params = balancing.get_weights(
         Y, 'adaboost', None, None, None)
     self.assertTrue(
         np.allclose(fit_params['classifier:sample_weight'],
                     np.array([0.4] * 80 + [1.6] * 20)))
Example #3
0
 def test_balancing_get_weights_treed_multilabel(self):
     Y = np.array([[0, 0, 0]] * 100 + [[1, 0, 0]] * 100 + [[0, 1, 0]] * 100 +
                  [[1, 1, 0]] * 100 + [[0, 0, 1]] * 100 + [[1, 0, 1]] * 10)
     balancing = Balancing(strategy='weighting')
     init_params, fit_params = balancing.get_weights(
         Y, 'adaboost', None, None, None)
     self.assertTrue(np.allclose(fit_params['classifier:sample_weight'],
                                 np.array([0.4] * 500 + [4.0] * 10)))
Example #4
0
 def test_balancing_get_weights_treed_multilabel(self):
     Y = np.array([[0, 0, 0]] * 100 + [[1, 0, 0]] * 100 +
                  [[0, 1, 0]] * 100 + [[1, 1, 0]] * 100 +
                  [[0, 0, 1]] * 100 + [[1, 0, 1]] * 10)
     balancing = Balancing(strategy='weighting')
     init_params, fit_params = balancing.get_weights(
         Y, 'adaboost', None, None, None)
     self.assertTrue(
         np.allclose(fit_params['classifier:sample_weight'],
                     np.array([0.4] * 500 + [4.0] * 10)))
Example #5
0
 def test_balancing_get_weights_svm_sgd(self):
     Y = np.array([0] * 80 + [1] * 20)
     balancing = Balancing(strategy='weighting')
     init_params, fit_params = balancing.get_weights(
         Y, 'libsvm_svc', None, None, None)
     self.assertEqual(("classifier:class_weight", "auto"),
                      list(init_params.items())[0])
     init_params, fit_params = balancing.get_weights(
         Y, None, 'liblinear_svc_preprocessor', None, None)
     self.assertEqual(("preprocessor:class_weight", "auto"),
                      list(init_params.items())[0])
Example #6
0
 def test_balancing_get_weights_svm_sgd(self):
     Y = np.array([0] * 80 + [1] * 20)
     balancing = Balancing(strategy='weighting')
     init_params, fit_params = balancing.get_weights(
         Y, 'libsvm_svc', None, None, None)
     self.assertEqual(("classifier:class_weight", "auto"),
                      list(init_params.items())[0])
     init_params, fit_params = balancing.get_weights(
         Y, None, 'liblinear_svc_preprocessor', None, None)
     self.assertEqual(("preprocessor:class_weight", "auto"),
                      list(init_params.items())[0])
Example #7
0
    def pre_transform(self, X, y, fit_params=None, init_params=None):
        self.num_targets = 1 if len(y.shape) == 1 else y.shape[1]

        # Weighting samples has to be done here, not in the components
        if self.configuration['balancing:strategy'] == 'weighting':
            balancing = Balancing(strategy='weighting')
            init_params, fit_params = balancing.get_weights(
                y, self.configuration['classifier:__choice__'],
                self.configuration['preprocessor:__choice__'],
                init_params, fit_params)

        X, fit_params = super(ParamSklearnClassifier, self).pre_transform(
            X, y, fit_params=fit_params, init_params=init_params)

        return X, fit_params
    def pre_transform(self, X, y, fit_params=None, init_params=None):
        self.num_targets = 1 if len(y.shape) == 1 else y.shape[1]

        # Weighting samples has to be done here, not in the components
        if self.configuration['balancing:strategy'] == 'weighting':
            balancing = Balancing(strategy='weighting')
            init_params, fit_params = balancing.get_weights(
                y, self.configuration['classifier:__choice__'],
                self.configuration['preprocessor:__choice__'], init_params,
                fit_params)

        X, fit_params = super(ParamSklearnClassifier,
                              self).pre_transform(X,
                                                  y,
                                                  fit_params=fit_params,
                                                  init_params=init_params)

        return X, fit_params