def test_error_model(self): for loo in [False, True]: icr = InductiveRegressor( ErrorModelNC(LinearRegressionLearner(), LinearRegressionLearner(), loo=loo), self.train, self.calibrate) lo, hi = icr(self.test.x, 0.1) self.assertLess(hi - lo, 30.0) icr = InductiveRegressor(AbsError(RandomForestRegressionLearner())) r = run(icr, 0.1, CrossSampler(Table('housing'), 20)) self.assertGreater(r.accuracy(), 0.85) print(r.accuracy(), r.median_range(), r.interdecile_mean()) icr = InductiveRegressor( ErrorModelNC(RandomForestRegressionLearner(), LinearRegressionLearner())) r = run(icr, 0.1, CrossSampler(Table('housing'), 20)) self.assertGreater(r.accuracy(), 0.85) print(r.accuracy(), r.median_range(), r.interdecile_mean()) icr = InductiveRegressor( ErrorModelNC(RandomForestRegressionLearner(), LinearRegressionLearner(), loo=True)) r = run(icr, 0.1, CrossSampler(Table('housing'), 20)) self.assertGreater(r.accuracy(), 0.85) print(r.accuracy(), r.median_range(), r.interdecile_mean())
def test_regression_scorer(self): data = Table('housing') learner = RandomForestRegressionLearner() scores = learner.score_data(data) self.assertEqual(['LSTAT', 'RM'], sorted([data.domain.attributes[i].name for i in np.argsort(scores)[-2:]]))
def test_regression_scorer(self): learner = RandomForestRegressionLearner() scores = learner.score_data(self.housing) self.assertEqual(['LSTAT', 'RM'], sorted([ self.housing.domain.attributes[i].name for i in np.argsort(scores[0])[-2:] ]))
def test_regression_scorer(self): data = Table('housing') learner = RandomForestRegressionLearner() scores = learner.score_data(data) self.assertEqual(['LSTAT', 'RM'], sorted([ data.domain.attributes[i].name for i in np.argsort(scores)[-2:] ]))
def setUpClass(cls): super().setUpClass() cls.heart = Table("heart_disease") cls.housing = Table("housing") kwargs = {"random_state": 0} cls.rf_cls = RandomForestLearner(**kwargs)(cls.heart) cls.rf_reg = RandomForestRegressionLearner(**kwargs)(cls.housing)
def setUpClass(cls): super().setUpClass() cls.iris = Table("iris") cls.heart = Table("heart_disease") cls.housing = Table("housing") cls.rf_cls = RandomForestLearner(random_state=0)(cls.iris) cls.rf_reg = RandomForestRegressionLearner(random_state=0)(cls.housing)
def test_predict_numpy_reg(self): table = Table('housing') forest = RandomForestRegressionLearner() model = forest(table) pred = model(table.X) self.assertEqual(len(table), len(pred)) self.assertTrue(all(pred) > 0)
def test_predict_single_instance_reg(self): table = Table('housing') forest = RandomForestRegressionLearner() model = forest(table) for ins in table: pred = model(ins) self.assertTrue(pred > 0)
def test_apply(self): self.widget.set_data(self.data) self.widget.apply() self.assertNotEqual(self.widget.plot_item, None) self.assertNotEqual(self.widget.scatterplot_item, None) self.widget.set_data(None) self.widget.apply() # TODO: output will be checked when it available in GuiTest # check if function does not change plots that are None according to test_set_data self.assertEqual(self.widget.plot_item, None) self.assertEqual(self.widget.scatterplot_item, None) self.widget.set_data(self.data) self.widget.set_learner(LinearRegressionLearner()) self.widget.apply() self.assertNotEqual(self.widget.plot_item, None) self.assertNotEqual(self.widget.scatterplot_item, None) self.widget.set_learner(RandomForestRegressionLearner()) self.widget.apply() self.assertNotEqual(self.widget.plot_item, None) self.assertNotEqual(self.widget.scatterplot_item, None) self.widget.set_preprocessor((Normalize(), )) self.assertNotEqual(self.widget.plot_item, None) self.assertNotEqual(self.widget.scatterplot_item, None)
def test_get_regression_trees(self): n = 5 forest = RandomForestRegressionLearner(n_estimators=n) model = forest(self.housing) self.assertEqual(len(model.trees), n) tree = model.trees[0] tree(self.housing[0])
def test_input_learner_disconnect(self): """Check base learner after disconnecting learner on the input""" self.send_signal("Learner", RandomForestRegressionLearner()) self.assertIsInstance(self.widget.base_estimator, RandomForestRegressionLearner) self.send_signal("Learner", None) self.assertEqual(self.widget.base_estimator, self.widget.DEFAULT_BASE_ESTIMATOR)
def test_continuous_class(self): data = self.housing model = RandomForestRegressionLearner(random_state=0)(data) res = permutation_feature_importance(model, data, MSE(), self.n_repeats) shape = len(data.domain.attributes), self.n_repeats self.assertEqual(res[0].shape, shape) self.assertEqual(res[1], [a.name for a in data.domain.attributes])
def test_wrap_score_reg(self): data = self.housing_missing model = RandomForestRegressionLearner(random_state=0)(data) scorer = _wrap_score(MSE(), _check_model(model, data)) mocked_model = Mock(wraps=model) baseline_score = scorer(mocked_model, data) mocked_model.assert_called_once() self.assertAlmostEqual(baseline_score, 2, 0)
def test_abs_error_rf(self): icr = InductiveRegressor( AbsErrorRF(RandomForestRegressionLearner(), RandomForestRegressor()), self.train, self.calibrate) lo, hi = icr(self.test.x, 0.1) self.assertLess(hi - lo, 30.0) icr = InductiveRegressor( AbsErrorRF(LinearRegressionLearner(), RandomForestRegressor()), self.train, self.calibrate) lo, hi = icr(self.test.x, 0.1) self.assertLess(hi - lo, 30.0) icr = InductiveRegressor( AbsErrorRF(RandomForestRegressionLearner(), RandomForestRegressor())) r = run(icr, 0.1, CrossSampler(Table('housing'), 10)) self.assertGreater(r.accuracy(), 0.85) print(r.median_range(), r.interdecile_mean())
def test_missing_target(self): data = self.housing.copy() data.Y[0] = np.nan rf = RandomForestRegressionLearner(random_state=0)(data) self.send_signal(self.widget.Inputs.data, data) self.send_signal(self.widget.Inputs.model, rf) self.wait_until_finished() self.assertDomainInPlot(self.widget.plot, data.domain) self.assertTrue(self.widget.Warning.missing_target.is_shown()) self.send_signal(self.widget.Inputs.data, None) self.assertFalse(self.widget.Warning.missing_target.is_shown())
def test_error_message_cleared_when_valid_learner_on_input(self): # Disconnecting an invalid learner should use the default one and hide # the error self.send_signal("Learner", KNNRegressionLearner()) self.send_signal('Learner', None) self.assertFalse(self.widget.Error.no_weight_support.is_shown(), 'Error message was not hidden on input disconnect') # Connecting a valid learner should also reset the error message self.send_signal("Learner", KNNRegressionLearner()) self.send_signal('Learner', RandomForestRegressionLearner()) self.assertFalse( self.widget.Error.no_weight_support.is_shown(), 'Error message was not hidden when a valid learner appeared on ' 'input')
def test_RandomForestRegression(self): forest = RandomForestRegressionLearner() results = CrossValidation(self.housing, [forest], k=10) _ = RMSE(results)
def test_check_model_reg_false(self): data = self.housing model = RandomForestRegressionLearner(random_state=0)(data) self.assertFalse(_check_model(model, data))
def test_regression_scorer(self): learner = RandomForestRegressionLearner() scores = learner.score_data(self.house) self.assertEqual(['LSTAT', 'RM'], sorted([self.house.domain.attributes[i].name for i in np.argsort(scores[0])[-2:]]))
def test_RandomForestRegression(self): table = Table('housing') forest = RandomForestRegressionLearner() results = CrossValidation(table, [forest], k=10) _ = RMSE(results)
def test_predict_single_instance_reg(self): forest = RandomForestRegressionLearner() model = forest(self.housing) for ins in self.housing: pred = model(ins) self.assertGreater(pred, 0)
def test_predict_numpy_reg(self): forest = RandomForestRegressionLearner() model = forest(self.housing) pred = model(self.housing.X) self.assertEqual(len(self.housing), len(pred)) self.assertGreater(all(pred), 0)
for s, v in exps]) neg_data.append((y_upper, y_lower)) neg_labels.append(attributes[k].name) return (np.arange(shap_values.shape[0]), pos_data, neg_data, pos_labels, neg_labels) if __name__ == "__main__": import matplotlib.pyplot as plt from Orange.classification import RandomForestLearner from Orange.regression import RandomForestRegressionLearner table = Table("housing") if table.domain.has_continuous_class: model_ = RandomForestRegressionLearner(n_estimators=10, random_state=0)(table) else: model_ = RandomForestLearner(n_estimators=10, random_state=0)(table) shap_values_, transformed_, _, base_value_ = \ compute_shap_values(model_, table[:50], table) idxs = get_instance_ordering(shap_values_[0], None, transformed_, SIMILARITY_ORDER) x_data_, pos_data_, neg_data_, pos_labels_, neg_labels_ = \ prepare_force_plot_data_multi_inst( shap_values_[0][idxs], base_value_[0], transformed_.domain ) for y1, y2 in pos_data_:
self.shutdown() super().onDeleteWidget() def sizeHint(self) -> QSizeF: sh = self.controlArea.sizeHint() return sh.expandedTo(QSize(700, 700)) def send_report(self): if not self.data or not self.background_data or not self.model: return items = {"Target class": "None"} if self.model.domain.has_discrete_class: class_var = self.model.domain.class_var items["Target class"] = class_var.values[self.target_index] self.report_items(items) self.report_plot() if __name__ == "__main__": # pragma: no cover from Orange.classification import RandomForestLearner from Orange.regression import RandomForestRegressionLearner table = Table("heart_disease") if table.domain.has_continuous_class: rf_model = RandomForestRegressionLearner(random_state=42)(table) else: rf_model = RandomForestLearner(random_state=42)(table) WidgetPreview(OWExplainPrediction).run(set_background_data=table, set_data=table[:1], set_model=rf_model)
self.Outputs.scores.send(scores) def send_report(self): if not self.data or not self.background_data or not self.model: return items = {"Target class": "None"} if self.model.domain.has_discrete_class: class_var = self.model.domain.class_var items["Target class"] = class_var.values[self.target_index] self.report_items(items) self.report_plot() def set_visual_settings(self, key: Tuple[str, str, str], value: Any): self.visual_settings[key] = value self.graph.parameter_setter.set_parameter(key, value) if __name__ == "__main__": from Orange.classification import RandomForestLearner from Orange.regression import RandomForestRegressionLearner table = Table("housing") kwargs = {"n_estimators": 10, "random_state": 0} if table.domain.has_continuous_class: model_ = RandomForestRegressionLearner(**kwargs)(table) else: model_ = RandomForestLearner(**kwargs)(table) WidgetPreview(OWExplainPredictions).run( set_background_data=table, set_data=table[:50], set_model=model_ )