Пример #1
0
def test_data_must_have_length():
    """
    Test that an error is raised when the data has no length.
    """

    input_data = np.array([])
    k = NaiveKDE(kernel='gaussian', bw=1)

    with pytest.raises(ValueError):
        k.fit(np.array(input_data))
Пример #2
0
def test_grid_must_have_length():
    """
    Test that an error is raised when the grid has no length.
    """

    input_data = np.array([3, 4])
    k = NaiveKDE(kernel="gaussian", bw=1)

    with pytest.raises(ValueError):
        k.fit(np.array(input_data))
        k.evaluate(np.array([]))
Пример #3
0
def test_1d_data_inputs(bw, kernel):
    """
    Test that passing data as lists, tuples and NumPy arrays are all ok.
    """
    input_data = [1, 2, 5, 10]

    k = NaiveKDE(kernel=kernel, bw=bw)
    # Arrays
    k.fit(np.array(input_data))
    x_1, y_1 = k.evaluate()

    # Lists
    k.fit(list(input_data))
    x_2, y_2 = k.evaluate()

    # Tuples
    k.fit(tuple(input_data))
    x_3, y_3 = k.evaluate()

    # Arrays of shape (obs, dims)
    k.fit(np.array(input_data).reshape(-1, 1))
    x_4, y_4 = k.evaluate()

    assert np.allclose(y_1, y_2)
    assert np.allclose(y_2, y_3)
    assert np.allclose(y_3, y_4)
Пример #4
0
def test_common_API_patterns():
    """
    Test common API patterns.
    """
    # Simplest way, with auto grid
    data = [1, 2, 5, 10]
    x, y = NaiveKDE().fit(data).evaluate()

    # Using a pre-defined grid
    x = np.linspace(-10, 50)
    y1 = NaiveKDE().fit(data).evaluate(x)

    # No chaining
    k = NaiveKDE()
    k.fit(data)
    y2 = k.evaluate(x)

    assert np.allclose(y1, y2)
Пример #5
0
    import matplotlib.pyplot as plt
    from KDEpy.NaiveKDE import NaiveKDE

    # Comparing tree and naive
    # -----------------------------------------
    data = [3, 3.5, 4, 6, 8]
    kernel = 'triweight'
    bw = [3, 0.3, 1, 0.3, 2]
    weights = [1, 1, 1, 1, 1]

    plt.figure(figsize=(10, 4))
    plt.title('Basic example of the naive KDE')

    plt.subplot(1, 2, 1)
    kde = NaiveKDE(kernel=kernel, bw=bw)
    kde.fit(data, weights)
    x = np.linspace(0, 10, num=1024)
    for d, b in zip(data, bw):
        k = NaiveKDE(kernel=kernel, bw=b).fit([d]).evaluate(x) / len(data)
        plt.plot(x, k, color='k', ls='--')

    y = kde.evaluate(x)
    plt.plot(x, y)
    plt.scatter(data, np.zeros_like(data))

    plt.subplot(1, 2, 2)
    kde = TreeKDE(kernel=kernel, bw=bw)
    kde.fit(data, weights)
    x = np.linspace(0, 10, num=1024)
    for d, b in zip(data, bw):
        k = NaiveKDE(kernel=kernel, bw=b).fit([d]).evaluate(x) / len(data)