<jupyter_start><jupyter_text>One-Dimensional Kernel Smoothers<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import math
%matplotlib inline
x = np.random.uniform(0, 1, 200)
x.sort()
e = np.random.normal(0, (1/3)**0.5, 200)
y = list(map(lambda x, e:math.sin(4*x) + e, x, e))
y_truth = list(map(lambda x:math.sin(4*x), x))

plt.scatter(x, y, facecolors='none', edgecolors='grey', alpha=0.8)
plt.plot(x, y_truth, 'b')
plt.xlabel('$x_0$')
def knn(trainx, trainy, testx, k=30):
    def _knn(x0):
        diff = abs(trainx - x0)
        index = np.argsort(diff)
        nny = [trainy[i] for i in index[:k]]
        return np.mean(nny)
    return [_knn(x0) for x0 in testx]
x_pre = np.linspace(0, 1, 100)
y_pre  = knn(x,y, x_pre, 30)
plt.scatter(x, y, facecolors='none', edgecolors='grey', alpha=0.8)
plt.plot(x, y_truth, 'b')
plt.xlabel('$x_0$')
plt.plot(x_pre, y_pre, 'g')
def eqk(x1, x2, l=0.2):
    return D(abs(x1 - x2)/l)
def D(t):
    return 3/4*(1-t**2) if abs(t)<=1 else 0
def knn_smooth(trainx, trainy, textx, k=30):
    def _knn(x0):
        diff = abs(trainx - x0)
        index = np.argsort(diff)
        m = [eqk(x0, trainx[i])*trainy[i] for i in index[:k]]
        n = [eqk(x0, trainx[i]) for i in index[:k]]
        return np.sum(m)/np.sum(n)
    return [_knn(x0) for x0 in textx]
y_pre_s  = knn_smooth(x,y, x_pre, 20)
plt.scatter(x, y, facecolors='none', edgecolors='grey', alpha=0.8)
plt.plot(x, y_truth, 'b')
plt.xlabel('$x_0$')
plt.plot(x_pre, y_pre_s, 'g')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19380355965721818, 'fraction_numerical': 0.03889255108767304, 'mean_word_length': 3.8967741935483873, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '604932', 'license': 'MIT', 'n_tokens_mistral': 742, 'n_tokens_neox': 702, 'n_words': 154, 'path': '6/Kernel smoothing methods.ipynb', 'repo_name': 'thylakoids/the-elements-of-statistical-learning', 'size': '1517'}