-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalweightedregression.py
53 lines (46 loc) · 1.17 KB
/
localweightedregression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from numpy import *
import operator
from os import listdir
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy.linalg
from scipy.stats.stats import pearsonr
def kernel(point,xmat,k):
m,n= shape(xmat)
weights=mat(eye((m)))
for j in range(m):
diff = point - X[j]
weights[j,j]= exp(diff*diff.T/(-2*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=shape(xmat)
ypred=zeros(m)
for i in range(m):
ypred[i]=xmat[i]*localWeight(xmat[i],xmat,ymat,k)
return ypred
#load data points
data=pd.read_csv('data10.csv')
bill=array(data.totbill)
tip=array(data.tip)
#Preparing and add 1 in bill
mbill=mat(bill)
mtip=mat(tip)
m=shape(mbill)[1]
one=mat(ones(m))
X=hstack((one.T,mbill.T))
#set k here
ypred=localWeightRegression(X,mtip,0.5)
SortIndex=X[:,1].argsort(0)
xsort=X[SortIndex][:,0]
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.scatter(bill,tip,color='green')
ax.plot(xsort[:,1],ypred[SortIndex],color='red',linewidth=5)
plt.xlabel('Total Bill')
plt.ylabel('Tip')
plt.show()