-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocallyWeightedRegression.py
48 lines (35 loc) · 1.19 KB
/
LocallyWeightedRegression.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def kernel(data, point, xmat, k):
m, n = np.shape(xmat)
ws = np.mat(np.eye((m)))
for j in range(m):
diff = point - data[j]
ws[j, j] = np.exp(diff * diff.T / (-2.0 * k**2))
return ws
def local_weight(data, point, xmat, ymat, k):
wei = kernel(data, point, xmat, k)
return (data.T * (wei * data)).I * (data.T * (wei * ymat.T))
def local_weight_regression(xmat, ymat, k):
m, _ = np.shape(xmat)
ypred = np.zeros(m)
for i in range(m):
ypred[i] = xmat[i] * local_weight(xmat, xmat[i], xmat, ymat, k)
return ypred
df = pd.read_csv('tips.csv')
features = np.array(df.total_bill)
labels = np.array(df.tip)
m = features.shape[0]
mtip = np.mat(labels)
data = np.hstack((np.ones((m, 1)), np.mat(features).T))
ypred = local_weight_regression(data, mtip, 0.5)
indices = data[:, 1].argsort(0)
xsort = data[indices][:, 0]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(features, labels, color='blue')
ax.plot(xsort[:, 1], ypred[indices], color = 'red', linewidth=3)
plt.xlabel('Total bill')
plt.ylabel('Tip')
plt.show()