Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid must be sorted error #118

Closed
gabrieldernbach opened this issue Oct 25, 2022 · 2 comments
Closed

Grid must be sorted error #118

gabrieldernbach opened this issue Oct 25, 2022 · 2 comments

Comments

@gabrieldernbach
Copy link

gabrieldernbach commented Oct 25, 2022

I want to run FFTKDE on a custom 2D grid, and found a related issue here:
Coming from #15

When I sue the code provided there (with KDEpy Version '1.1.0') I get a grid-must be sorted error

# Create bimodal 2D data
data = np.array([[-1, 1], [0, 1], [1, 1], [-1, -1], [0, -1], [1, -1]])

# Create 2D grid
grid_x = np.linspace(-2, 2, 2 ** 5)
grid_y = np.linspace(-2, 2, 2 ** 4)
grid = np.stack(np.meshgrid(grid_x, grid_y), -1).reshape(-1, 2)

# density estimates
y1 = KDEpy.FFTKDE(bw=0.2).fit(data).evaluate(grid)

What is the recommended way to provide grids then?
The documentation only provides a 1D case (y = FFTKDE().fit(data)(np.linspace(-10, 10, num=2**12)))

@gabrieldernbach gabrieldernbach changed the title Grid must be sorted Grid must be sorted error Oct 25, 2022
@trungnth
Copy link

I'm a python novice but there's an example in 2D case:

    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib.colors import LightSource
    from matplotlib import cm
    import matplotlib.pyplot as plt
    import numpy as np

    ax = plt.subplot(2, 3, 4, projection="3d")
    plt.title("Kernels normalized in any\ndimension for any $p$-norm", fontsize=6)
    data = np.array([[0, 0]])
    grid_points = 2**6  # Grid points in each dimension
    
    from KDEpy import FFTKDE
    x, z = FFTKDE(kernel="gaussian", bw=1, norm=2).fit(data)((grid_points, grid_points))
    x, y = np.unique(x[:, 0]), np.unique(x[:, 1])
    x, y = np.meshgrid(x, y)
    z = z.reshape(grid_points, grid_points).T + 0.1
    ls = LightSource(350, 45)
    rgb = ls.shade(z, cmap=cm.PuBu, vert_exag=0.1, blend_mode="soft")
    surf = ax.plot_surface(
        x,
        y,
        z,
        rstride=1,
        cstride=1,
        facecolors=rgb,
        linewidth=0,
        antialiased=False,
        shade=True,
    )
    ax.view_init(30, 65)
    plt.yticks([])
    plt.xticks([])
    ax.set_zticks([])

@tommyod
Copy link
Owner

tommyod commented Oct 29, 2022

Thanks @trungnth !

@tommyod tommyod closed this as completed Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants