Exemple #1
0
    def test_evaluate(self):
        assignment = {'feature': 0.0}

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=2.0)
        int_bin = sc.SetBin(mock_feature, -1.0, {0.0, 1.0})
        self.assertEqual(int_bin.evaluate(assignment), 0.0)

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=1.0)
        int_bin = sc.SetBin(mock_feature, -1.0, {0.0, 1.0})
        self.assertEqual(int_bin.evaluate(assignment), -1.0)
Exemple #2
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)
Exemple #3
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)
Exemple #4
0
    def test_construct_with_empty_values(self):
        mock_feature = Feature('feature')
        self.assertRaises(ValueError, sc.SetBin, mock_feature, 0.0, {})

        mock_feature = Feature('feature')
        set_bin = sc.SetBin(mock_feature, 0.0, {0, 1})
        self.assertSetEqual(set_bin.values, {0, 1})
Exemple #5
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)
Exemple #6
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)])
Exemple #7
0
    def test_contains(self):
        assignment = {'feature': 1.0}

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=1.0)
        set_bin = sc.SetBin(mock_feature, 0.0, {0.0})
        self.assertFalse(set_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=0.0)
        set_bin = sc.SetBin(mock_feature, 0.0, {0.0})
        self.assertTrue(set_bin.contains(assignment))

        mock_feature = Feature('feature')
        mock_feature.evaluate = MagicMock(return_value=0.5)
        set_bin = sc.SetBin(mock_feature, 0.0, {0.0, 1.0})
        self.assertFalse(set_bin.contains(assignment))
Exemple #8
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)
Exemple #9
0
    def test_overlaps(self):
        mock_feature = Feature('feature')

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

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

        bin1 = sc.SetBin(mock_feature, 0.0, {1.0})
        bin2 = sc.SetBin(mock_feature, 0.0, {-1.0, 1.0})
        self.assertTrue(bin1.overlaps(bin2))

        bin1 = sc.SetBin(mock_feature, 0.0, {0.0})
        bin2 = sc.SetBin(mock_feature, 0.0, {-1.0, 1.0})
        self.assertFalse(bin1.overlaps(bin2))

        bin1 = sc.SetBin(mock_feature, 0.0, {0.0, 1.0})
        bin2 = sc.SetBin(mock_feature, 0.0, {0.0, 1.0})
        self.assertTrue(bin1.overlaps(bin2))
Exemple #10
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)
Exemple #11
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)
Exemple #12
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)