Example #1
0
def test_returning_value():
    func = ShapeFunction(_create_partition(np.linspace(1, 10, 10)),
                         -1 * np.linspace(1, 10, 11), 'test_1')

    assert func.get_value(10.45) == -10.0
    assert func.get_value(0.45) == -1.0
    assert func.get_value(2.0) == -2.8
Example #2
0
def test_func_add_fails_with_different_features():
    func1 = ShapeFunction(np.linspace(1, 3, 3), np.linspace(1, 3, 3), 'test_1')
    func2 = ShapeFunction(np.linspace(1.5, 10.5, 5), -1 * np.linspace(1, 10, 5), 'test_2')

    assert func1 != func2
    with pytest.raises(AssertionError):
        func1.add(func2)
Example #3
0
def test_func_add_fails_with_different_features():
    func1 = ShapeFunction(np.linspace(1, 3, 3), np.linspace(1, 3, 3), 'test_1')
    func2 = ShapeFunction(np.linspace(1.5, 10.5, 5),
                          -1 * np.linspace(1, 10, 5), 'test_2')

    assert func1 != func2
    with pytest.raises(AssertionError):
        func1.add(func2)
Example #4
0
def test_func_add_with_equal_splits():

    func1 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)),
                          np.linspace(1, 10, 11), 'test_1')
    func2 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)),
                          -1 * np.linspace(1, 10, 11), 'test_1')
    func3 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)),
                          np.zeros(11), 'test_1')
    assert func1.add(func2).equals(func3)
Example #5
0
def test_func_add_2():
    func1 = ShapeFunction([np.PINF], [0], 'test_1')
    func2 = ShapeFunction([0, 1, 2], [-1, 1, -2], 'test_1')

    assert func1 != func2

    func3 = ShapeFunction([0.0, 1.0, 2.0, np.PINF],
                          [-1.0, 1.0, -2.0, 0.0], 'test_1')

    assert func1.add(func2).equals(func3)
Example #6
0
def test_func_add_2():
    func1 = ShapeFunction([np.PINF], [0], 'test_1')
    func2 = ShapeFunction([0, 1, 2], [-1, 1, -2], 'test_1')

    assert func1 != func2

    func3 = ShapeFunction([0.0, 1.0, 2.0, np.PINF], [-1.0, 1.0, -2.0, 0.0],
                          'test_1')

    assert func1.add(func2).equals(func3)
Example #7
0
def test_func_add():
    func1 = ShapeFunction(np.linspace(1, 3, 3), np.linspace(1, 3, 3), 'test_1')
    func2 = ShapeFunction(np.linspace(1.5, 10.5, 5), -1 * np.linspace(1, 10, 5), 'test_1')

    assert func1 != func2

    func3 = ShapeFunction([1.0, 1.5, 2.0, 3.0, 3.75, 6.0, 8.25, 10.5],
                          [1.0, -1.0, 2.0, 3.0, -3.25, -5.5, -7.75, -10.0], 'test_1')

    assert func1.add(func2).equals(func3)
Example #8
0
def test_func_add():
    func1 = ShapeFunction(np.linspace(1, 3, 3), np.linspace(1, 3, 3), 'test_1')
    func2 = ShapeFunction(np.linspace(1.5, 10.5, 5),
                          -1 * np.linspace(1, 10, 5), 'test_1')

    assert func1 != func2

    func3 = ShapeFunction([1.0, 1.5, 2.0, 3.0, 3.75, 6.0, 8.25, 10.5],
                          [1.0, -1.0, 2.0, 3.0, -3.25, -5.5, -7.75, -10.0],
                          'test_1')

    assert func1.add(func2).equals(func3)
Example #9
0
def test_correct_scoring():
    func1 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), np.linspace(1, 10, 11), 'attr_1')
    func2 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), -1 * np.linspace(1, 10, 11), 'attr_2')

    gam = GAM(max_depth=3, max_leaf_nodes=5, random_state=42)
    gam.shapes = {'attr_1': func1,
                  'attr_2': func2}
    gam.feature_names = ['attr_1', 'attr_2']
    data = np.asarray([[1, 1], [-1, -1]])

    for vec in data:
        # the shape function cancel each other in their effect
        # hence the exponent is zero and the probability 0.5
        assert gam.score(vec) == [0.5, 0.5]
Example #10
0
    def _create_shape_functions(self, data):
        shapes = dict()
        for feat in self.feature_names:
            dim_idx = self._get_index_for_feature(feat)
            splits = np.unique(data[:, dim_idx].flatten()).tolist()
            vals = self._get_shape(dim_idx, splits)

            shapes[feat] = ShapeFunction(splits, vals, feat)

        self.shapes = shapes
Example #11
0
def test_pseudo_response():

    func1 = ShapeFunction(_create_partition([0]), [np.log(3), np.log(100)], 'attr_1')

    gam = GAM(max_depth=3, max_leaf_nodes=5, random_state=42)
    gam.shapes = {'attr_1': func1}
    gam.feature_names = ['attr_1']

    data = np.asarray([[-1], [1]])
    pseudo_resp = gam._get_pseudo_responses(data, [1, -1])
    np.testing.assert_almost_equal(pseudo_resp, [0.2, -1.9998],
                                   decimal=6,
                                   verbose=True,
                                   err_msg="Pseudo Response doesn't match")
Example #12
0
def test_correct_shape_addition():
    func1 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), np.linspace(1, 10, 11), 'attr_1')
    func2 = func1.add(func1).multiply(0.5)
    assert func1.equals(func2)
Example #13
0
def test_func_add_with_equal_splits():

    func1 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), np.linspace(1, 10, 11), 'test_1')
    func2 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), -1 * np.linspace(1, 10, 11), 'test_1')
    func3 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), np.zeros(11), 'test_1')
    assert func1.add(func2).equals(func3)
Example #14
0
def test_correct_shape_addition():
    func1 = ShapeFunction(_create_partition(np.linspace(1, 10, 10)),
                          np.linspace(1, 10, 11), 'attr_1')
    func2 = func1.add(func1).multiply(0.5)
    assert func1.equals(func2)
Example #15
0
def test_func_multiply():
    func1 = ShapeFunction(np.linspace(1, 3, 3), np.linspace(1, 3, 3), 'test_1')
    func2 = ShapeFunction(np.linspace(1, 3, 3), 0.5 * np.linspace(1, 3, 3),
                          'test_2')

    assert func1.multiply(0.5).equals(func2)
Example #16
0
def test_returning_value():
    func = ShapeFunction(_create_partition(np.linspace(1, 10, 10)), -1 * np.linspace(1, 10, 11), 'test_1')

    assert func.get_value(10.45) == -10.0
    assert func.get_value(0.45) == -1.0
    assert func.get_value(2.0) == -2.8
Example #17
0
def test_func_multiply():
    func1 = ShapeFunction(np.linspace(1, 3, 3), np.linspace(1, 3, 3), 'test_1')
    func2 = ShapeFunction(np.linspace(1, 3, 3), 0.5 * np.linspace(1, 3, 3), 'test_2')

    assert func1.multiply(0.5).equals(func2)