Last active
September 5, 2018 04:56
-
-
Save JVero/6dec9a74a8a44f48f5007ffca4d78a41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib import pyplot as plt | |
def generate_generic_kernel(bandwidth, kernel_type): | |
def u(x): return x/bandwidth - 1 # u normalizes the range of the kernel supports between [-1, 1] | |
kernels = { | |
'epanechnikov': lambda x: 3/4 * (1 - u(x)**2), | |
'triangular': lambda x: 1 - np.abs(u(x)), | |
'knn': lambda x: 1/2 | |
} | |
if kernel_type not in kernels: | |
raise ValueError(f"{kernel_type} not in {kernels.keys()}") | |
return [kernels[kernel_type](x) for x in range(2 * bandwidth + 1)] | |
def kernel_smoothing(data: np.array, bandwidth: int = 12, kernel: str ='epanechnikov') -> np.array: | |
kernels = ['epanechnikov', 'triangular', 'knn'] | |
if kernel not in kernels: | |
raise ValueError(f"{kernel} not in {list(kernels)}") | |
generated_kernel = generate_generic_kernel(bandwidth, kernel) | |
return np.convolve(data, generated_kernel, mode='same') / np.sum(generated_kernel) # nadaraya-watson sum | |
if __name__ == "__main__": | |
x = np.linspace(1,100, 1000) | |
y = np.sin(x) | |
def legend(): plt.legend(('True Data', 'Filtered Data', 'Residuals')) | |
y_noisy = y + np.random.randn(1000)/3 | |
values = {} | |
values['KNN'] = kernel_smoothing(y_noisy, kernel='knn') | |
values['Parabolic'] = kernel_smoothing(y_noisy) | |
values['Triangular'] = kernel_smoothing(y_noisy, kernel='triangular') | |
for i in values: | |
plt.figure() | |
plt.plot(x, y) | |
plt.plot(x, values[i]) | |
plt.plot(x, y-values[i]) | |
plt.title(f'{i} Smoothing') | |
legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment