Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 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)])
Ejemplo n.º 6
0
    def test_construct_with_missing_endpoints(self):
        mock_feature = Feature('feature')

        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0)
        self.assertEqual(int_bin.left_end, float('-inf'))
        self.assertEqual(int_bin.right_end, float('inf'))

        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, left_end=1.0)
        self.assertEqual(int_bin.left_end, 1.0)
        self.assertEqual(int_bin.right_end, float('inf'))

        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, right_end=0.0)
        self.assertEqual(int_bin.left_end, float('-inf'))
        self.assertEqual(int_bin.right_end, 0.0)

        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, left_end=1.0, right_end=2.0)
        self.assertEqual(int_bin.left_end, 1.0)
        self.assertEqual(int_bin.right_end, 2.0)
Ejemplo n.º 7
0
    def test_contains(self):
        assignment = {'feature': 1.0}

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=1.0)
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        self.assertFalse(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=0.0)
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        self.assertTrue(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=-1.0)
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        self.assertFalse(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=2.0)
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        self.assertFalse(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=0.7)
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        self.assertTrue(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=float('inf'))
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0)
        self.assertFalse(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=float('-inf'))
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0)
        self.assertTrue(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=10.0)
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0)
        self.assertTrue(int_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value='non_numerical_value')
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0)
        self.assertRaises(ValueError, int_bin.contains, assignment)
Ejemplo n.º 8
0
    def test_evaluate(self):
        assignment = {'feature': 1.0}

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=1.0)
        int_bin = sc.LinearIntervalBin(mock_feature, -1.0, 0.5, 0.0, 1.0)
        self.assertEqual(int_bin.evaluate(assignment), 0.0)

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=0.5)
        int_bin = sc.LinearIntervalBin(mock_feature, -1.0, 0.5, 0.0, 1.0)
        self.assertEqual(int_bin.evaluate(assignment), -0.75)

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=0.0)
        int_bin = sc.LinearIntervalBin(mock_feature, -1.0, 0.5, 0.0, 1.0)
        self.assertEqual(int_bin.evaluate(assignment), -1.0)

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value='non_numerical_value')
        int_bin = sc.LinearIntervalBin(mock_feature, 0.0, 0.0)
        self.assertRaises(ValueError, int_bin.evaluate, assignment)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    def test_overlaps(self):
        mock_feature = Feature('feature')

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        self.assertTrue(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -1.0, 0.0)
        self.assertFalse(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 1.0, 2.0)
        self.assertFalse(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.5, 2.0)
        self.assertTrue(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -0.5, 0.5)
        self.assertTrue(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, -0.5, 2.0)
        self.assertTrue(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.IntervalBin(mock_feature, 0.0, -0.5, 2.0)
        self.assertTrue(bin1.overlaps(bin2))

        bin1 = sc.LinearIntervalBin(mock_feature, 0.0, 0.0, 0.0, 1.0)
        bin2 = sc.SetBin(mock_feature, 0.0, {0.0, 1.0})
        self.assertRaises(ValueError, bin1.overlaps, bin2)