-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.html
402 lines (343 loc) · 14 KB
/
kmeans.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<!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" />
<title>K-means clustering</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/yeti.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>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/font-awesome-4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; background-color: #f8f8f8; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
pre, code { background-color: #f8f8f8; }
code > span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code > span.dt { color: #204a87; } /* DataType */
code > span.dv { color: #0000cf; } /* DecVal */
code > span.bn { color: #0000cf; } /* BaseN */
code > span.fl { color: #0000cf; } /* Float */
code > span.ch { color: #4e9a06; } /* Char */
code > span.st { color: #4e9a06; } /* String */
code > span.co { color: #8f5902; font-style: italic; } /* Comment */
code > span.ot { color: #8f5902; } /* Other */
code > span.al { color: #ef2929; } /* Alert */
code > span.fu { color: #000000; } /* Function */
code > span.er { color: #a40000; font-weight: bold; } /* Error */
code > span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #000000; } /* Constant */
code > span.sc { color: #000000; } /* SpecialChar */
code > span.vs { color: #4e9a06; } /* VerbatimString */
code > span.ss { color: #4e9a06; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #000000; } /* Variable */
code > span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code > span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code > span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code > span.ex { } /* Extension */
code > span.at { color: #c4a000; } /* Attribute */
code > span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code > span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<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>
<link rel="stylesheet" href="styles.css" type="text/css" />
</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: 45px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 50px;
margin-top: -50px;
}
.section h2 {
padding-top: 50px;
margin-top: -50px;
}
.section h3 {
padding-top: 50px;
margin-top: -50px;
}
.section h4 {
padding-top: 50px;
margin-top: -50px;
}
.section h5 {
padding-top: 50px;
margin-top: -50px;
}
.section h6 {
padding-top: 50px;
margin-top: -50px;
}
</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>
$(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"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="morph_q.html">Morphometric questions</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="fa fa-database"></span>
Data sets
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="ea_iris.html">Anderson's Irises</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="glyphicon glyphicon-cloud"></span>
Data sanitation
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="data_sanitation.html">Data sanitation</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="fa fa-group"></span>
Groups
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="groups.html">How many groups of samples are in my data?</a>
</li>
<li>
<a href="pca.html">Principle components</a>
</li>
<li>
<a href="kmeans.html">K-means clustering</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="fa fa-group"></span>
Differentiation
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="differentiation.html">How to differentiate among groups of samples in my data?</a>
</li>
</ul>
</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">K-means clustering</h1>
</div>
<div id="TOC">
<ul>
<li><a href="#references">References</a></li>
</ul>
</div>
<p><span class="citation">(James et al. 2013; Markham 2014)</span></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">data</span>(iris)
<span class="kw">head</span>(iris)</code></pre></div>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># kmeans(iris[,1:4], centers = 2)</span>
myGroups <-<span class="st"> </span><span class="dv">2</span>:<span class="dv">20</span>
myKmeans <-<span class="st"> </span><span class="kw">vector</span>(<span class="st">"list"</span>, <span class="dt">length =</span> <span class="kw">length</span>(myGroups) -<span class="st"> </span><span class="dv">1</span>)
<span class="kw">sapply</span>(myGroups, function(x){ myKmeans[[x<span class="dv">-1</span>]] <<-<span class="st"> </span><span class="kw">kmeans</span>(iris[,<span class="dv">1</span>:<span class="dv">4</span>], <span class="dt">centers =</span> x) })</code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4] [,5]
## cluster Integer,150 Integer,150 Integer,150 Integer,150 Integer,150
## centers Numeric,8 Numeric,12 Numeric,16 Numeric,20 Numeric,24
## totss 681.3706 681.3706 681.3706 681.3706 681.3706
## withinss Numeric,2 Numeric,3 Numeric,4 Numeric,5 Numeric,6
## tot.withinss 152.348 78.85144 71.44525 49.82228 42.16294
## betweenss 529.0226 602.5192 609.9254 631.5483 639.2077
## size Integer,2 Integer,3 Integer,4 Integer,5 Integer,6
## iter 1 3 3 3 2
## ifault 0 0 0 0 0
## [,6] [,7] [,8] [,9] [,10]
## cluster Integer,150 Integer,150 Integer,150 Integer,150 Integer,150
## centers Numeric,28 Numeric,32 Numeric,36 Numeric,40 Numeric,44
## totss 681.3706 681.3706 681.3706 681.3706 681.3706
## withinss Numeric,7 Numeric,8 Numeric,9 Numeric,10 Numeric,11
## tot.withinss 47.02198 33.08052 34.50123 26.67797 26.86649
## betweenss 634.3486 648.2901 646.8694 654.6926 654.5041
## size Integer,7 Integer,8 Integer,9 Integer,10 Integer,11
## iter 3 3 3 4 3
## ifault 0 0 0 0 0
## [,11] [,12] [,13] [,14] [,15]
## cluster Integer,150 Integer,150 Integer,150 Integer,150 Integer,150
## centers Numeric,48 Numeric,52 Numeric,56 Numeric,60 Numeric,64
## totss 681.3706 681.3706 681.3706 681.3706 681.3706
## withinss Numeric,12 Numeric,13 Numeric,14 Numeric,15 Numeric,16
## tot.withinss 25.6458 22.69839 21.62515 20.8116 20.49106
## betweenss 655.7248 658.6722 659.7454 660.559 660.8795
## size Integer,12 Integer,13 Integer,14 Integer,15 Integer,16
## iter 2 3 4 4 3
## ifault 0 0 0 0 0
## [,16] [,17] [,18] [,19]
## cluster Integer,150 Integer,150 Integer,150 Integer,150
## centers Numeric,68 Numeric,72 Numeric,76 Numeric,80
## totss 681.3706 681.3706 681.3706 681.3706
## withinss Numeric,17 Numeric,18 Numeric,19 Numeric,20
## tot.withinss 18.83128 17.05898 15.92361 16.73973
## betweenss 662.5393 664.3116 665.447 664.6309
## size Integer,17 Integer,18 Integer,19 Integer,20
## iter 4 5 3 4
## ifault 0 0 0 0</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(myGroups, <span class="kw">unlist</span>(<span class="kw">lapply</span>(myKmeans, function(x){x$tot.withinss})), <span class="dt">ylab =</span> <span class="st">"Winthin ggroup SS"</span>, <span class="dt">type =</span> <span class="st">'b'</span>)</code></pre></div>
<p><img src="kmeans_files/figure-html/unnamed-chunk-3-1.png" width="960" style="display: block; margin: auto;" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co">#myMat <- matrix(1, ncol = nrow(iris), nrow = 2)</span>
<span class="co">#barplot(myMat, col = c(myKmeans[[5-1]]$cluster, iris$Species), border = NA)</span></code></pre></div>
<div id="references" class="section level1 unnumbered">
<h1>References</h1>
<div id="refs" class="references">
<div id="ref-james2013introduction">
<p>James, Gareth, Daniela Witten, Trevor Hastie, and Robert Tibshirani. 2013. <em>An Introduction to Statistical Learning</em>. Vol. 6. Springer. <a href="http://www-bcf.usc.edu/~gareth/ISL/" class="uri">http://www-bcf.usc.edu/~gareth/ISL/</a>.</p>
</div>
<div id="ref-islrblog">
<p>Markham, Kevin. 2014. “In-Depth Introduction to Machine Learning in 15 Hours of Expert Videos.” Blog. <a href="https://www.r-bloggers.com/in-depth-introduction-to-machine-learning-in-15-hours-of-expert-videos/" class="uri">https://www.r-bloggers.com/in-depth-introduction-to-machine-learning-in-15-hours-of-expert-videos/</a>.</p>
</div>
</div>
</div>
<center>
<hr>
<p>Copyright © Brian J. Knaus. All rights reserved.</p>
</center>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</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>