def test_evaluate(self): assignment = {'feature': 1.0} mock_feature = Feature('feature') mock_feature.evaluate = MagicMock(return_value=1.0) mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.1, 0.25, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, -0.1, -0.25, 0.0, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 1.0, 1.0, 2.0) ] partition = sc.Partition(mock_bins) self.assertEqual(partition.evaluate(assignment), 1.0) mock_feature = Feature('feature') mock_feature.evaluate = MagicMock(return_value=0.5) mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.1, 0.25, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, -0.1, -0.25, 0.0, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 1.0, 1.0, 2.0) ] partition = sc.Partition(mock_bins) self.assertEqual(partition.evaluate(assignment), -0.225) mock_feature = Feature('feature') mock_feature.evaluate = MagicMock(return_value=0.0) mock_bins = [ sc.SetBin(mock_feature, 0.1, {-1.0, 0.0}), sc.SetBin(mock_feature, -0.1, {0.5, 1.0}), sc.SetBin(mock_feature, 0.0, {1.5, 2.0}) ] partition = sc.Partition(mock_bins) self.assertEqual(partition.evaluate(assignment), 0.1) mock_feature = Feature('feature') mock_feature.evaluate = MagicMock(return_value=1.1) mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.1, 0.25, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, -0.1, -0.25, 0.0, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 1.0, 1.5, 2.0) ] partition = sc.Partition(mock_bins) self.assertRaises(ValueError, partition.evaluate, assignment) mock_feature = Feature('feature') mock_feature.evaluate = MagicMock(return_value=0.1) mock_bins = [ sc.SetBin(mock_feature, 0.1, {-1.0, 0.0}), sc.SetBin(mock_feature, -0.1, {0.5, 1.0}), sc.SetBin(mock_feature, 0.0, {1.5, 2.0}) ] partition = sc.Partition(mock_bins) self.assertRaises(ValueError, partition.evaluate, assignment)
def test_total_bins(self): mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.5, -2.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature1, -1.0, 2.0, 0.0, 1.0), sc.LinearIntervalBin(mock_feature1, -1.0, 1.0, 1.0, 2.0) ]), sc.Partition([ sc.SetBin(mock_feature2, 1.0, {-1.0, 0.0}), sc.SetBin(mock_feature2, -1.0, {1.0, 2.0}) ]) ] scorecard = sc.Scorecard(partitions) self.assertEqual(scorecard.total_bins, 5)
def test_bin_overlap_violation(self): mock_feature = Feature('feature') mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -0.1, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0) ] self.assertRaises(ValueError, sc.Partition, mock_bins) mock_feature = Feature('feature') mock_bins = [ sc.SetBin(mock_feature, 0.0, {-1.0, 0.0}), sc.SetBin(mock_feature, 0.0, {-0.1, 1.1}), sc.SetBin(mock_feature, 0.0, {1.0, 2.0}) ] partition = sc.Partition(mock_bins) self.assertEqual(partition.num_bins, 3) mock_feature = Feature('feature') mock_bins = [ sc.SetBin(mock_feature, 0.0, {-1.0, 0.0}), sc.SetBin(mock_feature, 0.0, {-0.1, 1.0}), sc.SetBin(mock_feature, 0.0, {1.0, 2.0}) ] self.assertRaises(ValueError, sc.Partition, mock_bins)
def test_bin_type_variation(self): mock_feature = Feature('feature') mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 2.0, 3.0) ] partition = sc.Partition(mock_bins) self.assertEqual(partition.num_bins, 4) mock_feature = Feature('feature') mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.SetBin(mock_feature, 0.0, {0.0, 0.5}), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 2.0, 3.0) ] self.assertRaises(ValueError, sc.Partition, mock_bins) mock_feature = Feature('feature') mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0), sc.IntervalBin(mock_feature, 0.0, 0.0, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 2.0, 3.0) ] self.assertRaises(ValueError, sc.Partition, mock_bins)
def test_bins_per_feature(self): mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.5, -2.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature1, -1.0, 2.0, 0.0, 1.0), sc.LinearIntervalBin(mock_feature1, -1.0, 1.0, 1.0, 2.0) ]), sc.Partition([ sc.SetBin(mock_feature2, 1.0, {-1.0, 0.0}), sc.SetBin(mock_feature2, -1.0, {1.0, 2.0}) ]) ] scorecard = sc.Scorecard(partitions) self.assertListEqual(scorecard.bins_per_feature, [(mock_feature1, 3), (mock_feature2, 2)])
def test_evaluate(self): assignment = {'feature1': 0.5, 'feature2': 0.0} mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') mock_feature1.evaluate = MagicMock(return_value=assignment['feature1']) mock_feature2.evaluate = MagicMock(return_value=assignment['feature2']) partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.5, -2.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature1, -1.0, 2.0, 0.0, 1.0) ]), sc.Partition([ sc.SetBin(mock_feature2, 1.0, {-1.0, 0.0}), sc.SetBin(mock_feature2, -1.0, {1.0, 2.0}) ]) ] scorecard = sc.Scorecard(partitions) self.assertEqual(scorecard.evaluate(assignment), 1.0) mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') mock_feature1.evaluate = MagicMock(return_value=assignment['feature1']) mock_feature2.evaluate = MagicMock(return_value=assignment['feature2']) partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.5, -2.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature1, -1.0, 2.0, 0.0, 1.0) ]), sc.Partition([ sc.SetBin(mock_feature2, 1.0, {-1.0, 0.0}), sc.SetBin(mock_feature2, -1.0, {1.0, 2.0}) ]) ] scorecard = sc.Scorecard(partitions, bias=-10) self.assertEqual(scorecard.evaluate(assignment), -9.0)
def test_feature_mismatch_violation(self): mock_feature = Feature('feature') mock_bins = [ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0) ] partition = sc.Partition(mock_bins) self.assertEqual(partition.feature, mock_feature) mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') mock_bins = [ sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature2, 0.0, 0.0, 0.0, 1.0), sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, 1.0, 2.0) ] self.assertRaises(ValueError, sc.Partition, mock_bins)
def test_repeated_features(self): mock_feature = Feature('feature') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0) ]) ] scorecard = sc.Scorecard(partitions) self.assertEqual(scorecard.num_features, 1) mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, 0.0, 1.0) ]), sc.Partition([ sc.SetBin(mock_feature2, 0.0, {-1.0, 0.0}), sc.SetBin(mock_feature2, 0.0, {1.0, 2.0}) ]) ] scorecard = sc.Scorecard(partitions) self.assertEqual(scorecard.num_features, 2) mock_feature = Feature('feature') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0) ]), sc.Partition([ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0) ]) ] self.assertRaises(ValueError, sc.Scorecard, partitions) mock_feature = Feature('feature') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0) ]), sc.Partition([ sc.SetBin(mock_feature, 0.0, {-1.0, 0.0}), sc.SetBin(mock_feature, 0.0, {1.0, 2.0}) ]) ] self.assertRaises(ValueError, sc.Scorecard, partitions) mock_feature1 = Feature('feature1') mock_feature2 = Feature('feature2') partitions = [ sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, 0.0, 1.0) ]), sc.Partition([ sc.LinearIntervalBin(mock_feature2, 0.0, 0.0, -1.0, 0.0), sc.LinearIntervalBin(mock_feature2, 0.0, 0.0, 1.0, 2.0) ]), sc.Partition([ sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, -2.0, -1.0), sc.LinearIntervalBin(mock_feature1, 0.0, 0.0, 5.0, 10.0) ]) ] self.assertRaises(ValueError, sc.Scorecard, partitions)