<jupyter_start><jupyter_text>Funzione kernel<jupyter_code>def kernel(x,xi,tau):
    return np.exp(np.sum((xi - x) ** 2, axis=1) / (-2 * tau * tau))<jupyter_output><empty_output><jupyter_text>Funzione che restituisce la matrice diagonale $\Psi$ dei valori kernel tra $x$ e tutti gli elementi nel training set<jupyter_code>def diag_kernel(x, X, tau):
    return np.diag(kernel(X,x,tau))<jupyter_output><empty_output><jupyter_text>Genera dataset secondo la funzione $\log(|x^2-1|+.5)$ con rumore additivo gaussiano<jupyter_code>n = 1000
X = np.linspace(-3, 3, num=n)
t = np.log(np.abs(X ** 2 - 1) + .5)

X += np.random.normal(scale=.1, size=n)
X = X.reshape(-1,1)<jupyter_output><empty_output><jupyter_text>Fissa valore della varianza del kernel<jupyter_code>tau = .05<jupyter_output><empty_output><jupyter_text>Regressione in un punto $x$<jupyter_code>x = -0.03

# calcola matrice kernel
psi = diag_kernel(np.array([x]).reshape(-1,1),X,tau)
domain = np.linspace(-3, 3, num=300)<jupyter_output><empty_output><jupyter_text>Visualizza valori del kernel<jupyter_code>f = plt.figure(figsize=(16,4))
plt.title('$x={0:5.3f}$'.format(x))
plt.plot(domain, kernel(x,domain.reshape(-1,1),tau), c=colors[0])
plt.show()<jupyter_output><empty_output><jupyter_text>Calcola i coefficienti della regressione locale a $x$<jupyter_code>y = local_regression(x, X, t, tau)
w = local_regression_coeffs(x, X, t, tau)<jupyter_output><empty_output><jupyter_text>Plot retta di regressione a $x$<jupyter_code>f = plt.figure(figsize=(16,8))
plt.title('$x={0:5.3f}$'.format(x))
plt.scatter(X, t, c= colors[0], alpha=.5)
plt.plot([x], [y], marker='o', color=colors[7], alpha=.5)
plt.plot(domain, np.c_[np.ones(len(domain)), domain] @ w, color=colors[1])
plt.show()<jupyter_output><empty_output><jupyter_text>Calcola predizioni su tutti i punti considerati<jupyter_code>prediction = [local_regression(x, X, t, tau) for x in domain]<jupyter_output><empty_output><jupyter_text>Visualizza valori predetti<jupyter_code>f = plt.figure(figsize=(16,8))
plt.title('tau=%g' % tau)
plt.scatter(X, t, c= colors[0], alpha=.5)
plt.plot(domain, prediction, lw=2, c=colors[1])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17657906869525128, 'fraction_numerical': 0.02627939142461964, 'mean_word_length': 7.821138211382114, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '40660', 'license': 'MIT', 'n_tokens_mistral': 948, 'n_tokens_neox': 910, 'n_words': 155, 'path': 'codici/local_regr.ipynb', 'repo_name': 'tvml/ml2122', 'size': '2169'}