-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering.php
470 lines (437 loc) · 19.5 KB
/
clustering.php
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php include('header.php') ?>
<style>
/* <!-- To add --> */
#knn {
display: flex;
flex-direction: column;
align-items: center;
font-family: Helvetica;
background-color: white;
color: black;
}
.d3ml__settings {
display: flex;
justify-content: space-around;
}
.d3ml__settings__group:first-of-type {
display: flex;
flex-direction: column;
}
</style>
<script src="https://rawgit.com/StefanieStoppel/d3ML/master/lib/d3ml.min.js"></script>
<!-- Container (Section) -->
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h2>Clustering</h2><br>
<h4>Clustering comes under unsupervised machine learning. It is done only using the input data that is available, and is used to determine the hidden patterns, similarities and anomalies present in the data. Clustering problems have observations divided into subsets into different subsets, each of them containing similar information. These subsets are called clusters. With clustering the groups (or clusters) are based on the similarities of data instances to each other. No predefined output class is used in training and the clustering algorithm is supposed to learn the grouping. An example of use would be an online movie company recommending you a movie because other customers who had made similar movie choices as you in the past (i,e. the cluster you fall in) have favorably rated that movie.</h4>
</div>
<div class="col-sm-4">
</br></br></br></br></br></br></br>
<img src = "images/cluster-image.png" alt="regression image">
</div>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<h4><strong>IMPORTANCE:</strong> <p>Clustering is similar to classification, but the basis is different. In Clustering you don’t know what you are looking for, and you are trying to identify some segments or clusters in your data. When you use clustering algorithms on your dataset, unexpected things can suddenly pop up like structures, clusters and groupings you would have never thought of otherwise.</p></h4><br>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<h4> In the visualization you can see how the <strong>k-nearest neighbor (KNN)</strong> algorithm
<strong>classifies</strong> new data points based on their <strong>k</strong> nearest neighbors, where the amount of neighbors k is specified using the slider above. The new point will be assigned the <strong>class</strong> that the majority of its k nearest neighbors hold.
</h4>
<h4>Click on the grey area to create new circles and see how their color changes based on their neighbors.</h4>
<div id="knn"></div>
<h4>The initial circles are our "training" data set. The data set is divided into two classes: <strong>red and blue</strong>.
When we add a new circle, we want to find out which class (aka color) we need to assign it to, depending on its neighbors.
<br><br>
What's going on behind the scenes after you add a circle:
</p>
<ol>
<li>The algorithm goes through all other circles and calculates their distance from your new circle.</li>
<li>It then sorts them by distance from your circle in ascending order, meaning the circles with the smallest distances to the new circle come first.</li>
<li>It picks the first k entries from the result of step 2.</li>
<li>It looks at the k nearest neighbors' classes. If the majority of them are blue, our new point will be blue as well. If most of them are red, then the new point will be red.</li>
</ol>
<h3>The weighted KNN:</h3></h4>
<h4>If you check the <strong>"Weighted" checkbox</strong> above, the algorithm is justified, so that the <strong>inverse of the distance</strong> of the k neighbors is taken into account. This is important during "ties", meaning that you chose an even amount of neighbors k and half of them are class red while the other half are blue.
If you don't use the weighted version of KNN in this case, the neighbor with the closest distance will <strong>ALWAYS</strong> win, so that your circle will take on its color.
</h4>
</div>
</div>
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h3><b>Import the libraries<b></h3><br>
<h4>In this section, we will see how Python's Scikit-Learn library can be used to implement the KNN algorithm in less than 20 lines of code.</h4>
<h4>We are going to use the famous iris data set for our KNN example. The dataset consists of four attributes: sepal-width, sepal-length, petal-width and petal-length. These are the attributes of specific types of iris plant. The task is to predict the class to which these plants belong. There are three classes in the dataset: Iris-setosa, Iris-versicolor and Iris-virginica.</h4>
</div>
<div class="col-sm-4 col-xs-12">
<!-- <div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Box 1</h1>
</div>
<div class="panel-footer">
<h3>Measure</h3>
<h4>Look where you stand out against your peers</h4>
<button class="btn btn-lg">Get started</button>
</div>
</div> -->
</div>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<p>import pandas ad pd</p>
<p>import matplotlib.plyplot as plt</p>
<p>import numpy as np</p>
</div>
</div>
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h3><b>Import and Preprocess the Dataset<b></h3><br>
<h4>To import the dataset and load it into our pandas dataframe, execute the following code</h4>
<h4>To see what the dataset actually looks like, execute the following command - dataset.head()</h4>
<h4>Executing the above script will display the first five rows of our dataset</h4>
<h4> To preprocess split our dataset into its attributes and labels. The X variable contains the first four columns of the dataset (i.e. attributes) while y contains the labels.</h4>
</div>
<div class="col-sm-4 col-xs-12">
<!-- <div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Box 2</h1>
</div>
<div class="panel-footer">
<h3>Measure</h3>
<h4>Look where you stand out against your peers</h4>
<button class="btn btn-lg">Get started</button>
</div>
</div> -->
</div>
</div>
<img src = "images/tableknn-image.jpg" align="center" alt="data image"><br><br>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<p>url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"</p>
<p>names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']</p>
<p>dataset = pd.read_csv(url, names=names)</p>
<p>dataset.head()</p></br>
<p>X = dataset.iloc[:, :-1].values</p>
<p>Y = dataset.iloc[:, 4].values</p>
</div>
</div>
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h3><b>Train Test Split<b></h3><br>
<h4>To avoid over-fitting, we will divide our dataset into training and test splits, which gives us a better idea as to how our algorithm performed during the testing phase. This way our algorithm is tested on un-seen data, as it would be in a production application.</h4>
<h4>To create training and test splits, execute the following script as below</h4>
<h4>The above script splits the dataset into 80% train data and 20% test data. This means that out of total 150 records, the training set will contain 120 records and the test set contains 30 of those records.</h4>
</div>
<div class="col-sm-4 col-xs-12">
<!-- <div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Box 3</h1>
</div>
<div class="panel-footer">
<h3>Measure</h3>
<h4>Look where you stand out against your peers</h4>
<button class="btn btn-lg">Get started</button>
</div>
</div> -->
</div>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<p>from sklearn.model_selection import train_test_split</p>
<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)</p>
</div>
</div>
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h3><b>Feature Scaling<b></h3><br>
<h4>Before making any actual predictions, it is always a good practice to scale the features so that all of them can be uniformly evaluated.
The gradient descent algorithm (which is used in neural network training and other machine learning algorithms) also converges faster with normalized features.</h4>
<h4>Since the range of values of raw data varies widely, in some machine learning algorithms, objective functions will not work properly without normalization. For example, the majority of classifiers calculate the distance between two points by the Euclidean distance. If one of the features has a broad range of values, the distance will be governed by this particular feature. Therefore, the range of all features should be normalized so that each feature contributes approximately proportionately to the final distance.</h4>
</div>
<div class="col-sm-4 col-xs-12">
<!-- <div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Box 4</h1>
</div>
<div class="panel-footer">
<h3>Measure</h3>
<h4>Look where you stand out against your peers</h4>
<button class="btn btn-lg">Get started</button>
</div>
</div> -->
</div>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<p>from sklearn.preprocessing import StandardScaler</p>
<p>scaler = StandardScaler()</p>
<p>scaler.fit(X_train)</p></br>
<p>X_train = scaler.transform(X_train)</p>
<p>X_test = scaler.transform(X_test)</p>
</div>
</div>
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h3><b>Training and Predictions<b></h3><br>
<h4>The next step is to import the KNeighborsClassifier class from the sklearn.neighbors library. In the second line, this class is initialized with one parameter, i.e. n_neigbours. This is basically the value for the K. There is no ideal value for K and it is selected after testing and evaluation, however to start out, 5 seems to be the most commonly used value for KNN algorithm.</h4>
<h4>The final step is to make predictions on our test data. To do so, execute the following script - y_pred = classifier.predict(X_test)</h4>
<h4>For evaluating an algorithm, confusion matrix, precision, recall and f1 score are the most commonly used metrics. The confusion_matrix and classification_report methods of the sklearn.metrics can be used to calculate these metrics.</h4>
</div>
<div class="col-sm-4 col-xs-12">
<!-- <div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Box 5</h1>
</div>
<div class="panel-footer">
<h3>Measure</h3>
<h4>Look where you stand out against your peers</h4>
<button class="btn btn-lg">Get started</button>
</div>
</div> -->
</div>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<p>from sklearn.neighbors import KNeighborsClassifier</p>
<p>classifier = KNeighborsClassifier(n_neighbors=5)</p>
<p>classifier.fit(X_train, y_train)</p></br>
<p>y_pred = classifier.predict(X_test)</p></br>
<p>from sklearn.metrics import classification_report, confusion_matrix</p>
<p>print(confusion_matrix(y_test, y_pred))</p>
<p>print(classification_report(y_test, y_pred))</p>
</div>
</div>
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-8">
<h3><b>Comparing Error Rate with the K value</b></h3><br>
<h4>In the training and prediction section we said that there is no way to know beforehand which value of K that yields the best results in the first go. We randomly chose 5 as the K value and it just happen to result in 100% accuracy.</h4>
<h4>One way to help you find the best value of K is to plot the graph of K value and the corresponding error rate for the dataset.</h4>
<h4>The next step is to plot the error values against K values.</h4>
<h4>From the output we can see that the mean error is zero when the value of the K is between 5 and 18. I would advise you to play around with the value of K to see how it impacts the accuracy of the predictions.</h4>
</div>
<div class="col-sm-4 col-xs-12">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Clustering</h1>
</div>
<div class="panel-footer">
<h3>Question 1</h3>
<p>KNN is <input style= "color:black; text-decoration: underline; border:#000;" id='answer1'class="panel-footer" type="text" name="" value=""> algorithm and therefore requires no training prior to making real time predictions.</p>
<p id="right1" style="display:none; color:#008000">You are right! Good going!</p>
<p id="wrong1" style="display:none; color:#ff0000">Oops! You are wrong. Please try again.</p>
<button class="btn btn-lg" id="question1" type="button" name="button">Submit</button>
<!-- <button class="btn btn-lg" type="button" name="button">See answer</button> -->
</div>
</div>
</div>
</div>
</div>
<div class="container-fluid bg-grey">
<div class="row">
<p>error = []</p>
<p>for i in range(1, 40):</p>
<p>knn = KNeighborsClassifier(n_neighbors=i)</p>
<p>knn.fit(X_train, y_train)</p>
<p>pred_i = knn.predict(X_test)</p>
<p>error.append(np.mean(pred_i != y_test))</p></br>
<p>plt.figure(figsize=(12, 6))</p>
<p>plt.plot(range(1, 40), error, color='red', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=10)</p>
<p>plt.title('Error Rate K Value')</p>
<p>plt.xlabel('K Value')</p>
<p>plt.ylabel('Mean Error')</p>
</div>
</div>
<img src = "images/knn-image.png" align="center" alt="knn image"><br><br>
<!-- Container (Section) -->
<div id="services" class="container-fluid text-center">
<h2>IN DEPTH</h2>
<h4>Machine Learning</h4>
<br>
<div class="row slideanim">
<div class="col-sm-4">
<span class="glyphicon glyphicon-off logo-small"></span>
<h4>DATA PREPROCESSING</h4>
<p>First and important step</p>
</div>
<div class="col-sm-4">
<span class="glyphicon glyphicon-ok logo-small"></span>
<h4>REGRESSION</h4>
<p>Numerical Predictive Analysis</p>
</div>
<div class="col-sm-4">
<span class="glyphicon glyphicon-th-large logo-small"></span>
<h4>CLASSIFICATION</h4>
<p>Categorical Predictive Analysis</p>
</div>
</div>
<br><br>
<div class="row slideanim">
<div class="col-sm-4">
<span class="glyphicon glyphicon-barcode logo-small"></span>
<h4>CLUSTERING</h4>
<p>Unsupervised Predictive Ananlysis</p>
</div>
<div class="col-sm-4">
<span class="glyphicon glyphicon-certificate logo-small"></span>
<h4>NEURAL NETWORKS</h4>
<p>Advanced computing systems similar to the brain</p>
</div>
<div class="col-sm-4">
<span class="glyphicon glyphicon-wrench logo-small"></span>
<h4 style="color:#303030;">DIMENSIONALITY REDUCTION</h4>
<p>Increasing efficiency everytime</p>
</div>
</div>
</div>
<!-- Container (Section) -->
<div id="pricing" class="container-fluid">
<div class="text-center">
<h2>Let's Get Started</h2>
<h4>Choose a section that you want to start with</h4>
</div>
<div class="row slideanim">
<div class="col-sm-4 col-xs-12">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Tutorials</h1>
</div>
<div class="panel-footer">
<h3>Learn</h3>
<h4>Learn about differnet algorithms in ML</h4>
<button class="btn btn-lg"><a href="regression.php">Go back to data processing!</a></button>
</div>
</div>
</div>
<div class="col-sm-4 col-xs-12">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Quiz</h1>
</div>
<div class="panel-footer">
<h3>Evaluate</h3>
<h4>Understand how well you know your concepts</h4>
<?php if(isset($_SESSION['name'])){echo '<button class="btn btn-lg"><a href="Classification_Quiz.php">Take quiz!</a></button>';}
else{echo "<button class='btn btn-lg'>Please ".'<a href="/tutscorner/login.php">'.'login'.'</a>'." to take the quiz.</button>";} ?>
</div>
</div>
</div>
<div class="col-sm-4 col-xs-12">
<div class="panel panel-default text-center">
<div class="panel-heading">
<h1>Scoreboard</h1>
</div>
<div class="panel-footer">
<h3>Measure</h3>
<h4>Look where you stand out against your peers</h4>
<button class="btn btn-lg"><a href="Scoreboard.php">Scoreboard</a></button>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$("#question1").on('click', function(e){
e.preventDefault();
$answer = $("#answer1").val();
console.log('check');
console.log($answer);
if($answer=="lazy learning"){
console.log("Right");
$('#right1').show();
$('#wrong1').hide();
}
else{
$('#wrong1').show();
$('#right1').hide();
}
})
</script>
<script>
$(document).ready(function(){
// Add smooth scrolling to all links in navbar + footer link
$(".navbar a, footer a[href='#myPage']").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (900) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 900, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
const displayWidth = window.innerWidth - 25;
const displayHeight = 450;
const dataSetSize = 250;
const options = {
rootNode: '#knn',
width: displayWidth,
height: displayHeight,
backgroundColor: '#0f3c3d',
cirlceFill: 'white',
circleStroke: 'white'
};
const types = ['A', 'B'];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function createRandomEllipsoidCoordinates(width, height, cx, cy) {
const rho = Math.sqrt(Math.random())
const phi = Math.random() * Math.PI * 2
const rands = {x: getRandomInt(-width/2,width/2), y: getRandomInt(-height/2,height/2)}
const x = (rho * Math.cos(phi) * width/2) + cx + rands.x
const y = (rho * Math.sin(phi) * height/2) + cy + rands.y
return {x, y}
}
function createRandomData() {
const ellipsoidOptions = {
'A': {
width: displayWidth/3,
height: displayWidth/3,
cx: displayWidth/3,
cy: displayHeight/3
},
'B': {
width: displayWidth/2.5,
height: displayWidth/2.5,
cx: displayWidth*0.663,
cy: displayHeight*0.66
}
};
return Array.apply(null, Array(dataSetSize))
.map(d => {
const type = Math.random() > 0.5 ? types[0] : types[1];
const {width, height, cx, cy} = ellipsoidOptions[type]
const {x, y} = createRandomEllipsoidCoordinates(width, height, cx, cy);
return {x, y, type};
}
);
}
const data = createRandomData();
const k = 3;
const vis = new d3ml.KNNVisualization(data, options, types, k);
vis.draw();
});
</script>
<?php include('footer.php') ?>