Source

orange-multitarget / orangecontrib / multitarget / widgets / OWClusteringRandomForest.py

"""
<name>Clustering Random Forest</name>
<description>Clustering random forest learner/classifier for multi
target classification.</description>
<icon>icons/ClusteringRandomForest.png</icon>
<priority>110</priority>
"""

import Orange
import Orange.multitarget

from Orange.tuning import PreprocessedLearner

from OWWidget import *
import OWGUI


NAME = "Clustering Random Forest"
DESCRIPTION = """
Clustering random forest learner/classifier for multi-target classification.
"""
CATEGORY = "Multitarget"
PRIORITY = 110
ICON = "icons/ClusteringRandomForest.png"

INPUTS = [("Data", Orange.data.Table, "set_data"),
          ("Preprocess", PreprocessedLearner, "set_preprocessor")]

OUTPUTS = [("Learner", Orange.classification.Learner),
           ("Classifier", Orange.classification.Classifier)]

REPLACES = ["_multitarget.widgets.OWClusteringRandomForest.OWClusteringRandomForest"]

class OWClusteringRandomForest(OWWidget):
    settingsList = ["name", "trees", "min_instances", "min_majority",
                    "max_depth", "min_MSE", "method"]

    def __init__(self, parent=None, signalManager=None,
                 title="Clustering Random Forest"):
        OWWidget.__init__(self, parent, signalManager, title,
                          wantMainArea=False)

        self.inputs = [("Data", Orange.data.Table, self.set_data),
                       ("Preprocess", PreprocessedLearner,
                        self.set_preprocessor)
                       ]
        self.outputs = [("Learner", Orange.classification.Learner),
                        ("Classifier", Orange.classification.Classifier)
                        ]

        self.name = 'Clustering Random Forest'
        self.trees = 25
        self.max_depth = 100
        self.min_majority = 1.0
        self.min_MSE = 0.001
        self.min_instances = 5
        self.method = 0

        self.maxTrees = 10000

        self.loadSettings()

        self.data = None
        self.preprocessor = None

        box = OWGUI.widgetBox(self.controlArea, "Classifier/Learner Name")
        OWGUI.lineEdit(box, self, "name")

        OWGUI.spin(box, self, "trees", 1, 1000, orientation="horizontal", label="Number of trees in forest")

        OWGUI.separator(self.controlArea)

        OWGUI.spin(box, self, "max_depth", 1, 1000, 1,
                   "Stop splitting nodes at depth")

        OWGUI.doubleSpin(box, self, "min_majority", 0.01, 1.0, 0.01,
                         "Minimal majority class proportion (%)",
                         tooltip="Minimal proportion of the majority class "
                                 "value each of the class variables has to "
                                 "reach to stop induction (only used for "
                                 "classification)"
                        )

        OWGUI.doubleSpin(box, self, "min_MSE", 0.001, 1.0, 0.001,
                         "Min. mean squared error",
                         tooltip="Minimal mean squared error each of the "
                                 "class variables has to reach to stop "
                                 "induction (only used for regression)."
                         )

        OWGUI.spin(box, self, "min_instances", 1, 1000, 1,
                   "Min. instances in leaves")


        OWGUI.radioButtonsInBox(self.controlArea, self, "method",
              box = "Feature scorer",
              btnLabels = ["Inter dist", "Intra dist", "Silhouette" ,"Gini-index"],
              tooltips = ["Maximal distance between clusters",
                          "Minimal distance inside clusters ",
                          "Silhouette measure with prototypes",
                          "Gini-index, used for nominal class variables"]
                          )

        OWGUI.separator(self.controlArea)

        OWGUI.button(self.controlArea, self, "&Apply",
                     callback=self.apply,
                     tooltip="Create the learner and apply it on input data.",
                     autoDefault=True
                     )

        self.data = None
        self.preprocessor = None
        self.apply()

    def sendReport(self):
        self.reportSettings(
            "Parameters",
            [("# of trees", self.trees),
            ("Max depth", self.max_depth),
             ("Min. majority", self.min_majority),
             ("Min MSE", self.min_MSE),
             ("Min instances in leaves", self.min_instances)
             ]
        )
        self.reportData(self.data)


    def set_data(self, data=None):
        """Set the widget input data.
        """
        self.data = data
        self.error([0])
        if data is not None and not data.domain.class_vars:
            data = None
            self.error(0, "Input data must have multi target domain.")

        self.data = data
        self.apply()

        self.send("Random Forest Classifier", self.classifier)
        
    def set_preprocessor(self, preprocessor=None):
        """Set data preprocessor.
        """
        self.preprocessor = preprocessor



    def apply(self):
        """Apply the settings to the output learner. If input data is available
        then also construct a classifier.

        """

        learner = Orange.multitarget.tree.ClusteringTreeLearner(
                    max_depth=self.max_depth,
                    min_majority=self.min_majority,
                    min_MSE=self.min_MSE,
                    min_instances=self.min_instances,
                    method=self.method,
                    name=self.name)

        learner = Orange.ensemble.forest.RandomForestLearner(base_learner=learner, \
            trees=self.trees, name=self.name)

        if self.preprocessor is not None:
            learner = self.preprocessor.wrapLearner(learner)

        classifier = None
        self.error([1])
        if self.data is not None:
            try:
                classifier = learner(self.data)
                classifier.name = self.name
            except Exception, ex:
                self.error(1, str(ex))

        self.send("Learner", learner)
        self.send("Classifier", classifier)




if __name__=="__main__":
    # no tests currently
    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.