Example #1
0
def test_sample_weight():
    group = numpy.random.binomial(1, .5, size=1000) == 1
    sample_weight = 1 / (group * 100 + 1.0)
    x = numpy.random.uniform(-10, 10, size=1000)
    y = numpy.abs(x)
    y[group] = numpy.abs(x[group] - 5)
    y += numpy.random.normal(0, 1, size=1000)
    model = Earth().fit(x[:, numpy.newaxis], y, sample_weight=sample_weight)
    
    # Check that the model fits better for the more heavily weighted group
    assert_true(model.score(x[group], y[group]) < model.score(
        x[numpy.logical_not(group)], y[numpy.logical_not(group)]))
    
    # Make sure that the score function gives the same answer as the trace
    pruning_trace = model.pruning_trace()
    rsq_trace = pruning_trace.rsq(model.pruning_trace().get_selected())
    assert_almost_equal(model.score(x, y, sample_weight=sample_weight),
                        rsq_trace)
Example #2
0
def test_sample_weight():
    group = numpy.random.binomial(1, .5, size=1000) == 1
    sample_weight = 1 / (group * 100 + 1.0)
    x = numpy.random.uniform(-10, 10, size=1000)
    y = numpy.abs(x)
    y[group] = numpy.abs(x[group] - 5)
    y += numpy.random.normal(0, 1, size=1000)
    model = Earth().fit(x[:, numpy.newaxis], y, sample_weight=sample_weight)

    # Check that the model fits better for the more heavily weighted group
    assert_true(model.score(x[group], y[group]) < model.score(
        x[numpy.logical_not(group)], y[numpy.logical_not(group)]))

    # Make sure that the score function gives the same answer as the trace
    pruning_trace = model.pruning_trace()
    rsq_trace = pruning_trace.rsq(model.pruning_trace().get_selected())
    assert_almost_equal(model.score(x, y, sample_weight=sample_weight),
                        rsq_trace)
Example #3
0
def test_untrained():

    model = Earth(**default_params)
    assert_raises(NotFittedError, model.predict, X)
    assert_raises(NotFittedError, model.transform, X)
    assert_raises(NotFittedError, model.predict_deriv, X)
    assert_raises(NotFittedError, model.score, X)

    # the following should be changed to raise NotFittedError
    assert_equal(model.forward_trace(), None)
    assert_equal(model.pruning_trace(), None)
    assert_equal(model.summary(), "Untrained Earth Model")
Example #4
0
def test_untrained():

    model = Earth(**default_params)
    assert_raises(NotFittedError, model.predict, X)
    assert_raises(NotFittedError, model.transform, X)
    assert_raises(NotFittedError, model.predict_deriv, X)
    assert_raises(NotFittedError, model.score, X)

    # the following should be changed to raise NotFittedError
    assert_equal(model.forward_trace(), None)
    assert_equal(model.pruning_trace(), None)
    assert_equal(model.summary(), "Untrained Earth Model")
Example #5
0
def test_untrained():
    # NotFittedError moved from utils.validation to exceptions
    # some time after 0.17.1
    try:
        from sklearn.exceptions import NotFittedError
    except ImportError:
        from sklearn.utils.validation import NotFittedError

    # Make sure calling methods that require a fitted Earth object
    # raises the appropriate exception when using a not yet fitted
    # Earth object
    model = Earth(**default_params)
    assert_raises(NotFittedError, model.predict, X)
    assert_raises(NotFittedError, model.transform, X)
    assert_raises(NotFittedError, model.predict_deriv, X)
    assert_raises(NotFittedError, model.score, X)

    # the following should be changed to raise NotFittedError
    assert_equal(model.forward_trace(), None)
    assert_equal(model.pruning_trace(), None)
    assert_equal(model.summary(), "Untrained Earth Model")
Example #6
0
def test_untrained():
    # NotFittedError moved from utils.validation to exceptions
    # some time after 0.17.1
    try:
        from sklearn.exceptions import NotFittedError
    except ImportError:
        from sklearn.utils.validation import NotFittedError
    
    # Make sure calling methods that require a fitted Earth object
    # raises the appropriate exception when using a not yet fitted 
    # Earth object
    model = Earth(**default_params)
    assert_raises(NotFittedError, model.predict, X)
    assert_raises(NotFittedError, model.transform, X)
    assert_raises(NotFittedError, model.predict_deriv, X)
    assert_raises(NotFittedError, model.score, X)

    # the following should be changed to raise NotFittedError
    assert_equal(model.forward_trace(), None)
    assert_equal(model.pruning_trace(), None)
    assert_equal(model.summary(), "Untrained Earth Model")