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)
# 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 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=(9, 4.0)) fig.subplots_adjust(left=0.1, right=0.95, bottom=0.14, top=0.9, hspace=0.01, wspace=0.01) # First plot: scatter the points
#------------------------------------------------------------ # 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,
#------------------------------------------------------------ # plot the results fig = plt.figure(figsize=(8, 8)) fig.subplots_adjust() 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 = ent2[:N] t = np.linspace(-10, 30, 1000) # Compute density with KDE 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', lw=1.5) hist(xN, bins='blocks', ax=ax, normed=True, zorder=1, histtype='stepfilled', lw=1.5, color='k', alpha=0.2, label="Bayesian Blocks") ax.plot(t, dens_nbrs, '-', lw=2, color='gray', zorder=2, label="Nearest Neighbors (k=%i)" % k)
#------------------------------------------------------------ # plot the results fig = plt.figure(figsize=(5, 5)) fig.subplots_adjust(bottom=0.08, top=0.95, right=0.95, hspace=0.1) 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 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,
def contourDens(x, y, z, out, nBins = 5): fig = pylab.figure() ax = fig.add_subplot(111, projection='3d') bins = num.linspace(out.min(), out.max(), nBins + 1) # plot outermost shell where bin criterion is satisfied idx = num.where((out > bins[1]) & (out < bins[2])) ax.scatter(x[idx], y[idx], z[idx]) #for bin in range(nBins): #idx = num.where(out > bins[bin]) #color = pylab.matplotlib.colors.ColorConverter().to_rgb(str(bins[bin] / bins.max())) #ax.scatter(x[idx], y[idx], z[idx]) #ax.plot_surface(x[idx], y[idx], z[idx], color=color) qMatrix = qatsResults_3d() densEstimator = KDE(h=5) """ Need qMatrix shape to be (Npoints, 3) or (Npoints, 4) Each row will be a coordinate specifying a point in 4-space We want the density to be largest near signal peaks (so points with high SNR) If indices are used to specify the first three coordinates, the main factor in deciding density SHOULD be the snr (since indices are all evenly spaced) """ print qMatrix.shape wIdx = num.arange(qMatrix.shape[0]) dIdx = num.arange(qMatrix.shape[1]) pBinIdx = num.arange(qMatrix.shape[2]) P, D, W = meshgrid_3d(pBinIdx, dIdx, wIdx) w = W.ravel() d = D.ravel()
# 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
if use_sklearn_KDE: kde1 = KernelDensity(5, kernel='gaussian') log_dens1 = kde1.fit(X).score_samples(Xgrid) dens1 = X.shape[0] * np.exp(log_dens1).reshape((Ny, Nx)) kde2 = KernelDensity(5, kernel='tophat') log_dens2 = kde2.fit(X).score_samples(Xgrid) dens2 = X.shape[0] * np.exp(log_dens2).reshape((Ny, Nx)) kde3 = KernelDensity(5, kernel='exponential') log_dens3 = kde3.fit(X).score_samples(Xgrid) dens3 = X.shape[0] * np.exp(log_dens3).reshape((Ny, Nx)) else: 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,