-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregression.py
166 lines (114 loc) · 4.15 KB
/
regression.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""regression.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1I0ZeyWwtrvaZhkmCBmxvVeB2AOxnSd_C
<img src="https://rhyme.com/assets/img/logo-dark.png" align="center">
<h2 align="center"> Univariate Linear Regression </h2>
### Task 2: Load the Data and Libraries
---
"""
# Commented out IPython magic to ensure Python compatibility.
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# %matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
plt.rcParams['figure.figsize'] = (12, 8)
data = pd.read_csv('bike_sharing_data.txt')
data.head()
#data.tail() #To print last five values of the dataset
#population(in 10k), profit(in $10k)
data.info()
"""### Task 3: Visualize the Data
---
"""
ax = sns.scatterplot(x="Population",y="Profit",data=data)
ax.set_title("Profit in $10000s vs City Population in 10000s")
"""### Task 4: Compute the Cost $J(\theta)$
---
The objective of linear regression is to minimize the cost function
$$J(\theta) = \frac{1}{2m} \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)} )^2$$
where $h_{\theta}(x)$ is the hypothesis and given by the linear model
$$h_{\theta}(x) = \theta^Tx = \theta_0 + \theta_1x_1$$
"""
def cost_function(X, y, theta):
m = len(y)
y_pred = X.dot(theta)
error = (y_pred - y)**2
return 1/(2*m)*np.sum(error)
m = data.Population.values.size
X = np.append(np.ones((m, 1)), data.Population.values.reshape(m, 1),axis=1)
y= data.Profit.values.reshape(m, 1)
theta = np.zeros((2, 1))
cost_function(X, y, theta)
"""### Task 5: Gradient Descent
---
Minimize the cost function $J(\theta)$ by updating the below equation and repeat unitil convergence
$\theta_j := \theta_j - \alpha \frac{1}{m} \sum_{i=1}^m (h_{\theta}(x^{(i)}) - y^{(i)})x_j^{(i)}$ (simultaneously update $\theta_j$ for all $j$).
"""
def gradient_descent(X, y, theta, alpha, iterations):
m = len(y)
costs = []
for i in range(iterations):
y_pred = X.dot(theta)
error = np.dot(X.transpose(), (y_pred - y))
theta -= alpha * 1/m * error
costs.append(cost_function(X, y, theta))
return theta, costs
theta, costs = gradient_descent(X, y, theta, alpha=0.01, iterations=2000)
print("h(X) = {} + {}x1".format(str(round(theta[0, 0], 2)),
str(round(theta[1, 0], 2))))
"""### Task 6: Visualising the Cost Function $J(\theta)$
---
"""
from mpl_toolkits.mplot3d import Axes3D
theta_0 = np.linspace(-10, 10, 100)
theta_1 = np.linspace(-1, 4, 100)
cost_values = np.zeros((len(theta_0), len(theta_1)))
for i in range(len(theta_0)):
for j in range(len(theta_1)):
t = np.array([theta_0[i], theta_1[j]])
cost_values[i, j] = cost_function(X, y, t)
fig = plt.figure(figsize = (12, 8))
ax = fig.gca(projection = '3d')
surf = ax.plot_surface(theta_0, theta_1, cost_values, cmap = 'viridis')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.xlabel("$\Theta_0$")
plt.ylabel("$\Theta_1$")
ax.set_zlabel("$J(\Theta)$")
ax.view_init(30,330)
plt.show()
"""### Task 7: Plotting the Convergence
---
Plot $J(\theta)$ against the number of iterations of gradient descent:
"""
plt.plot(costs)
plt.xlabel("Iterations")
plt.ylabel("$J(\Theta)$")
plt.title("Values of the Cost Function over iterations of Gradient Descent")
"""### Task 8: Training Data with Linear Regression Fit
---
"""
theta.shape
theta
theta = np.squeeze(theta)
sns.scatterplot(x="Population",y="Profit",data=data)
x_value = [x for x in range(5, 25)]
y_value = [x * theta[1] + theta[0] for x in x_value]
sns.lineplot(x_value, y_value)
plt.xlabel("Population in 10000s")
plt.ylabel("Profit in $10000s")
plt.title("Linear Regression Fit")
"""### Task 9: Inference using the optimized $\theta$ values
---
$h_\theta(x) = \theta^Tx$
"""
def predict(x, theta):
y_pred = np.dot(theta.transpose(), x)
return y_pred
y_pred_2 = predict(np.array([1,8.3]), theta) * 10000
print("For a population of 83,000 people, the model predicts a profit of $"+str(round(y_pred_2, 0)))
y_pred_1 = predict(np.array([1,4]), theta) * 10000
print("For a population of 40,000 people, the model predicts a profit of $"+str(round(y_pred_1, 0)))