-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex1.knit.html
366 lines (312 loc) · 11.1 KB
/
ex1.knit.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Jai Broome" />
<title>Exercise 1</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet"
href="site_libs/highlight/default.css"
type="text/css" />
<script src="site_libs/highlight/highlight.js"></script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">coursera-ml-R</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="ex1">Exercise 1</a>
</li>
<li>
<a href="ex2">Exercise 2</a>
</li>
<li>
<a href="ex3">Exercise 3</a>
</li>
<li>
<a href="ex4">Exercise 4</a>
</li>
<li>
<a href="ex7">Exercise 7</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Exercise 1</h1>
<h4 class="author"><em>Jai Broome</em></h4>
<h4 class="date"><em>21 November, 2016</em></h4>
</div>
<div id="TOC">
<ul>
<li><a href="#simple-octavematlab-function"><span class="toc-section-number">1</span> Simple Octave/MATLAB function</a><ul>
<li><a href="#submitting-solutions"><span class="toc-section-number">1.1</span> Submitting Solutions</a></li>
</ul></li>
<li><a href="#linear-regression-with-one-variable"><span class="toc-section-number">2</span> Linear regression with one variable</a><ul>
<li><a href="#plotting-the-data"><span class="toc-section-number">2.1</span> Plotting the Data</a></li>
<li><a href="#gradient-descent"><span class="toc-section-number">2.2</span> Gradient Descent</a></li>
<li><a href="#debugging"><span class="toc-section-number">2.3</span> Debugging</a></li>
<li><a href="#visualizing-jtheta"><span class="toc-section-number">2.4</span> Visualizing <span class="math inline">\(J(\theta)\)</span></a></li>
</ul></li>
<li><a href="#optional-exercises"><span class="toc-section-number">3</span> Optional exercises</a></li>
</ul>
</div>
<pre class="r"><code>require(ggplot2)
require(dplyr)
require(knitr)</code></pre>
<div id="simple-octavematlab-function" class="section level1">
<h1><span class="header-section-number">1</span> Simple Octave/MATLAB function</h1>
<p>I’ll skip over this since I figure anyone wanting to follow along is already familiar with R</p>
<div id="submitting-solutions" class="section level2">
<h2><span class="header-section-number">1.1</span> Submitting Solutions</h2>
<p>You won’t be able to submit this for credit, but hopefully you still find this useful</p>
</div>
</div>
<div id="linear-regression-with-one-variable" class="section level1">
<h1><span class="header-section-number">2</span> Linear regression with one variable</h1>
<p>Here I read in the data, split it into <code>X</code> and <code>y</code>, and generate starting values for <code>theta</code></p>
<pre class="r"><code>q1data <- read.table("data/ex1data1.txt",
sep = ",",
col.names = c("population", "profit"))
q1data$ones <- 1
q1data <- q1data %>% select(ones, population, profit)
X <- select(q1data, -profit)
y <- q1data$profit
theta <- rep(0, times = 2)</code></pre>
<div id="plotting-the-data" class="section level2">
<h2><span class="header-section-number">2.1</span> Plotting the Data</h2>
<pre class="r"><code>g1 <- ggplot(q1data, aes(x = population, y = profit)) +
geom_point(shape = 4, color = "red", size = 3) +
xlab("Population of City in 10,000s") +
ylab("Profit in $10,000s")
g1</code></pre>
<p><img src="ex1_files/figure-html/viz-exploration-1.png" width="672" /></p>
</div>
<div id="gradient-descent" class="section level2">
<h2><span class="header-section-number">2.2</span> Gradient Descent</h2>
<pre class="r"><code>iterations <- 1500
alpha <- 0.01</code></pre>
<p>Functions are defined in a separate script so they can be accessed for other assignments. See <code>?knitr::read_chunk</code></p>
<pre class="r"><code>read_chunk("ex1/ex1_chunks.R")</code></pre>
<pre class="r"><code>computeCost <- function(X, y, theta, lambda = 0){
if(is.null(dim(X))){
X <- t(data.frame(X))
}
m <- nrow(X)
pred <- theta %*% t(X)
sqError <- (pred - y) ^ 2
cost <- (1 / (2 * m)) * sum(sqError)
reg <- (lambda / (2 * m)) * sum(theta[-1]^2)
J <- cost + reg
gradient <- (1 / m) * sum((pred - y) * X[, 1]) # first element unregularized
for(j in 2:length(theta)){
gradient <- c(gradient, ((1 / m) * sum((pred - y) * X[, j]) + lambda * theta[j] / m))
}
return(list(J=J, gradient=gradient))
}</code></pre>
<pre class="r"><code>gradStep <- function(thetaj, alpha, gradj){
thetaj - alpha * gradj
}</code></pre>
<p>Note that in the loop, <code>thetas</code> get stored in a temporary variable for simultaneous updating.</p>
<pre class="r"><code>gradientDescent <- function(X, y, theta, alpha, iterations, lambda = 0){
if(length(theta) != ncol(X)){stop("theta and X are nonconformable")}
J_history <- data.frame()
for (iteration in 1:iterations){
a <- computeCost(X = X, y = y, theta = theta, lambda = lambda)
J <- a$J
gradient <- a$gradient
J_history <- rbind(J_history, c(J, iteration - 1, theta))
thetaTemp <- vector()
for(j in 1:length(theta)){
thetaTemp <- c(thetaTemp, gradStep(theta[j], alpha, gradient[j]))
}
theta <- thetaTemp
}
# for final iteration
J <- computeCost(X = X, y = y, theta = theta, lambda = lambda)$J
J_history <- rbind(J_history, c(J, iteration, theta))
thetanames <- rep("theta", times = length(theta) - 1)
for(i in length(thetanames)){thetanames[i] <- paste("theta", i, sep = "")}
colnames(J_history) <- c("loss", "iterations", "theta0", thetanames)
return(J_history)
}</code></pre>
<pre class="r"><code>computeCost(X, y, theta)</code></pre>
<pre><code>## $J
## [1] 32.07273
##
## $gradient
## [1] -5.839135 -65.328850</code></pre>
<p>Should be <code>32.07</code></p>
</div>
<div id="debugging" class="section level2">
<h2><span class="header-section-number">2.3</span> Debugging</h2>
<p>Pay particular attention to indexing in these exercizes, because unlike Matlab, R <em>does</em> start indexing at 1</p>
<pre class="r"><code>jhist <- gradientDescent(X, y, theta, alpha, iterations)
jhist <- jhist[2:(nrow(jhist) - 1),] #makes graph more interpretible </code></pre>
<pre class="r"><code>ggplot(jhist, aes(x = iterations, y = loss)) + geom_line()</code></pre>
<p><img src="ex1_files/figure-html/viz-results-1.png" width="672" /></p>
<pre class="r"><code>g1 + geom_abline(slope = tail(jhist$theta1, n = 1), intercept = tail(jhist$theta0, n = 1))</code></pre>
<p><img src="ex1_files/figure-html/viz-results-2.png" width="672" /></p>
</div>
<div id="visualizing-jtheta" class="section level2">
<h2><span class="header-section-number">2.4</span> Visualizing <span class="math inline">\(J(\theta)\)</span></h2>
<p>I skipped over this section since the code is already written in Matlab by the course instructors. See the assignment page for the images that show how different values of the thetas yield different costs</p>
</div>
</div>
<div id="optional-exercises" class="section level1">
<h1><span class="header-section-number">3</span> Optional exercises</h1>
<p>These are still incomplete. I’m planning on returning to these after making it through the main course content</p>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>