def test_equality(self): d = data.Table("zoo") d1 = distribution.Discrete(d, 0) d2 = distribution.Discrete(d, 0) d3 = distribution.Discrete(d, 1) self.assertEqual(d1, d1) self.assertEqual(d1, d2) self.assertNotEqual(d1, d3)
def test_fallback(self): d = data.Table("zoo") default = distribution.Discrete(d, "type") d._compute_distributions = Mock(side_effect=NotImplementedError) fallback = distribution.Discrete(d, "type") np.testing.assert_almost_equal(fallback, default) np.testing.assert_almost_equal(fallback.unknowns, default.unknowns)
def test_fallback_with_weights_and_nan(self): d = data.Table("zoo") d.set_weights(np.random.uniform(0., 1., size=len(d))) d.Y[::10] = np.nan default = distribution.Discrete(d, "type") d._compute_distributions = Mock(side_effect=NotImplementedError) fallback = distribution.Discrete(d, "type") np.testing.assert_almost_equal(fallback, default) np.testing.assert_almost_equal(fallback.unknowns, default.unknowns)
def test_normalize(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") disc.normalize() self.assertEqual(disc, self.rfreqs) disc.normalize() self.assertEqual(disc, self.rfreqs) disc1 = distribution.Discrete(None, d.domain.class_var) disc1.normalize() v = len(d.domain.class_var.values) assert_dist_almost_equal(disc1, [1 / v] * v)
def test_add(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") disc += [1, 2, 3, 4, 5, 6, 7] self.assertEqual(disc, [5.0, 22.0, 16.0, 12.0, 15.0, 47.0, 12.0]) disc2 = distribution.Discrete(d, d.domain.class_var) disc3 = disc - disc2 self.assertEqual(disc3, list(range(1, 8))) disc3 *= 2 self.assertEqual(disc3, [2 * x for x in range(1, 8)])
def test_hash(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") disc2 = distribution.Discrete(d, d.domain.class_var) self.assertEqual(hash(disc), hash(disc2)) disc2[0] += 1 self.assertNotEqual(hash(disc), hash(disc2)) disc2[0] -= 1 self.assertEqual(hash(disc), hash(disc2)) disc2.unknowns += 1 self.assertNotEqual(hash(disc), hash(disc2))
def test_random(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") ans = set() for i in range(1000): ans.add(int(disc.random())) self.assertEqual(ans, set(range(len(d.domain.class_var.values))))
def random_classifier(*args, **kwargs): prob = [random.random() for _ in data.domain.class_var.values] sprob = sum(prob) prob = [i / sprob for i in prob] distribution.Discrete(prob) return data.domain.class_var[random.randint( 0, len(data.domain.class_var.values) - 1)], prob
def test_array_with_unknowns(self): d = data.Table("zoo") d.Y[0] = np.nan disc = distribution.Discrete(d, "type") self.assertIsInstance(disc, np.ndarray) self.assertEqual(disc.unknowns, 1) true_freq = [4., 20., 13., 8., 10., 40., 5.] assert_dist_equal(disc, true_freq) np.testing.assert_array_equal(disc.array_with_unknowns, np.append(true_freq, 1))
def __call__(self, feature, data): if not data.domain.class_var: raise ValueError("Data with class labels required.") elif not isinstance(data.domain.class_var, DiscreteVariable): raise ValueError("Data with discrete class labels required.") cont = contingency.Discrete(data, feature) instances_with_class = np.sum( distribution.Discrete(data, data.domain.class_var)) return self.from_contingency( cont, 1. - np.sum(cont.unknowns) / instances_with_class)
def test_construction(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") self.assertIsInstance(disc, np.ndarray) self.assertIs(disc.variable, d.domain["type"]) self.assertEqual(disc.unknowns, 0) self.assertIs(disc.variable, d.domain.class_var) disc7 = distribution.Discrete(self.freqs) self.assertIsInstance(disc, np.ndarray) self.assertIsNone(disc7.variable) self.assertEqual(disc7.unknowns, 0) self.assertEqual(disc, disc7) disc1 = distribution.Discrete(None, d.domain.class_var) self.assertIsInstance(disc1, np.ndarray) self.assertIs(disc1.variable, d.domain.class_var) self.assertEqual(disc.unknowns, 0) assert_dist_equal(disc1, [0] * len(d.domain.class_var.values))
def test_deepcopy(self): d = data.Table("zoo") d1 = distribution.Discrete(d, 0) dc = copy.deepcopy(d1) # This always worked because `other` wasn't required to have `unknowns` self.assertEqual(d1, dc) # This failed before implementing `__deepcopy__` self.assertEqual(dc, d1) self.assertEqual(hash(d1), hash(dc)) # Test that `dc` has the required attributes self.assertEqual(dc.variable, d1.variable) self.assertEqual(dc.unknowns, d1.unknowns)
def test_from_table(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") self.assertIsInstance(disc, np.ndarray) self.assertIs(disc.variable, d.domain["type"]) self.assertEqual(disc.unknowns, 0) assert_dist_equal(disc, self.freqs) disc2 = distribution.Discrete(d, d.domain.class_var) self.assertIsInstance(disc2, np.ndarray) self.assertIs(disc2.variable, d.domain.class_var) self.assertEqual(disc, disc2) disc3 = distribution.Discrete(d, len(d.domain.attributes)) self.assertIsInstance(disc3, np.ndarray) self.assertIs(disc3.variable, d.domain.class_var) self.assertEqual(disc, disc3) disc5 = distribution.class_distribution(d) self.assertIsInstance(disc5, np.ndarray) self.assertIs(disc5.variable, d.domain.class_var) self.assertEqual(disc, disc5)
def score_data(self, data, feature): instances_with_class = \ np.sum(distribution.Discrete(data, data.domain.class_var)) def score_from_contingency(f): cont = contingency.Discrete(data, f) return self.from_contingency( cont, 1. - np.sum(cont.unknowns) / instances_with_class) scores = [score_from_contingency(f) for f in data.domain.attributes] if feature is not None: return scores[0] return scores
def fit_storage(self, data): if self.binarize and any( attr.is_discrete and len(attr.values) > self.MAX_BINARIZATION for attr in data.domain.attributes): # No fallback in the script; widgets can prevent this error # by providing a fallback and issue a warning about doing so raise ValueError("Exhaustive binarization does not handle " "attributes with more than {} values". format(self.MAX_BINARIZATION)) active_inst = np.nonzero(~np.isnan(data.Y))[0].astype(np.int32) root = self._build_tree(data, active_inst) if root is None: distr = distribution.Discrete(data, data.domain.class_var) if np.sum(distr) == 0: distr[:] = 1 root = Node(None, 0, distr) root.subset = active_inst model = TreeModel(data, root) return model
def _build_tree(self, data, active_inst, level=1): """Induce a tree from the given data Returns: root node (Node)""" node_insts = data[active_inst] distr = distribution.Discrete(node_insts, data.domain.class_var) if len(node_insts) < self.min_samples_leaf: return None if len(node_insts) < self.min_samples_split or \ max(distr) >= sum(distr) * self.sufficient_majority or \ self.max_depth is not None and level > self.max_depth: node, branches, n_children = Node(None, None, distr), None, 0 else: node, branches, n_children = self._select_attr(node_insts) node.subset = active_inst if branches is not None: node.children = [ self._build_tree(data, active_inst[branches == br], level + 1) for br in range(n_children)] return node
def test_modus(self): d = data.Table("zoo") disc = distribution.Discrete(d, "type") self.assertEqual(str(disc.modus()), "mammal")
def _select_attr(self, data): """Select the attribute for the next split. Returns: tuple with an instance of Node and a numpy array indicating the branch index for each data instance, or -1 if data instance is dropped """ # Prevent false warnings by pylint attr = attr_no = None col_x = None REJECT_ATTRIBUTE = 0, None, None, 0 def _score_disc(): """Scoring for discrete attributes, no binarization The class computes the entropy itself, not by calling other functions. This is to make sure that it uses the same definition as the below classes that compute entropy themselves for efficiency reasons.""" n_values = len(attr.values) if n_values < 2: return REJECT_ATTRIBUTE cont = _tree_scorers.contingency(col_x, len(data.domain.attributes[attr_no].values), data.Y, len(data.domain.class_var.values)) attr_distr = np.sum(cont, axis=0) null_nodes = attr_distr < self.min_samples_leaf # This is just for speed. If there is only a single non-null-node, # entropy wouldn't decrease anyway. if sum(null_nodes) >= n_values - 1: return REJECT_ATTRIBUTE cont[:, null_nodes] = 0 attr_distr = np.sum(cont, axis=0) cls_distr = np.sum(cont, axis=1) n = np.sum(attr_distr) # Avoid log(0); <= instead of == because we need an array cls_distr[cls_distr <= 0] = 1 attr_distr[attr_distr <= 0] = 1 cont[cont <= 0] = 1 class_entr = n * np.log(n) - np.sum(cls_distr * np.log(cls_distr)) attr_entr = np.sum(attr_distr * np.log(attr_distr)) cont_entr = np.sum(cont * np.log(cont)) score = (class_entr - attr_entr + cont_entr) / n / np.log(2) score *= n / len(data) # punishment for missing values branches = col_x.copy() branches[np.isnan(branches)] = -1 if score == 0: return REJECT_ATTRIBUTE node = DiscreteNode(attr, attr_no, None) return score, node, branches, n_values def _score_disc_bin(): """Scoring for discrete attributes, with binarization""" n_values = len(attr.values) if n_values <= 2: return _score_disc() cont = contingency.Discrete(data, attr) attr_distr = np.sum(cont, axis=0) # Skip instances with missing value of the attribute cls_distr = np.sum(cont, axis=1) if np.sum(attr_distr) == 0: # all values are missing return REJECT_ATTRIBUTE best_score, best_mapping = _tree_scorers.find_binarization_entropy( cont, cls_distr, attr_distr, self.min_samples_leaf) if best_score <= 0: return REJECT_ATTRIBUTE best_score *= 1 - np.sum(cont.unknowns) / len(data) mapping, branches = MappedDiscreteNode.branches_from_mapping( col_x, best_mapping, n_values) node = MappedDiscreteNode(attr, attr_no, mapping, None) return best_score, node, branches, 2 def _score_cont(): """Scoring for numeric attributes""" nans = np.sum(np.isnan(col_x)) non_nans = len(col_x) - nans arginds = np.argsort(col_x)[:non_nans] best_score, best_cut = _tree_scorers.find_threshold_entropy( col_x, data.Y, arginds, len(class_var.values), self.min_samples_leaf) if best_score == 0: return REJECT_ATTRIBUTE best_score *= non_nans / len(col_x) branches = np.full(len(col_x), -1, dtype=int) mask = ~np.isnan(col_x) branches[mask] = (col_x[mask] > best_cut).astype(int) node = NumericNode(attr, attr_no, best_cut, None) return best_score, node, branches, 2 ####################################### # The real _select_attr starts here is_sparse = sp.issparse(data.X) domain = data.domain class_var = domain.class_var best_score, *best_res = REJECT_ATTRIBUTE best_res = [Node(None, None, None)] + best_res[1:] disc_scorer = _score_disc_bin if self.binarize else _score_disc for attr_no, attr in enumerate(domain.attributes): col_x = data.X[:, attr_no] if is_sparse: col_x = col_x.toarray() col_x = col_x.flatten() sc, *res = disc_scorer() if attr.is_discrete else _score_cont() if res[0] is not None and sc > best_score: best_score, best_res = sc, res best_res[0].value = distribution.Discrete(data, class_var) return best_res