[[Category:Articles containing proofs]]
[[Category:Articles with example MATLAB/Octave code]]
]
import numpy as np
import matplotlib.pyplot as plt
def gaussian_weight(x, x_i, tau):
return np.exp(-(x - x_i)**2 / (2 * tau**2)) # Use regular minus and correct exponentiation
def locally_weighted_regression(X, y, tau):
X = np.array(X)
y = np.array(y)
n = len(X)
y_pred = []
for query_point in X:
weights = np.array([gaussian_weight(query_point, x_i, tau) for x_i in X])
W = np.diag(weights)
X_mat = np.c_[np.ones(n), X] # Add intercept (column of ones)
theta = np.linalg.pinv(X_mat.T @ W @ X_mat) @ X_mat.T @ W @ y # Solving for theta
y_pred.append(theta[0] + theta[1] * query_point)
return np.array(y_pred)
# Generate data
np.random.seed(42)
X = np.linspace(-3, 3, 50)
y = np.sin(X) + np.random.normal(0, 0.2, X.shape)
tau = 0.5
# Perform locally weighted regression
y_pred = locally_weighted_regression(X, y, tau)
# Plot results
plt.figure(figsize=(10, 6))
plt.scatter(X, y, color='red', label='Data Points')
plt.plot(X, y_pred, color='blue', label='LWR Fitted Curve')
plt.title('Locally Weighted Regression')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()
|