Commits

Aleš Erjavec committed bcef9c6

Added Earth widget

Comments (0)

Files changed (3)

orangekit/earth/widgets/OWEarth.py

+"""
+<name>Earth</name>
+<description>Multivariate Adaptive Regression Splines (MARS)</description>
+<category>Regression</category>
+<icon>icons/EarthMars.svg</icon>
+<priority>100</priority>
+<tags>MARS, Multivariate, Adaptive, Regression, Splines</tags>
+"""
+
+import Orange
+
+from OWWidget import *
+import OWGUI
+
+from orngWrap import PreprocessedLearner
+
+from orangekit import earth
+
+NAME = "Earth"
+DESCRIPTION = "Multivariate Adaptive Regression Splines (MARS)"
+ICON = "icons/EarthMars.svg"
+CATEGORY = "Regression"
+PRIORITY = 100
+
+KEYWORDS = ["MARS", "Multivariate", "Adaptive", "Regression", "Splines"]
+
+INPUTS = (
+    {"name": "Data",
+     "type": Orange.data.Table,
+     "handler": "set_data",
+     "doc": "Set input training data set."},
+
+    {"name": "Preprocessor",
+     "type": PreprocessedLearner,
+     "handler": "set_preprocessor",
+     "doc": "Data Preprocessor"}
+)
+
+OUTPUTS = (
+    ("Learner", earth.EarthLearner),
+    ("Predictor", earth.EarthClassifier),
+    ("Basis Matrix", Orange.data.Table)
+)
+
+REPLACES = ["Orange.OrangeWidgets.Regression.OWEarth.OWEarth"]
+
+
+class OWEarth(OWWidget):
+    settingsList = ["name", "degree", "terms", "penalty"]
+
+    def __init__(self, parent=None, signalManager=None,
+                 title="Earth"):
+        OWWidget.__init__(self, parent, signalManager, title,
+                          wantMainArea=False)
+
+        self.name = "Earth Learner"
+        self.degree = 1
+        self.terms = 21
+        self.penalty = 2
+
+        self.loadSettings()
+
+        #####
+        # GUI
+        #####
+
+        OWGUI.lineEdit(self.controlArea, self, "name",
+                       box="Learner/Classifier Name",
+                       tooltip="Name for the learner/predictor")
+
+        box = OWGUI.widgetBox(self.controlArea, "Forward Pass", addSpace=True)
+        OWGUI.spin(box, self, "degree", 1, 3, step=1,
+                   label="Max. term degree",
+                   tooltip="Maximum degree of the terms derived "
+                           "(number of hinge functions).")
+        s = OWGUI.spin(box, self, "terms", 1, 200, step=1,
+                       label="Max. terms",
+                       tooltip="Maximum number of terms derived in the "
+                               "forward pass.")
+        s.control.setSpecialValueText("Automatic")
+
+        box = OWGUI.widgetBox(self.controlArea, "Pruning Pass", addSpace=True)
+        OWGUI.doubleSpin(box, self, "penalty", min=0.0, max=10.0, step=0.25,
+                   label="Knot penalty")
+
+        OWGUI.button(self.controlArea, self, "&Apply",
+                     callback=self.apply)
+
+        self.data = None
+        self.preprocessor = None
+        self.resize(300, 200)
+
+        self.apply()
+
+    def set_data(self, data=None):
+        """
+        Set the input data set.
+        """
+        self.data = data
+
+    def set_preprocessor(self, pproc=None):
+        self.preprocessor = pproc
+
+    def handleNewSignals(self):
+        self.apply()
+
+    def apply(self):
+        learner = earth.EarthLearner(
+            degree=self.degree,
+            terms=self.terms if self.terms >= 2 else None,
+            penalty=self.penalty,
+            name=self.name
+        )
+
+        predictor = None
+        basis_matrix = None
+        if self.preprocessor:
+            learner = self.preprocessor.wrapLearner(learner)
+
+        self.error(0)
+        if self.data is not None:
+            try:
+                predictor = learner(self.data)
+                predictor.name = self.name
+            except Exception, ex:
+                self.error(0, "An error during learning: %r" % ex)
+
+            if predictor is not None:
+                base_features = predictor.base_features()
+                basis_domain = Orange.data.Domain(
+                    base_features,
+                    self.data.domain.class_var,
+                    self.data.domain.class_vars)
+                basis_domain.add_metas(self.data.domain.get_metas())
+                basis_matrix = Orange.data.Table(basis_domain, self.data)
+
+        self.send("Learner", learner)
+        self.send("Predictor", predictor)
+        self.send("Basis Matrix", basis_matrix)
+
+    def sendReport(self):
+        self.reportSettings(
+            "Learning parameters",
+            [("Degree", self.degree),
+             ("Terms", self.terms if self.terms >= 2 else "Automatic"),
+             ("Knot penalty", "%.2f" % self.penalty)
+             ])
+
+        self.reportData(self.data)
+
+if __name__ == "__main__":
+    app = QApplication(sys.argv)
+    w = OWEarth()
+    w.set_data(Orange.data.Table("auto-mpg"))
+    w.show()
+    app.exec_()
+    w.saveSettings()

orangekit/earth/widgets/__init__.py

Empty file added.

orangekit/earth/widgets/icons/EarthMars.svg

Added
New image
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="48px" height="48px" viewBox="0 0 48 48" enable-background="new 0 0 48 48" xml:space="preserve">
+<g>
+	<path fill="#333333" d="M38.884,22.226c-0.006-0.046-0.006-0.093-0.012-0.139c-0.04-0.312-0.097-0.617-0.158-0.921
+		c-0.017-0.086-0.03-0.174-0.048-0.259c-0.075-0.356-0.165-0.706-0.265-1.053c-0.041-0.144-0.079-0.289-0.125-0.431
+		c-0.12-0.375-0.258-0.74-0.406-1.102c-0.01-0.023-0.019-0.047-0.028-0.07c-0.144-0.347-0.301-0.686-0.47-1.019
+		c-0.021-0.041-0.042-0.081-0.063-0.122c-0.17-0.328-0.35-0.65-0.542-0.964c-0.016-0.026-0.034-0.052-0.05-0.078
+		c-0.442-0.708-0.947-1.37-1.497-1.99c-0.434-0.489-0.893-0.955-1.386-1.385c-0.088-0.076-0.174-0.156-0.264-0.231
+		c-0.209-0.174-0.427-0.338-0.646-0.501c-0.135-0.101-0.271-0.2-0.41-0.296c-0.204-0.142-0.411-0.277-0.622-0.408
+		c-0.169-0.105-0.341-0.205-0.515-0.304c-0.198-0.112-0.396-0.225-0.599-0.328c-0.211-0.107-0.428-0.204-0.645-0.302
+		c-0.179-0.08-0.355-0.166-0.537-0.239c-0.396-0.16-0.799-0.306-1.211-0.432c-0.1-0.031-0.204-0.052-0.304-0.08
+		c-0.332-0.094-0.667-0.182-1.007-0.253c-0.158-0.033-0.318-0.056-0.478-0.084c-0.291-0.051-0.584-0.097-0.881-0.131
+		c-0.173-0.02-0.348-0.033-0.523-0.047c-0.292-0.023-0.585-0.038-0.881-0.044C24.212,9.013,24.115,9,24.014,9
+		c-0.183,0-0.366,0.003-0.551,0.01c-0.081,0.003-0.158,0.017-0.238,0.021h0c-4.08,0.209-7.705,2.031-10.274,4.832
+		c-0.019,0.02-0.035,0.042-0.053,0.062c-0.3,0.33-0.587,0.671-0.857,1.027c-0.037,0.048-0.068,0.102-0.104,0.15
+		c-0.245,0.332-0.482,0.67-0.7,1.022c-0.055,0.089-0.1,0.184-0.153,0.273c-0.189,0.32-0.375,0.643-0.541,0.978
+		c-0.063,0.129-0.114,0.265-0.174,0.396c-0.14,0.306-0.281,0.609-0.401,0.926c-0.067,0.177-0.117,0.361-0.177,0.541
+		c-0.094,0.279-0.194,0.557-0.271,0.843c-0.062,0.229-0.103,0.467-0.154,0.7c-0.054,0.246-0.119,0.488-0.16,0.739
+		c-0.05,0.296-0.073,0.6-0.105,0.901c-0.021,0.196-0.055,0.388-0.067,0.586c-0.033,0.503-0.041,1.013-0.022,1.527
+		c0.019,0.511,0.063,1.017,0.131,1.515c0.022,0.16,0.062,0.316,0.089,0.477c0.056,0.332,0.109,0.664,0.187,0.986
+		c0.046,0.193,0.111,0.377,0.164,0.566c0.08,0.283,0.154,0.568,0.25,0.844c0.069,0.199,0.156,0.389,0.233,0.584
+		c0.101,0.256,0.196,0.514,0.31,0.762c0.091,0.197,0.198,0.387,0.297,0.578c0.121,0.234,0.235,0.473,0.368,0.697
+		c0.111,0.191,0.238,0.373,0.357,0.559c0.139,0.215,0.273,0.434,0.423,0.643c0.13,0.182,0.274,0.352,0.413,0.527
+		c0.156,0.197,0.308,0.398,0.474,0.588c0.148,0.17,0.309,0.328,0.465,0.492c0.172,0.18,0.341,0.363,0.521,0.533
+		c0.165,0.156,0.341,0.301,0.513,0.449c0.187,0.162,0.371,0.326,0.566,0.479c0.18,0.141,0.369,0.27,0.555,0.402
+		c0.201,0.143,0.4,0.289,0.608,0.422c0.193,0.123,0.395,0.234,0.594,0.35c0.214,0.123,0.426,0.248,0.646,0.361
+		c0.206,0.105,0.417,0.199,0.628,0.295c0.226,0.102,0.451,0.207,0.682,0.299c0.216,0.086,0.437,0.158,0.657,0.234
+		c0.237,0.08,0.473,0.162,0.715,0.232c0.225,0.064,0.454,0.117,0.683,0.172c0.247,0.057,0.493,0.115,0.744,0.162
+		c0.232,0.043,0.469,0.074,0.705,0.105c0.255,0.033,0.51,0.068,0.769,0.09c0.239,0.02,0.481,0.027,0.724,0.035
+		C23.65,38.98,23.818,39,23.991,39c0.182,0,0.364-0.004,0.547-0.01c8.277-0.297,14.75-7.248,14.452-15.528
+		C38.976,23.044,38.937,22.633,38.884,22.226z M35.49,17.914c-0.528,0.02-1.089,0.199-1.563-0.274
+		c-0.228-0.228-0.957-1.595-1.458-0.638c-0.182,0.455-0.334,1.739,0.03,1.921c0.187,0.102,0.509,0.101,0.715,0.123
+		c0.443,0.05,1.184-0.019,1.51,0.353c0.414,0.471-0.35,1.015-0.68,1.232c-0.484,0.317-1.059,0.608-1.655,0.583
+		c-0.415-0.018-0.726-0.511-0.957-0.8c-0.397-0.496-0.669-1.077-1.078-1.565c-0.241-0.288-0.727-0.858-1.167-0.682
+		c-0.201,0.081-0.369,0.315-0.389,0.531c-0.056,0.6,0.276,1.271,0.543,1.788c0.283,0.548,0.632,1.099,0.755,1.713
+		c0.137,0.684,0.137,1.139,1.003,1.139c0.547,0,1.595-1.002,1.869-0.638c0.683,0.956-1.413,2.779-1.915,3.234
+		c-0.5,0.457-1.139,0.775-1.139,1.551c0,0.547,0.502,1.004,0.502,1.549c0,0.912-0.684,0.273-1.23,0.865
+		c-0.501,0.549,0.045,1.277-0.228,1.824c-0.365,0.773-2.826,2.096-3.691,1.914c-1.002-0.229-2.232-2.234-2.369-3.189
+		c-0.092-0.639,0.227-1.232,0.319-1.824c0.045-0.684,0.045-1.365-0.137-2.006c-0.228-0.729-0.729-1.367-1.185-1.958
+		c-0.319-0.365-0.319-0.229-0.364-0.639c-0.046-0.272,0.5-0.272,0.137-0.591c-0.274-0.274-1.458,0.318-1.732,0.41
+		c-0.775,0.273-1.504,0.591-2.324,0.228c-1.048-0.502-1.959-2.052-1.959-3.191c0-1.595,1.367-2.095,2.188-3.234
+		c0.364-0.547,0.592-1.003,1.23-1.14c0.684-0.137,1.094,0.092,1.777-0.182c1.458-0.593,1.731,0.045,3.008,0.547
+		c0.228,0.091,0.683,0.364,0.911,0.364c0.219,0,0.292-0.036,0.37-0.208c0.351-0.779,0.953-0.353,1.544-0.156
+		c0.684,0.228,2.373,0.135,2.874-0.913c0.365-0.866-0.547-0.775-1.094-0.866c-0.364-0.046-1.96-0.547-1.14-1.14
+		c0.365-0.273,1.641,0.319,2.142,0.319c0.729,0.046,1.186-0.137,1.002-0.911c-0.182-0.866-0.456-0.639-1.184-0.365
+		c-0.365,0.137-0.319,0.273-0.729,0.273c-0.274,0-0.547-0.182-0.82-0.137c-0.911,0.137-1.002,1.14-1.549,1.687
+		c-0.729,0.774-0.547,0-0.82-0.547c-0.117-0.234-0.321-0.438-0.522-0.6c-0.121-0.097-0.622-0.36-0.663-0.051
+		c-0.024,0.178,0.188,0.366,0.259,0.511c0.136,0.275,0.212,0.62-0.213,0.596c-0.457-0.046-0.684-1.049-1.14-1.23
+		c-1.686-0.775-2.415,1.458-3.828,1.641c-0.774,0.091-0.774-0.41-0.547-0.957c0.319-0.82,0.683-0.639,1.367-0.82
+		c0.73-0.229,0.456-0.593,0.866-0.957c0.319-0.273,0.957-0.41,1.321-0.684c0.293-0.187,0.606-0.459,0.939-0.678
+		c0.112-0.007,0.222-0.024,0.335-0.028C23.695,11.003,23.854,11,24.014,11C29.004,11,33.315,13.786,35.49,17.914z"/>
+	<path fill="#FFFFFF" d="M24.014,11c-0.159,0-0.318,0.003-0.479,0.009c-0.113,0.004-0.223,0.021-0.335,0.028
+		c-0.333,0.219-0.646,0.491-0.939,0.678c-0.364,0.273-1.002,0.41-1.321,0.684c-0.41,0.364-0.136,0.729-0.866,0.957
+		c-0.684,0.182-1.047,0-1.367,0.82c-0.228,0.547-0.228,1.048,0.547,0.957c1.413-0.183,2.142-2.416,3.828-1.641
+		c0.456,0.182,0.683,1.185,1.14,1.23c0.426,0.024,0.349-0.32,0.213-0.596c-0.071-0.145-0.283-0.333-0.259-0.511
+		c0.041-0.31,0.542-0.046,0.663,0.051c0.202,0.161,0.405,0.365,0.522,0.6c0.273,0.547,0.091,1.321,0.82,0.547
+		c0.547-0.547,0.638-1.55,1.549-1.687c0.273-0.045,0.546,0.137,0.82,0.137c0.41,0,0.365-0.137,0.729-0.273
+		c0.729-0.273,1.002-0.501,1.184,0.365c0.183,0.774-0.273,0.957-1.002,0.911c-0.501,0-1.777-0.593-2.142-0.319
+		c-0.82,0.593,0.775,1.094,1.14,1.14c0.547,0.091,1.458,0,1.094,0.866c-0.501,1.048-2.19,1.141-2.874,0.913
+		c-0.591-0.196-1.193-0.623-1.544,0.156c-0.078,0.172-0.151,0.208-0.37,0.208c-0.228,0-0.683-0.273-0.911-0.364
+		c-1.277-0.502-1.55-1.14-3.008-0.547c-0.684,0.273-1.094,0.045-1.777,0.182c-0.638,0.137-0.866,0.593-1.23,1.14
+		c-0.82,1.14-2.188,1.64-2.188,3.234c0,1.14,0.911,2.689,1.959,3.191c0.82,0.363,1.549,0.046,2.324-0.228
+		c0.273-0.092,1.458-0.685,1.732-0.41c0.364,0.318-0.183,0.318-0.137,0.591c0.045,0.41,0.045,0.273,0.364,0.639
+		c0.456,0.591,0.957,1.23,1.185,1.958c0.182,0.641,0.182,1.322,0.137,2.006c-0.092,0.592-0.411,1.186-0.319,1.824
+		c0.137,0.955,1.367,2.961,2.369,3.189c0.866,0.182,3.327-1.141,3.691-1.914c0.273-0.547-0.273-1.275,0.228-1.824
+		c0.547-0.592,1.23,0.047,1.23-0.865c0-0.545-0.502-1.002-0.502-1.549c0-0.775,0.639-1.094,1.139-1.551
+		c0.502-0.455,2.598-2.278,1.915-3.234c-0.274-0.364-1.322,0.638-1.869,0.638c-0.866,0-0.866-0.455-1.003-1.139
+		c-0.123-0.614-0.472-1.165-0.755-1.713c-0.267-0.518-0.599-1.188-0.543-1.788c0.02-0.216,0.188-0.45,0.389-0.531
+		c0.44-0.177,0.926,0.394,1.167,0.682c0.409,0.488,0.68,1.069,1.078,1.565c0.231,0.289,0.542,0.782,0.957,0.8
+		c0.596,0.025,1.171-0.266,1.655-0.583c0.331-0.218,1.094-0.762,0.68-1.232c-0.326-0.371-1.067-0.303-1.51-0.353
+		c-0.206-0.022-0.528-0.021-0.715-0.123c-0.364-0.182-0.212-1.466-0.03-1.921c0.501-0.957,1.23,0.41,1.458,0.638
+		c0.474,0.474,1.035,0.294,1.563,0.274C33.315,13.786,29.004,11,24.014,11z"/>
+</g>
+</svg>