Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

5/23/22, 8:12 PM Exp-12 - Jupyter Notebook

In [53]:

import numpy as np

In [54]:

n = 10

X = np.linspace(-3, 3, num=n)
y = np.abs(X ** 3 - 1)
X += np.random.normal(scale=.1, size=n)

In [55]:

def get_weights(x, X, tau):


return np.exp(np.sum((X - x) ** 2, axis=1) / (-2 * (tau **2) ))

In [56]:

def local_weighted_regression(x, X, y, tau):


x = np.r_[1, x]
X = np.c_[np.ones(len(X)), X]

xw = X.T * get_weights(x, X, tau)

theta = np.linalg.pinv(xw @ X) @ xw @ y

return theta @ x

In [57]:

tau = 0.1
domain = np.linspace(-3, 3, num=300)
prediction = [local_weighted_regression(x0, X, y, tau) for x0 in domain]

localhost:8888/notebooks/Exp-12.ipynb 1/2
5/23/22, 8:12 PM Exp-12 - Jupyter Notebook

In [58]:

plt.scatter(X, y, alpha=.3)
plt.plot(domain, prediction, color='red')

Out[58]:

[<matplotlib.lines.Line2D at 0x149423c0070>]

In [ ]:

localhost:8888/notebooks/Exp-12.ipynb 2/2

You might also like