예제 #1
0
 def test_regression(self):
     sf = StackedFitter([TreeLearner(), KNNLearner()])
     cv = CrossValidation(k=3, random_state=0)
     results = cv(self.housing[:50], [sf, TreeLearner(), KNNLearner()])
     mse = MSE()(results)
     self.assertLess(mse[0], mse[1])
     self.assertLess(mse[0], mse[2])
예제 #2
0
 def test_regression(self):
     sf = StackedFitter([TreeLearner(), KNNLearner()])
     results = CrossValidation(
         self.housing[:50],
         [sf, TreeLearner(), KNNLearner()], k=3)
     mse = MSE(results)
     self.assertLess(mse[0], mse[1])
     self.assertLess(mse[0], mse[2])
    def test_tree_determinism(self):
        """Check that the tree is drawn identically upon receiving the same
        dataset with no parameter changes."""
        n_tries = 10
        # Make sure the tree are deterministic for iris
        scene_nodes = []
        for _ in range(n_tries):
            self.send_signal(self.widget.Inputs.tree, self.signal_data)
            scene_nodes.append([n.pos() for n in self.get_visible_squares()])
        for node_row in zip(*scene_nodes):
            self.assertTrue(
                self._check_all_same(node_row),
                "The tree was not drawn identically in the %d times it was "
                "sent to widget after receiving the iris dataset." % n_tries)

        # Make sure trees are deterministic with data where some variables have
        # the same entropy
        data_same_entropy = Table(
            path.join(path.dirname(path.dirname(path.dirname(__file__))),
                      "tests", "datasets", "same_entropy.tab"))
        data_same_entropy = TreeLearner()(data_same_entropy)
        scene_nodes = []
        for _ in range(n_tries):
            self.send_signal(self.widget.Inputs.tree, data_same_entropy)
            scene_nodes.append([n.pos() for n in self.get_visible_squares()])
        for node_row in zip(*scene_nodes):
            self.assertTrue(
                self._check_all_same(node_row),
                "The tree was not drawn identically in the %d times it was "
                "sent to widget after receiving a dataset with variables with "
                "same entropy." % n_tries)
예제 #4
0
def main():
    from Orange.modelling import TreeLearner
    from AnyQt.QtWidgets import QApplication
    import sys

    app = QApplication(sys.argv)

    ow = OWPythagorasTree()
    data = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')

    model = TreeLearner(max_depth=1000)(data)
    model.instances = data
    ow.set_tree(model)

    ow.show()
    ow.raise_()
    ow.handleNewSignals()
    app.exec_()
예제 #5
0
def main():
    from Orange.modelling import TreeLearner
    from AnyQt.QtWidgets import QApplication
    import sys

    app = QApplication(sys.argv)

    ow = OWPythagorasTree()
    data = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')

    model = TreeLearner(max_depth=1000)(data)
    model.instances = data
    ow.set_tree(model)

    ow.show()
    ow.raise_()
    ow.handleNewSignals()
    app.exec_()
    def setUpClass(cls):
        super().setUpClass()
        WidgetOutputsTestMixin.init(cls)

        # Set up for output tests
        tree = TreeLearner()
        cls.model = tree(cls.data)
        cls.model.instances = cls.data

        cls.signal_name = "Tree"
        cls.signal_data = cls.model

        # Set up for widget tests
        titanic_data = Table('titanic')[::50]
        cls.titanic = TreeLearner(max_depth=1)(titanic_data)
        cls.titanic.instances = titanic_data

        housing_data = Table('housing')[:10]
        cls.housing = TreeLearner(max_depth=1)(housing_data)
        cls.housing.instances = housing_data
예제 #7
0
    def test_context(self):
        iris_tree = TreeLearner()(Table("iris"))
        self.send_signal(self.widget.Inputs.tree, self.titanic)
        self.widget.target_class_index = 1

        self.send_signal(self.widget.Inputs.tree, iris_tree)
        self.assertEqual(0, self.widget.target_class_index)

        self.widget.target_class_index = 2
        self.send_signal(self.widget.Inputs.tree, self.titanic)
        self.assertEqual(1, self.widget.target_class_index)

        self.send_signal(self.widget.Inputs.tree, iris_tree)
        self.assertEqual(2, self.widget.target_class_index)
예제 #8
0
    def test_bad_data(self):
        """
        Firstly it creates predictions with TreeLearner. Then sends predictions and
        different data with different domain to Predictions widget. Those different
        data and domain are similar to original data and domain but they have three
        different target values instead of two.
        GH-2129
        """
        Variable._clear_all_caches()

        filestr1 = """\
        age\tsex\tsurvived
        d\td\td
        \t\tclass
        adult\tmale\tyes
        adult\tfemale\tno
        child\tmale\tyes
        child\tfemale\tyes
        """
        file1 = io.StringIO(filestr1)
        table = TabReader(file1).read()
        learner = TreeLearner()
        tree = learner(table)

        filestr2 = """\
        age\tsex\tsurvived
        d\td\td
        \t\tclass
        adult\tmale\tyes
        adult\tfemale\tno
        child\tmale\tyes
        child\tfemale\tunknown
        """
        file2 = io.StringIO(filestr2)
        bad_table = TabReader(file2).read()

        self.send_signal(self.widget.Inputs.predictors, tree, 1)

        with excepthook_catch():
            self.send_signal(self.widget.Inputs.data, bad_table)

        Variable._clear_all_caches(
        )  # so that test excepting standard titanic work
예제 #9
0
    def test_tree_determinism(self):
        """Check that the tree is drawn identically upon receiving the same
        dataset with no parameter changes."""
        n_tries = 10

        def _check_all_same(data):
            """Check that all the elements within an iterable are identical."""
            iterator = iter(data)
            try:
                first = next(iterator)
            except StopIteration:
                return True
            return all(first == rest for rest in iterator)

        # Make sure the tree are deterministic for iris
        scene_nodes = []
        for _ in range(n_tries):
            self.send_signal(self.signal_name, self.signal_data)
            scene_nodes.append([n.pos() for n in self.get_visible_squares()])
        for node_row in zip(*scene_nodes):
            self.assertTrue(
                _check_all_same(node_row),
                "The tree was not drawn identically in the %d times it was "
                "sent to widget after receiving the iris dataset." % n_tries)

        # Make sure trees are deterministic with data where some variables have
        # the same entropy
        data_same_entropy = Table(
            path.join(path.dirname(path.dirname(path.dirname(__file__))),
                      "tests", "datasets", "same_entropy.tab"))
        data_same_entropy = TreeLearner()(data_same_entropy)
        scene_nodes = []
        for _ in range(n_tries):
            self.send_signal(self.signal_name, data_same_entropy)
            scene_nodes.append([n.pos() for n in self.get_visible_squares()])
        for node_row in zip(*scene_nodes):
            self.assertTrue(
                _check_all_same(node_row),
                "The tree was not drawn identically in the %d times it was "
                "sent to widget after receiving a dataset with variables with "
                "same entropy." % n_tries)
예제 #10
0
        elif self.target_class_index == 2:
            values = (0, np.std(self.clf_dataset.Y))
            colors = _get_colors_domain(self.model.domain)
            while len(values) != len(colors):
                values.insert(1, -1)
            items = list(zip(values, colors))
        else:
            items = None

        self.legend = OWContinuousLegend(items=items, **self.LEGEND_OPTIONS)
        self.legend.setVisible(self.show_legend)
        self.scene.addItem(self.legend)


class TreeGraphicsView(PannableGraphicsView, ZoomableGraphicsView,
                       AnchorableGraphicsView, PreventDefaultWheelEvent):
    """QGraphicsView that contains all functionality we will use to display
    tree."""


class TreeGraphicsScene(UpdateItemsOnSelectGraphicsScene):
    """QGraphicsScene that the tree uses."""


if __name__ == "__main__":  # pragma: no cover
    from Orange.modelling import TreeLearner
    data = Table('iris')
    model = TreeLearner(max_depth=1000)(data)
    model.instances = data
    WidgetPreview(OWPythagorasTree).run(model)
예제 #11
0
                values.insert(1, -1)
            items = list(zip(values, colors))
        else:
            items = None

        self.legend = OWContinuousLegend(items=items, **self.LEGEND_OPTIONS)
        self.legend.setVisible(self.show_legend)
        self.scene.addItem(self.legend)


class TreeGraphicsView(
        PannableGraphicsView,
        ZoomableGraphicsView,
        AnchorableGraphicsView,
        PreventDefaultWheelEvent
):
    """QGraphicsView that contains all functionality we will use to display
    tree."""


class TreeGraphicsScene(UpdateItemsOnSelectGraphicsScene):
    """QGraphicsScene that the tree uses."""


if __name__ == "__main__":  # pragma: no cover
    from Orange.modelling import TreeLearner
    data = Table('iris')
    model = TreeLearner(max_depth=1000)(data)
    model.instances = data
    WidgetPreview(OWPythagorasTree).run(model)
예제 #12
0
 def test_classification(self):
     sf = StackedFitter([TreeLearner(), KNNLearner()])
     results = CrossValidation(self.iris, [sf], k=3)
     ca = CA(results)
     self.assertGreater(ca, 0.9)