def gauss2D(sigma, kernel_size): if kernel_size % 2 == 0: raise ValueError( 'kernel_size must be odd, otherwise the filter will not have a center to convolve on' ) # solution Gx = gauss1D(sigma, kernel_size) Gy = gauss1D(sigma, kernel_size) G2 = Gx.reshape(kernel_size, 1) @ Gy.reshape(1, kernel_size) return G2
def gauss2D(sigma, kernel_size): ## solution # compute 1D filters G_x = gauss1D(sigma, kernel_size) G_y = G_x.copy() # compute product and re-normalize G = np.outer(G_y, G_x) G /= np.sum(G) return G
import numpy as np from parameters import parameters from knn import knn from kde import kde from gauss1D import gauss1D import matplotlib.pyplot as plt h, k = parameters() print('Question: Kernel/K-Nearest Neighborhood Density Estimators') # Produce the random samples samples = np.random.normal(0, 1, 100) # Compute the original normal distribution realDensity = gauss1D(0, 1, 100, 5) # Estimate the probability density using the KDE estDensity = kde(samples, h) # plot results plt.subplot(2, 1, 1) plt.plot(estDensity[:, 0], estDensity[:, 1], 'r', linewidth=1.5, label='KDE Estimated Distribution') plt.plot(realDensity[:, 0], realDensity[:, 1], 'b', linewidth=1.5,