0% found this document useful (0 votes)
3 views2 pages

Program 6

The document provides a Python implementation of the non-parametric Locally Weighted Regression algorithm to fit data points from a dataset. It includes functions for calculating weights, performing local weight regression, and plotting the results using Matplotlib. The example uses a dataset of tips based on total bill amounts and visualizes the fitted regression curve.

Uploaded by

abhi5gnan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Program 6

The document provides a Python implementation of the non-parametric Locally Weighted Regression algorithm to fit data points from a dataset. It includes functions for calculating weights, performing local weight regression, and plotting the results using Matplotlib. The example uses a dataset of tips based on total bill amounts and visualizes the fitted regression curve.

Uploaded by

abhi5gnan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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)

You might also like