Ejemplo n.º 1
0
class TestConstantBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.bf = ConstantBasisFunction()

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.empty(shape=(m, 10))
        assert_false(numpy.all(B[:, 0] == 1))
        self.bf.apply(self.X, B[:, 0])
        assert_true(numpy.all(B[:, 0] == 1))

    def test_apply_deriv(self):
        m, _ = self.X.shape
        b = numpy.empty(shape=m)
        j = numpy.empty(shape=m)
        self.bf.apply_deriv(self.X, b, j, 1)
        assert_true(numpy.all(b == 1))
        assert_true(numpy.all(j == 0))

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf))
        assert_true(self.bf == bf_copy)

    def test_smoothed_version(self):
        smoothed = self.bf._smoothed_version(None, {}, {})
        assert_true(type(smoothed) is ConstantBasisFunction)
Ejemplo n.º 2
0
class TestLinearBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.parent = ConstantBasisFunction()
        self.bf = LinearBasisFunction(self.parent, 1)

    def test_apply(self):
        m, n = self.X.shape
        B = numpy.ones(shape=(m, 10))
        self.bf.apply(self.X, B[:, 0])
        assert_true(numpy.all(B[:, 0] == self.X[:, 1]))

    def test_apply_deriv(self):
        m, _ = self.X.shape
        b = numpy.empty(shape=m)
        j = numpy.empty(shape=m)
        self.bf.apply_deriv(self.X, b, j, 1)
        numpy.testing.assert_almost_equal(b, self.X[:, 1])
        numpy.testing.assert_almost_equal(j, 1.0)

    def test_degree(self):
        assert_equal(self.bf.degree(), 1)

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf))
        assert_true(self.bf == bf_copy)

    def test_smoothed_version(self):
        translation = {self.parent: self.parent._smoothed_version(None, {}, {})}
        smoothed = self.bf._smoothed_version(self.parent, {}, translation)
        assert_true(type(smoothed) is LinearBasisFunction)
        assert_equal(smoothed.get_variable(), self.bf.get_variable())
Ejemplo n.º 3
0
 def __init__(self):
     super(self.__class__, self).__init__()
     self.parent = ConstantBasisFunction()
     self.bf1 = SmoothedHingeBasisFunction(self.parent,
                                           1.0, 0.0, 3.0, 10, 1,
                                           False)
     self.bf2 = SmoothedHingeBasisFunction(self.parent,
                                           1.0, 0.0, 3.0, 10, 1,
                                           True)
Ejemplo n.º 4
0
class TestHingeBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.parent = ConstantBasisFunction()
        self.bf = HingeBasisFunction(self.parent, 1.0, 10, 1, False)

    def test_getters(self):
        assert not self.bf.get_reverse()
        assert self.bf.get_knot() == 1.0
        assert self.bf.get_variable() == 1
        assert self.bf.get_knot_idx() == 10
        assert self.bf.get_parent() == self.parent

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.ones(shape=(m, 10))
        self.bf.apply(self.X, B[:, 0])
        numpy.testing.assert_almost_equal(
            B[:, 0],
            (self.X[:, 1] - 1.0) * (self.X[:, 1] > 1.0)
        )

    def test_apply_deriv(self):
        m, _ = self.X.shape
        b = numpy.empty(shape=m)
        j = numpy.empty(shape=m)
        self.bf.apply_deriv(self.X, b, j, 1)
        numpy.testing.assert_almost_equal(
            (self.X[:, 1] - 1.0) * (self.X[:, 1] > 1.0),
            b
        )
        numpy.testing.assert_almost_equal(1.0 * (self.X[:, 1] > 1.0),
                                          j)

    def test_degree(self):
        assert_equal(self.bf.degree(), 1)

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf))
        assert_true(self.bf == bf_copy)

    def test_smoothed_version(self):
        knot_dict = {self.bf: (.5, 1.5)}
        translation = {self.parent: self.parent._smoothed_version(None, {}, {})}
        smoothed = self.bf._smoothed_version(self.parent,
                                             knot_dict, translation)
        assert_true(type(smoothed) is SmoothedHingeBasisFunction)
        assert_true(translation[self.parent] is smoothed.get_parent())
        assert_equal(smoothed.get_knot_minus(), 0.5)
        assert_equal(smoothed.get_knot_plus(), 1.5)
        assert_equal(smoothed.get_knot(), self.bf.get_knot())
        assert_equal(smoothed.get_variable(), self.bf.get_variable())
Ejemplo n.º 5
0
from pyearth._basis import Basis, ConstantBasisFunction, HingeBasisFunction, LinearBasisFunction
from pyearth.export import export_python_function, export_python_string
from nose.tools import assert_almost_equal
import numpy
import six
from pyearth import Earth

numpy.random.seed(0)

basis = Basis(10)
constant = ConstantBasisFunction()
basis.append(constant)
bf1 = HingeBasisFunction(constant, 0.1, 10, 1, False, 'x1')
bf2 = HingeBasisFunction(constant, 0.1, 10, 1, True, 'x1')
bf3 = LinearBasisFunction(bf1, 2, 'x2')
basis.append(bf1)
basis.append(bf2)
basis.append(bf3)
X = numpy.random.normal(size=(100, 10))
B = numpy.empty(shape=(100, 4), dtype=numpy.float64)
basis.transform(X, B)
beta = numpy.random.normal(size=4)
y = numpy.empty(shape=100, dtype=numpy.float64)
y[:] = numpy.dot(B, beta) + numpy.random.normal(size=100)
default_params = {"penalty": 1}


def test_export_python_function():
    for smooth in (True, False):
        model = Earth(penalty=1, smooth=smooth).fit(X, y)
        export_model = export_python_function(model)
Ejemplo n.º 6
0
 def __init__(self):
     super(Container, self).__init__()
     self.parent = ConstantBasisFunction()
     self.bf = LinearBasisFunction(self.parent, 1)
Ejemplo n.º 7
0
class TestSmoothedHingeBasisFunction(BaseTestClass):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.parent = ConstantBasisFunction()
        self.bf1 = SmoothedHingeBasisFunction(self.parent,
                                              1.0, 0.0, 3.0, 10, 1,
                                              False)
        self.bf2 = SmoothedHingeBasisFunction(self.parent,
                                              1.0, 0.0, 3.0, 10, 1,
                                              True)

    def test_getters(self):
        assert not self.bf1.get_reverse()
        assert self.bf2.get_reverse()
        assert self.bf1.get_knot() == 1.0
        assert self.bf1.get_variable() == 1
        assert self.bf1.get_knot_idx() == 10
        assert self.bf1.get_parent() == self.parent
        assert self.bf1.get_knot_minus() == 0.0
        assert self.bf1.get_knot_plus() == 3.0

    def test_pickle_compatibility(self):
        bf_copy = pickle.loads(pickle.dumps(self.bf1))
        assert_equal(self.bf1, bf_copy)

    def test_smoothed_version(self):
        translation = {self.parent: self.parent._smoothed_version(None, {}, {})}
        smoothed = self.bf1._smoothed_version(self.parent, {}, translation)
        assert_equal(self.bf1, smoothed)

    def test_degree(self):
        assert_equal(self.bf1.degree(), 1)
        assert_equal(self.bf2.degree(), 1)

    def test_p_r(self):
        pplus = (2*3.0 + 0.0 - 3*1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        assert_equal(self.bf1.get_p(), pplus)
        assert_equal(self.bf1.get_r(), rplus)
        assert_equal(self.bf2.get_p(), pminus)
        assert_equal(self.bf2.get_r(), rminus)

    def test_apply(self):
        m, _ = self.X.shape
        B = numpy.ones(shape=(m, 10))
        self.bf1.apply(self.X, B[:, 0])
        self.bf2.apply(self.X, B[:, 1])
        pplus = (2 * 3.0 + 0.0 - 3 * 1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        c1 = numpy.ones(m)
        c1[self.X[:, 1] <= 0.0] = 0.0
        c1[(self.X[:, 1] > 0.0) &
           (self.X[:, 1] < 3.0)] = (pplus*((self.X[(self.X[:, 1] > 0.0) &
                                           (self.X[:, 1] < 3.0), 1] - 0.0)**2) +
                                    rplus*((self.X[(self.X[:, 1] > 0.0) &
                                           (self.X[:, 1] < 3.0), 1] - 0.0)**3))
        c1[self.X[:, 1] >= 3.0] = self.X[self.X[:, 1] >= 3.0, 1] - 1.0
        c2 = numpy.ones(m)
        c2[self.X[:, 1] >= 3.0] = 0.0
        c2.flat[self.X[:, 1] <= 0.0] = -1 * (self.X[self.X[:, 1] <= 0.0] - 1.0)
        c2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**2) +
            rminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**3)
        )
        numpy.testing.assert_almost_equal(B[:, 0], c1)
        numpy.testing.assert_almost_equal(B[:, 1], c2)

    def test_apply_deriv(self):
        m, _ = self.X.shape
        pplus = (2*3.0 + 0.0 - 3*1.0) / ((3.0 - 0.0)**2)
        rplus = (2*1.0 - 3.0 - 0.0) / ((3.0 - 0.0)**3)
        pminus = (3*1.0 - 2*0.0 - 3.0) / ((0.0 - 3.0)**2)
        rminus = (0.0 + 3.0 - 2*1.0) / ((0.0 - 3.0)**3)
        c1 = numpy.ones(m)
        c1[self.X[:, 1] <= 0.0] = 0.0
        c1[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pplus*((self.X[(self.X[:, 1] > 0.0) &
                   (self.X[:, 1] < 3.0), 1] - 0.0)**2) +
            rplus*((self.X[(self.X[:, 1] > 0.0) &
                   (self.X[:, 1] < 3.0), 1] - 0.0)**3))
        c1[self.X[:, 1] >= 3.0] = self.X[self.X[:, 1] >= 3.0, 1] - 1.0
        c2 = numpy.ones(m)
        c2[self.X[:, 1] >= 3.0] = 0.0
        c2.flat[self.X[:, 1] <= 0.0] = -1 * (self.X[self.X[:, 1] <= 0.0] - 1.0)
        c2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            pminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**2) +
            rminus*((self.X[(self.X[:, 1] > 0.0) &
                    (self.X[:, 1] < 3.0), 1] - 3.0)**3)
        )
        b1 = numpy.empty(shape=m)
        j1 = numpy.empty(shape=m)
        b2 = numpy.empty(shape=m)
        j2 = numpy.empty(shape=m)
        cp1 = numpy.ones(m)
        cp1[self.X[:, 1] <= 0.0] = 0.0
        cp1[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            2.0*pplus*((self.X[(self.X[:, 1] > 0.0) &
                       (self.X[:, 1] < 3.0), 1] - 0.0)) +
            3.0*rplus*((self.X[(self.X[:, 1] > 0.0) &
                       (self.X[:, 1] < 3.0), 1] - 0.0)**2)
        )
        cp1[self.X[:, 1] >= 3.0] = 1.0
        cp2 = numpy.ones(m)
        cp2[self.X[:, 1] >= 3.0] = 0.0
        cp2[self.X[:, 1] <= 0.0] = -1.0
        cp2[(self.X[:, 1] > 0.0) & (self.X[:, 1] < 3.0)] = (
            2.0*pminus*((self.X[(self.X[:, 1] > 0.0) &
                        (self.X[:, 1] < 3.0), 1] - 3.0)) +
            3.0*rminus*((self.X[(self.X[:, 1] > 0.0) &
                        (self.X[:, 1] < 3.0), 1] - 3.0)**2))
        self.bf1.apply_deriv(self.X, b1, j1, 1)
        self.bf2.apply_deriv(self.X, b2, j2, 1)
        numpy.testing.assert_almost_equal(b1, c1)
        numpy.testing.assert_almost_equal(b2, c2)
        numpy.testing.assert_almost_equal(j1, cp1)
        numpy.testing.assert_almost_equal(j2, cp2)
Ejemplo n.º 8
0
 def __init__(self):
     super(self.__class__, self).__init__()
     self.bf = ConstantBasisFunction()
Ejemplo n.º 9
0
 def __init__(self):
     super(self.__class__, self).__init__()
     self.parent = ConstantBasisFunction()
     self.bf = LinearBasisFunction(self.parent, 1)
Ejemplo n.º 10
0
 def __init__(self):
     super(self.__class__, self).__init__()
     self.parent = ConstantBasisFunction()
     self.bf = HingeBasisFunction(self.parent, 1.0, 10, 1, False)
 def __init__(self):
     super(Container, self).__init__()
     self.parent = ConstantBasisFunction()
     self.bf = MissingnessBasisFunction(self.parent, 1, True)
     self.child = HingeBasisFunction(self.bf, 1.0, 10, 1, False)