Exemplo n.º 1
0
class Chi2(SklScorer):
    """
    A wrapper for `${sklname}`. The following is the documentation
    from `scikit-learn <http://scikit-learn.org>`_.

    ${skldoc}
    """
    __wraps__ = skl_fss.chi2
    feature_type = DiscreteVariable
    class_type = DiscreteVariable
    preprocessors = [Discretize()]

    def score(self, X, y):
        f, p = skl_fss.chi2(X, y)
        return f
    def test_bad_learner(self):
        """
        Some learners on input might raise error.
        GH-38
        """
        w = self.widget

        self.assertFalse(w.Error.fitting_failed.is_shown())
        learner = LogisticRegressionLearner()
        learner.preprocessors = [Discretize()]
        self.send_signal(w.Inputs.learner, learner)
        self.send_signal(w.Inputs.data, self.iris)
        self.assertTrue(w.Error.fitting_failed.is_shown())
        learner.preprocessors = []
        self.send_signal(w.Inputs.learner, learner)
        self.assertFalse(w.Error.fitting_failed.is_shown())
    def test_set_preprocessor(self):
        """
        Test preprocessor set
        """
        w = self.widget

        preprocessor = Continuize()

        # check if empty
        self.assertIn(w.preprocessors, [[], None])

        self.send_signal(w.Inputs.preprocessor, preprocessor)

        # check preprocessor is set
        self.assertEqual(w.preprocessors, [preprocessor])
        self.assertIn(preprocessor,
                      self.get_output(w.Outputs.learner).preprocessors)

        # remove preprocessor
        self.send_signal(w.Inputs.preprocessor, None)

        self.assertIn(w.preprocessors, [[], None])
        self.assertNotIn(preprocessor,
                         self.get_output(w.Outputs.learner).preprocessors)

        # set it again
        preprocessor = Discretize()
        self.send_signal(w.Inputs.preprocessor, preprocessor)

        # check preprocessor is set
        self.assertEqual(w.preprocessors, [preprocessor])
        self.assertIn(preprocessor,
                      self.get_output(w.Outputs.learner).preprocessors)

        # change preprocessor
        preprocessor = Continuize()
        self.send_signal(w.Inputs.preprocessor, preprocessor)

        # check preprocessor is set
        self.assertEqual(w.preprocessors, [preprocessor])
        self.assertIn(preprocessor,
                      self.get_output(w.Outputs.learner).preprocessors)
Exemplo n.º 4
0
class ClassificationScorer(Scorer):
    """
    Base class for feature scores in a class-labeled data set.

    Parameters
    ----------
    feature : int, string, Orange.data.Variable
        Feature id
    data : Orange.data.Table
        Data set

    Attributes
    ----------
    feature_type : Orange.data.Variable
        Required type of features.

    class_type : Orange.data.Variable
        Required type of class variable.
    """
    feature_type = DiscreteVariable
    class_type = DiscreteVariable
    supports_sparse_data = True
    preprocessors = Scorer.preprocessors + [
        Discretize(remove_const=False)
    ]

    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
Exemplo n.º 5
0
        self.set_output_label_text()

    def merge_data(self):
        attributes = getattr(self.data.domain, 'attributes')
        cls_vars = getattr(self.data.domain, 'class_vars')
        metas_v = getattr(self.data.domain, 'metas')\
            + getattr(self.transformed_data.domain, 'attributes')
        domain = Domain(attributes, cls_vars, metas_v)
        X = self.data.X
        Y = self.data.Y
        metas = np.hstack((self.data.metas, self.transformed_data.X))
        table = Table.from_numpy(domain, X, Y, metas)
        table.name = getattr(self.data, 'name', '')
        table.attributes = getattr(self.data, 'attributes', {})
        table.ids = self.data.ids
        return table

    def send_report(self):
        if self.preprocessor is not None:
            self.report_items("Settings",
                              (("Preprocessor", self.preprocessor), ))
        if self.data is not None:
            self.report_data("Data", self.data)
        if self.transformed_data is not None:
            self.report_data("Transformed data", self.transformed_data)


if __name__ == "__main__":  # pragma: no cover
    WidgetPreview(OWTransform).run(set_data=Table("iris"),
                                   set_preprocessor=Discretize())
Exemplo n.º 6
0
            except Exception as ex:  # pylint: disable=broad-except
                self.Error.pp_error(ex)
        self.Outputs.transformed_data.send(self.transformed_data)

        self.set_preprocessor_label_text()
        self.set_output_label_text()

    def send_report(self):
        if self.preprocessor is not None:
            self.report_items("Settings",
                              (("Preprocessor", self.preprocessor), ))
        if self.data is not None:
            self.report_data("Data", self.data)
        if self.transformed_data is not None:
            self.report_data("Transformed data", self.transformed_data)


if __name__ == "__main__":
    from AnyQt.QtWidgets import QApplication

    app = QApplication([])
    ow = OWTransform()
    d = Table("iris")
    pp = Discretize()
    ow.set_data(d)
    ow.set_preprocessor(pp)
    ow.handleNewSignals()
    ow.show()
    app.exec_()
    ow.saveSettings()