def test_1D_density(): np.random.seed(0) dist = norm(0, 1) X = dist.rvs((5000, 1)) X2 = np.linspace(-5, 5, 10).reshape((10, 1)) true_dens = dist.pdf(X2[:, 0]) * X.shape[0] classifiers = [KDE('gaussian', h=0.1), KDE('tophat', h=0.2), KDE('exponential', h=0.1), KDE('quadratic', h=0.2), KNeighborsDensity(method='simple', n_neighbors=250), KNeighborsDensity(method='bayesian', n_neighbors=250)] for clf in classifiers: yield (check_1D_density, clf, X, X2, true_dens, 100)
#------------------------------------------------------------ # Create the grid on which to evaluate the results Nx = 50 Ny = 125 xmin, xmax = (-375, -175) ymin, ymax = (-300, 200) #------------------------------------------------------------ # Evaluate for several models Xgrid = np.vstack( map(np.ravel, np.meshgrid(np.linspace(xmin, xmax, Nx), np.linspace(ymin, ymax, Ny)))).T kde = KDE(metric='gaussian', h=5) dens_KDE = kde.fit(X).eval(Xgrid).reshape((Ny, Nx)) knn5 = KNeighborsDensity('bayesian', 5) dens_k5 = knn5.fit(X).eval(Xgrid).reshape((Ny, Nx)) knn40 = KNeighborsDensity('bayesian', 40) dens_k40 = knn40.fit(X).eval(Xgrid).reshape((Ny, Nx)) #------------------------------------------------------------ # Plot the results fig = plt.figure(figsize=(5, 2.2)) fig.subplots_adjust(left=0.12, right=0.95, bottom=0.2, top=0.9,
N_values = (500, 5000) subplots = (211, 212) k_values = (10, 100) for N, k, subplot in zip(N_values, k_values, subplots): ax = fig.add_subplot(subplot) xN = x[:N] t = np.linspace(-10, 30, 1000) # Compute density with KDE if use_sklearn_KDE: kde = KernelDensity(0.1, kernel='gaussian') kde.fit(xN[:, None]) dens_kde = np.exp(kde.score_samples(t[:, None])) else: kde = KDE('gaussian', h=0.1).fit(xN[:, None]) dens_kde = kde.eval(t[:, None]) / N # Compute density with Bayesian nearest neighbors nbrs = KNeighborsDensity('bayesian', n_neighbors=k).fit(xN[:, None]) dens_nbrs = nbrs.eval(t[:, None]) / N # plot the results ax.plot(t, true_pdf(t), ':', color='black', zorder=3, label="Generating Distribution") ax.plot(xN, -0.005 * np.ones(len(xN)), '|k') hist(xN,
# Fetch the great wall data X = fetch_great_wall() #------------------------------------------------------------ # Create the grid on which to evaluate the results Nx = 50 Ny = 125 xmin, xmax = (-375, -175) ymin, ymax = (-300, 200) #------------------------------------------------------------ # Evaluate for several models Xgrid = np.vstack(map(np.ravel, np.meshgrid(np.linspace(xmin, xmax, Nx), np.linspace(ymin, ymax, Ny)))).T kde1 = KDE(metric='gaussian', h=5) dens1 = kde1.fit(X).eval(Xgrid).reshape((Ny, Nx)) kde2 = KDE(metric='tophat', h=5) dens2 = kde2.fit(X).eval(Xgrid).reshape((Ny, Nx)) kde3 = KDE(metric='exponential', h=5) dens3 = kde3.fit(X).eval(Xgrid).reshape((Ny, Nx)) #------------------------------------------------------------ # Plot the results fig = plt.figure(figsize=(5, 2.2)) fig.subplots_adjust(left=0.12, right=0.95, bottom=0.2, top=0.9, hspace=0.01, wspace=0.01) # First plot: scatter the points