5. Implement the non-parametric Locally Weighted Regression algorithm in order to fit data points. Select appropriate data set for your experiment and draw graphs.
PROGRAM:
#install required packages
#pip install numpy matplotlib
import numpy as np
import matplotlib.pyplot as plt
# ==============================
# 1. Create Dataset
# ==============================
np.random.seed(42)
X = np.linspace(0, 10, 100)
y = np.sin(X) + np.random.normal(0, 0.2, 100)
# ==============================
# 2. Locally Weighted Regression
# ==============================
def kernel(x, x_i, tau):
return np.exp(-((x - x_i) ** 2) / (2 * tau ** 2))
def locally_weighted_regression(X, y, query_point, tau):
m = len(X)
X_bias = np.c_[np.ones(m), X]
query_bias = np.array([1, query_point])
weights = np.zeros((m, m))
for i in range(m):
weights[i, i] = kernel(query_point, X[i], tau)
theta = np.linalg.pinv(X_bias.T @ weights @ X_bias) @ X_bias.T @ weights @ y
prediction = query_bias @ theta
return prediction
# ==============================
# 3. Predict Values
# ==============================
tau = 0.5
X_test = np.linspace(0, 10, 200)
y_pred = []
for x in X_test:
y_pred.append(locally_weighted_regression(X, y, x, tau))
y_pred = np.array(y_pred)
# ==============================
# 4. Plot Graph
# ==============================
plt.figure(figsize=(9, 6))
plt.scatter(X, y, label="Training Data")
plt.plot(X_test, y_pred, label="LWR Curve", linewidth=2)
plt.title("Locally Weighted Regression")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.grid(True)
plt.show()OUTPUT:

