Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Orange/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def __call__(self, data, progress_callback=None):
progress_callback(0.1, "Fitting...")
model = self._fit_model(data)
model.used_vals = [np.unique(y).astype(int) for y in data.Y[:, None].T]
model.domain = data.domain
if not hasattr(model, "domain") or model.domain is None:
# some models set domain themself and it should be respected
# e.g. calibration learners set the base_learner's domain which
# would be wrongly overwritten if we set it here for any model
model.domain = data.domain
model.supports_multiclass = self.supports_multiclass
model.name = self.name
model.original_domain = origdomain
Expand Down
29 changes: 29 additions & 0 deletions Orange/classification/tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from Orange.base import Model
from Orange.classification import LogisticRegressionLearner
from Orange.classification.calibration import \
ThresholdLearner, ThresholdClassifier, \
CalibratedLearner, CalibratedClassifier
Expand Down Expand Up @@ -65,6 +66,18 @@ def test_non_binary_base(self):
base_model.domain.class_var.is_discrete = False
self.assertRaises(ValueError, ThresholdClassifier, base_model, 0.5)

def test_np_data(self):
"""
Test ThresholdModel with numpy data.
When passing numpy data to model they should be already
transformed to models domain since model do not know how to do it.
"""
data = Table('heart_disease')
base_learner = LogisticRegressionLearner()
model = ThresholdLearner(base_learner)(data)
res = model(model.data_to_model_domain(data).X)
self.assertTupleEqual((len(data),), res.shape)


class TestThresholdLearner(unittest.TestCase):
@patch("Orange.evaluation.performance_curves.Curves.from_results")
Expand Down Expand Up @@ -169,6 +182,18 @@ def test_calibrated_probs(self):
calprobs = self.model.calibrated_probs(self.probs)
np.testing.assert_almost_equal(calprobs, expprobs)

def test_np_data(self):
"""
Test CalibratedClassifier with numpy data.
When passing numpy data to model they should be already
transformed to models domain since model do not know how to do it.
"""
data = Table('heart_disease')
base_learner = LogisticRegressionLearner()
model = CalibratedLearner(base_learner)(data)
res = model(model.data_to_model_domain(data).X)
self.assertTupleEqual((len(data),), res.shape)


class TestCalibratedLearner(unittest.TestCase):
@patch("Orange.classification.calibration._SigmoidCalibration.fit")
Expand Down Expand Up @@ -207,3 +232,7 @@ def test_fit_storage(self, test_on_training, sigmoid_fit):
for call, cls_probs in zip(sigmoid_fit.call_args_list,
res.probabilities[0].T):
np.testing.assert_equal(call[0][0], cls_probs)


if __name__ == "__main__":
unittest.main()
23 changes: 16 additions & 7 deletions Orange/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,29 @@ def test_result_shape_numpy(self):
Test whether results shapes are correct when testing on numpy data
"""
iris = Table('iris')
iris_bin = Table(
Domain(
iris.domain.attributes,
DiscreteVariable("iris", values=["a", "b"])
),
iris.X[:100], iris.Y[:100]
)
for learner in all_learners():
with self.subTest(learner.__name__):
try:
model = learner()(iris)
except TypeError:
# cannot be tested with default parameters
continue
transformed_iris = model.data_to_model_domain(iris)
args = []
if learner in (ThresholdLearner, CalibratedLearner):
args = [LogisticRegressionLearner()]
data = iris_bin if learner is ThresholdLearner else iris
model = learner(*args)(data)
transformed_iris = model.data_to_model_domain(data)

res = model(transformed_iris.X[0:5])
self.assertTupleEqual((5,), res.shape)

res = model(transformed_iris.X[0:1], model.Probs)
self.assertTupleEqual((1, 3), res.shape)
self.assertTupleEqual(
(1, len(data.domain.class_var.values)), res.shape
)


class ExpandProbabilitiesTest(unittest.TestCase):
Expand Down