Localweighted - Jupyter Notebook

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

5/8/24, 10:16 AM localweighted - Jupyter Notebook

In [1]: import numpy as np



import pandas as pd

import matplotlib.pyplot as plt

In [2]: data = pd.read_csv('tips.csv')



print("\n Input Data \n\n\n", data.head())

Input Data

total_bill tip sex smoker day time size


0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

In [3]: #Take the first and second column of the data


colA = np.array(data.total_bill)

colB= np.array(data.tip)

#Convert these columns to numpy matrices
mcolA = np.mat(colA) #total_bill

mcolB = np.mat(colB) #tip

In [4]: print(np.shape(colA))
print(np.shape(mcolA))
print(np.shape(colB))
print(np.shape(mcolB))

(244,)
(1, 244)
(244,)
(1, 244)

In [5]: m= np. shape (mcolB)[1] #mhas stored number of columns


one= np.ones((1, m), dtype = int) #Prepared a ones matrix

In [6]: # horizontal stacking to prepare input matrix



X = np.hstack((one.T, mcolA.T)) # X is Input matrix: Transpose of one matri

print(X.shape)

#print(X)

(244, 2)

localhost:8888/notebooks/localweighted.ipynb 1/4
5/8/24, 10:16 AM localweighted - Jupyter Notebook

In [7]: def kernel(point, xmat, k):


m,n = np.shape(xmat)
weights = np.eye(m) #Identity matrix
for j in range(m):
diff = point - X[j] #numerator
weights[j, j] = np.exp(diff * diff.T/(-2.0 * k**2))
return weights

In [8]: def localWeight (point, xmat, ymat, k):


wt = kernel (point, xmat, k)
W = (X.T * (wt*X)).I * (X.T * wt * ymat.T)
return W

In [9]: def localweightRegression(xmat, ymat, k):


m,n = np.shape(xmat) # Capture the shape of input matrix
ypred = np.zeros(m) # initializeoutput matrix from ZEROS

for i in range(m):
ypred[i] = xmat[i] * localWeight(xmat[i], xmat, ymat, k)
return ypred

In [10]: ypred = localweightRegression(X,mcolB, 0.5)

localhost:8888/notebooks/localweighted.ipynb 2/4
5/8/24, 10:16 AM localweighted - Jupyter Notebook

In [11]: print(ypred)

[ 2.89600072 1.79792676 3.4522923 3.52469879 3.55735533 4.30366513


1.74542033 3.3728316 2.44785581 2.48880782 1.8083104 4.79767983
2.4062242 2.96892092 2.48153633 3.60296925 1.79915825 2.69663122
2.89434019 3.31271154 2.99001488 3.16455055 2.45092913 5.81565108
3.01841158 2.97028536 2.25892138 2.0276448 3.60793899 2.97252695
1.95484881 2.99194322 2.44464168 3.32920123 2.96499951 3.21755765
2.7072132 2.89066608 2.88520305 4.24629839 2.56138533 2.92431206
2.4541946 1.95650777 3.76134349 3.00455741 3.53901817 4.09179973
3.66290968 3.00808646 2.0045738 1.80493014 4.74741353 1.89792722
4.27129464 2.93423809 3.43543432 3.4808215 1.94126954 7.23057352
3.16455055 2.41735695 1.88491665 3.00455741 2.93692617 3.09292596
2.77475539 1. 3.14256163 2.45273299 2.00874844 2.90170782
3.36328249 4.29987815 2.49550941 1.79043042 2.99001488 3.59729086
3.57479198 2.91358841 2.92392492 2.84629776 1.85736502 3.64623342
2.53190263 4.74356864 2.12713771 3.00620863 3.72100147 3.50603051
3.89403235 3.48271177 1.00112776 2.71243715 3.56803152 4.72571737
3.64447065 2.00820525 3.4522923 1.99806175 1.96577236 2.40748575
2.73357888 3.49064434 3.41892711 2.40849865 3.24526888 4.2646515
3.01146825 2.51405069 2.46876909 2.66849559 3.43658805 3.28533993
4.15216662 2.91465427 4.00576716 1.80257051 1.99658663 3.20974122
2.01171454 2.27845294 2.5103734 2.51807015 1.99932995 4.09338498
1.67174976 2.51473657 1.97178927 3.61791048 2.87096561 3.1570832
1.92407281 1.99643407 3.00910865 1.6698732 1.79915825 2.49729675
2.54147716 2.17623457 2.92511195 5.25976981 4.85670868 3.48788364
2.76596129 1.6615036 2.89846777 2.01463497 1.94169058 2.19910914
2.48355546 2.16463303 2.91203372 3.50429058 3.00467675 4.06514336
4.81733982 4.08416499 2.26675738 2.79134571 3.59411672 2.02187921
2.65306522 2.41735695 2.92857426 3.46560955 3.35741098 4.29599956
1.79551232 1.79993042 10.00000018 2.46330009 2.66849559 4.28421141
2.87744196 3.10778925 2.98471053 2.51633495 1.95831242 4.86189257
4.8411461 3.75181006 3.49981959 3.78290831 4.09278339 3.32920123
3.4114179 3.79013071 3.0155603 3.77491673 2.43081117 3.01565605
3.66451838 2.4064311 2.82356452 2.13199516 1.79792676 4.99343222
2.11637142 2.31297787 2.88065888 2.0384983 2.11637142 2.75218276
3.26218779 2.78322035 3.36532098 4.25846558 3.22538732 2.04326216
3.88698994 3.96972235 8.6847072 2.21946704 3.74312332 2.08272891
3.74790142 2.00369567 1.94456856 3.81795418 2.00085407 2.27845294
1.68568026 2.53190263 2.27845294 2.68589476 1.8514574 3.22847809
2.22342127 3.56617451 3.24368916 2.43081117 2.00568726 1.82360332
2.40872825 1.85736502 2.01207171 3.26367062 4.73341892 3.93042418
3.58402268 3.52143297 2.97207458 2.86850167]

localhost:8888/notebooks/localweighted.ipynb 3/4
5/8/24, 10:16 AM localweighted - Jupyter Notebook

In [12]: import matplotlib.pyplot as plt



xsort = X.copy()
xsort.sort(axis=0)

plt.scatter(colA, colB, color='blue')
plt.plot(xsort[:, 1], ypred[X[:, 1].argsort(0)], color='yellow', linewidth=
plt.xlabel('Total Bill')
plt.ylabel('Tip')
plt.show()

In [ ]: ​

In [ ]: ​

In [ ]: ​

In [ ]: ​

localhost:8888/notebooks/localweighted.ipynb 4/4

You might also like