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 make_indicator_var(source, value_ind, weight=None, zero_based=True): 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) return Orange.data.ContinuousVariable("{}={}".format( source.name, source.values[value_ind]), compute_value=indicator)