def test_nan(self): var = DiscreteVariable("d", tuple("abcde")) col = np.array([1.0, 4, 2, np.nan, 2, 0]) transform = Indicator(var, 2).transform expected = [0, 0, 1, np.nan, 1, 0] np.testing.assert_equal(transform(col), expected) sparse = transform(sp.csr_matrix(col)) self.assertTrue(sp.issparse(sparse)) np.testing.assert_equal(sparse.toarray().ravel(), expected) self.assertEqual(transform(1), 0) self.assertEqual(transform(2), 1) self.assertTrue(np.isnan(transform(np.nan))) transform = Indicator(var, 0).transform expected = [0, 0, 0, np.nan, 0, 1] np.testing.assert_equal(transform(col), expected) sparse = transform(sp.csr_matrix(col)) # Currently, this always returns dense array assert not sp.issparse(sparse) np.testing.assert_equal(sparse, expected) self.assertEqual(transform(1), 0) self.assertEqual(transform(0), 1) self.assertTrue(np.isnan(transform(np.nan))) transform = Indicator1(var, 2).transform expected = [-1, -1, 1, np.nan, 1, -1] np.testing.assert_equal(transform(col), expected) np.testing.assert_equal( transform(sp.csr_matrix(col).toarray().ravel()), expected) self.assertEqual(transform(1), -1) self.assertEqual(transform(2), 1) self.assertTrue(np.isnan(transform(np.nan)))
def results_one_vs_rest(results, pos_index): from Orange.preprocess.transformation import Indicator actual = results.actual == pos_index predicted = results.predicted == pos_index if results.probabilities is not None: c = results.probabilities.shape[2] assert c >= 2 neg_indices = [i for i in range(c) if i != pos_index] pos_prob = results.probabilities[:, :, [pos_index]] neg_prob = np.sum(results.probabilities[:, :, neg_indices], axis=2, keepdims=True) probabilities = np.dstack((neg_prob, pos_prob)) else: probabilities = None res = Orange.evaluation.Results() res.actual = actual res.predicted = predicted res.folds = results.folds res.row_indices = results.row_indices res.probabilities = probabilities value = results.domain.class_var.values[pos_index] class_var = Orange.data.DiscreteVariable( "I({}=={})".format(results.domain.class_var.name, value), values=["False", "True"], compute_value=Indicator(results.domain.class_var, pos_index)) domain = Orange.data.Domain(results.domain.attributes, [class_var], results.domain.metas) res.data = None res.domain = domain return res
def make_indicator_var(source, value_ind, weight=None): if weight is None: indicator = Indicator(source, value=value_ind) else: indicator = WeightedIndicator(source, value=value_ind, weight=weight) return Orange.data.ContinuousVariable("{}={}".format( source.name, source.values[value_ind]), compute_value=indicator)
def make_indicator_var(source, value_ind, weight=None, zero_based=True): var = Orange.data.ContinuousVariable("{}={}".format( source.name, source.values[value_ind])) if zero_based and weight is None: indicator = Indicator(source, value=value_ind) elif zero_based: indicator = WeightedIndicator(source, value=value_ind, weight=weight) elif weight is None: indicator = Indicator1(source, value=value_ind) else: indicator = WeightedIndicator_1(source, value=value_ind, weight=weight) var.compute_value = indicator return var