Program 6: Implement the non-parametric Locally Weighted Regression
algorithm in order to fit data points. Select appropriate data set for your
experiment and draw graphs.
import [Link] as plt
import pandas as pd
import numpy as np
def kernel(point,xmat, k):
m,n = [Link](xmat)
weights = [Link]([Link]((m))) # eye - identity matrix
for j in range(m):
diff = point - X[j]
weights[j,j] = [Link](diff*diff.T/(-2.0*k**2))
return weights
def localWeight(point,xmat,ymat,k):
wei = kernel(point,xmat,k)
W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))
return W
def localWeightRegression(xmat,ymat,k):
m,n = [Link](xmat)
ypred = [Link](m)
for i in range(m):
ypred[i] = xmat[i]*localWeight(xmat[i],xmat,ymat,k)
return ypred
def graphPlot(X,ypred):
sortindex = X[:,1].argsort(0) #argsort - index of the smallest
xsort = X[sortindex][:,0]
fig = [Link]()
ax = fig.add_subplot(1,1,1)
[Link](bill,tip, color='green')
[Link](xsort[:,1],ypred[sortindex], color = 'red', linewidth=5)
[Link]('Total bill')
[Link]('Tip')
[Link]();
# load data points
data = pd.read_csv('[Link]')
bill = [Link](data.total_bill) # We use only Bill amount and Tips data tip =
[Link]([Link])
tip = [Link]([Link])
mbill = [Link](bill) # .mat will convert nd array is converted in 2D array mtip =
[Link](tip)
mtip = [Link](tip)
m= [Link](mbill)[1]
one = [Link]([Link](m))
X = [Link]((one.T,mbill.T)) # 244 rows, 2 cols
#print(X)
ypred = localWeightRegression(X,mtip,2) # increase k to get smooth curves
graphPlot(X,ypred)