forked from lqwk/ucla-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs188-sankararaman-ML
1104 lines (777 loc) · 26.7 KB
/
cs188-sankararaman-ML
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Computer Science 188 - Introduction to Machine Learning
http://web.cs.ucla.edu/~sriram/courses/cs188.winter-2017/html/index.html
==============
Jan. 10, 2017
==============
mini-quiz (30 mins) on Tue. Jan. 17.
Course in machine learning by Hal Daume III (CIML)
Machine Learning: The art and science of algorithms that make sense of data by
Peter Flach (FL)
Exams
------
Midterm (Feb. 14)
Final (Mar. 22)
closed book, closed notes
Python 2.7
-----------
numpy
scipy
scikit-learn
When do we use ML?
-------------------
- human expertise does not exist
+ navigating on Mars
- humans cannot explain their expertise
+ speech recognition
- algorithms must be customized
+ personalized medicine
- data exists to acquire expertise
+ genomics
- recognizing patterns
+ facial identities or facial expressions
+ handwritten or spoken words
+ medical images
- generating patterns
+ generating images or motion sequences
- recognizing anomalies
+ unusual credit card transactions
+ unusual patterns of sensor readings in a nuclear power plant
- prediction
+ future stock prices or currency exchange rates
Machine Learning
-----------------
Study of algorithms that
- improve their performance P
- at some task T
- with experience E
A well-defined learning task is given by <P, T, E>
Defining the Learning Task
---------------------------
Improve on task T with respect to performance metric P, based on experience E
T: Playing checkers
P: Percentage of games won agianst arbitrary opponents
E: Playing practice games agianst itself
T: Recognizing hand-written words
P: Percentage of words correctly classified
E: Database of human-labeled images of handwritten words
T: Driving on four-lane highways using vision sensors
P: Average distance traveled before a human-judged error
E: Sequence of images and steering commands recorded while observing a human
driver
T: Categorize email messages as spam or legitimate
P: Percentage of email messages correctly classified
E: Database of emails, some with human-given labels
TYPES OF LEARNING
Supervised (Inductive) Learning
--------------------------------
Learning with a teacher
Given: labeled traning instances (examples)
Goal: learn mapping that predicts label for test instance
Regression
-----------
Given (x1, y1), (x2, y2), ... , (xn, yn)
Learn a function f(x) to predict y given x
* y is real-valued == regression
Classification
---------------
Given (x1, y1), (x2, y2), ... , (xn, yn)
Learn a function f(x) to predict y given x
* y is categorical == classification
x can be multi-dimensional
each dimension corresponds to an attribute
Unsupervised Learning
----------------------
Learning without a teacher
Given: unlabeled inputs
Goal: learn some intrinsic structure in inputs
Given x1, x2, ... , xn (without labels)
Output hidden structure behind the x's
- clustering
- gemonics application
group individuals by genetic similarity
- independent component analysis
separate a combined signal into its original sources
two people are talking, separate the voices
Reinforcement Learning
-----------------------
Learning by interacting
Given: agent interacting in environment (having set of states)
Goal: learn policy (state to action mapping) that maximizes agent's reward
Given sequence of states and actions with (delayed) rewards
Learn policy that maximizes agent's reward
- game playing
given sequences of moves and whether or not the player won at the end,
learn to make good moves
- robot in maze
FRAMING A LEARNING PROBLEM
Representing Instances/Examples
--------------------------------
What is an instance?
How is it represented?
- Define a list of features.
This is how the algorithm views the data.
Features are the questions we can ask about the instances.
Red Apple: red, round, leaf, 3oz, ...
Green Apple: green, round, no leaf, 4oz, ...
Yellow Banana: yellow, curved, no leaf, 4oz, ...
Green Banana: green, curved, no leaf, 5oz, ...
During learning/traning/induction, learn a model of what distinguishes apples
and bananas based on the features.
Generate a classifier.
With a new instance, apply the classifier.
Classifier classifies a new instance based on the features.
Learning Algorithm
-------------------
Learning is about *generalizing* from training data
What does this *assume* about training and test set?
- Don't see a test instance exactly the same as a training instance
Generalization Ability: performance of the learning algorithm
How do we measure performance depends on the problem we are trying to solve
The training and test data should be strongly related
Loss function L(y, y^)
tells us how bad the system's prediction of y is compared to the true
value of y
- A loss function for regression (squared loss)
L(y, y^) = (y - y^)^2
- A loss function for classification
L(y, y^) = 0 if y = y^
1 otherwise
Use the probabilistic model of learning
There is some unknown probability distribution p over instance/label pairs
called the data generating distribution
Learning Problem defined by
loss function: measures of performance
data generating distribution: what data do we expect to see
characterizes experience
Problem Setting
set of possible instances X
set of possible labels Y
unknown target function f: X -> Y
set of function hypothesis: H = { h | h: X -> Y }
Input
training instances drawn from the dta generating distribution p
{ (xi, yi) }i = 1 -> n = { (x1, y1), ... , (xn, yn) }
Output
hypothesis h in H that best approximates f
h should do well (as measured by the loss) on future instances
formally, h should have low expected (test) loss/risk
E((x,y)~p)[L(y,h(x))] = sum(x,y) p(x,y)L(y,h(x))
Problem: we don't know what p is
but we are given samples drawn from p
we instead approximate the risk by the training error/empirical risk
1/n sum(i = i -> n) L(yi, h(xi))
why is this reasonable?
both the training data and the test data set are generated based on
this distribution
Problem: can make the training error zero by memorizing
Regression
-----------
For regression, the common choice is squared loss
L(yi,h(xi)) = (yi - h(xi))^2
Empirical loss of function h applied to training data is then
1/n sum(i = 1 -> n) L(yi,h(xi)) = 1/n sum(i = 1 -> n) (yi - h(xi))^2
Fundamental Difficulties of Machine Learning
---------------------------------------------
we have access to the training error but really care about the expected loss
our learned funciton needs to generalize beyond the training data
Key Issues in Machine Learning
-------------------------------
Representation: how to choose a hypothesis space?
- often we use prior knowledge to guide this choice
- the ability to answer the next two questions also affects choice
- which spaces have been useful in practical applications and why?
Optimization: how to find the best hypothesis within this space?
- (the ALGORITHMIC question), at the intersection of computer science
and optimization research
- are some learning problems computationally intractable?
(the COMPUTATIONAL quesion)
Evaluation: how to gauge the accuracy of a hypothesis on unseen testing data?
- the previous exampe showed that choosing the hypothesis which simply
minimizes training set error (i.e. empirical loss) is not optimal
- this question is the main topic of learning theory
- how can we have confidence in the results?
(the STATISTICAL question)
Formulation: how can we formulate application problems as machine learning
problems? (the ENGINEERING question)
Machine Learning in Practice
-----------------------------
+
L | understand domain, prior knowledge, and goals
O | data integration, selection, cleaning, pre-processing
O | learn models
P | interpret results
+
==============
Jan. 12, 2017
==============
Talking about probability and statistics, for uncertainty
Random Variables
-----------------
Example: Toss a coin twice
---------------------------
Sample Space (omega): {HH, HT, TH, TT}
P(HH) = 1/4
P(HT) = 1/4
P(TH) = 1/4
P(TT) = 1/4
random variable: X = number of heads observed, can be in {0, 1, 2}
P(X=0) = 1/4
P(X=1) = 1/2
P(X=2) = 1/4
P(X=0 or X=1) = P(X=0 v X=1) = P(X=0) + P(X=1) = 3/4
+--------------------------+
| omega | area(omega) = 1
| +------+ +------+ |
| | A1 | | A2 | |
| +------+ +------+ |
| |
+--------------------------+
Axioms of Probability
----------------------
omega : sample space
A : event
(1) P(A) >= 0 for all events A
(2) P(omega) = 1
(3) P(A1 v A2) = P(A1) + P(A2) for A1 ^ A2 = NIL
Corollaries
------------
(1) P(A^C) = 1 - P(A)
(2) P(A1 v A2) = P(A1) + P(A2) - P(A1 ^ A2)
Discrete Random Variables
--------------------------
X in {x_1, ..., x_M}
Probability Mass Function: P_X(x_m) = P(X = x_m)
Example
--------
(1) (2)
Box Red Blue
Balls Red Blue
Ball Red Blue
Box
Red 6 2
Blue 1 3
X(box) {r, b} {x_1, ..., x_m}
Y(Ball) {r, b} {y_1, ..., y_l}
Y
+--------------------
| |
X |----+--------------- c_i
| |
| |
| |
r_j
P(X=x_i, Y=y_j)
repeat N times
n_ij = #{X=x_i and Y=y_j}
c_i = #{X=x_i}
r_j = #{Y=y_j}
N = total number of times of experiment
Joint Probability
------------------
P(X=x_i and Y=y_j)
P(X=x_i, Y=y_j) = n_ij / N
Marginal Probability
---------------------
Sum Rule of Probability
P(X=x_i) = c_i / N with c_i = sum{j} n_ij
= sum{j} n_ij / N
= sum{j} (n_ij / N)
= sum{j} P(X=x_i, Y=y_j)
P(Y=y_j) = sum{i} P(X=x_i, Y=y_j)
Conditional Probability
------------------------
P(Y=y_j | X=x_i) = n_ij / c_i
= (n_ij / N) / (c_i / N)
= P(X=x_i, Y=y_j) / P(X=x_i)
Summary
--------
Sum Rule: P(X) = sum{y} P(X,y)
Product Rule: P(X,Y) = P(Y|X)P(X)
= P(X|Y)P(Y)
Bayes Theorem: P(X|Y) = P(Y|X)P(X) / P(Y)
Independence
-------------
X, Y are independent iff P(X,Y) = P(X)P(Y)
P(X,Y) = P(X|Y)P(Y) = P(X)P(Y) ==> P(X|Y) = P(X)
Continuous Random Variables
----------------------------
X is in R
P(X in [x, x+dx]) = p(x)dx
Probability Density Function (PDF)
-----------------------------------
p(x) is the PDF for continuous random variables
P(X in [a,b]) = integrate{a -> b} p(x)dx
(1) p(x) >= 0
(2) integrate{-infty -> +infty} p(x)dx = 1
Cumulative Density Function (CDF)
----------------------------------
probability that X is less than or equal to some chosen value x
P(X <= x)
given X,Y, want to find
P(X in [x1,x2], Y in [y1,y2])
= integrate{x1 -> x2} integrate{y1 -> y2} p(x,y)dxdy
p(x,y) : joint probability density function
Expectation & Variance
-----------------------
random variable X with distribution p(x): X ~ p(x)
Let X in {x_1, ... , x_M}
Expectation: E[X] = sum{m=1 -> M} x_m * p(x_m)
E[X] = integrate{-infty -> +infty} x * p(x) * dx
Variance: Var[X] = sum{m=1 -> M} (x_m - E[X])^2 * p(x_m)
= E[ (X - E[X])^2 ]
Var[X] = integrate{-infty -> +infty} (x - E[X])^2 * p(x) * dx
E[g(X)] = sum{m=1 -> M} g(x_m) * p(x_m)
g(x) can be any function
g(x) = (x-E[X])^2 -> Variance
---------------------------------
Discrete: Binary Random Variable
Bernoulli Distribution
---------------------------------
X ~ Ber(p)
P(X=0) & P(X=1) with P(X=0) + P(X=1) = 1
usually we have P(X=1) = p (0 <= p <= 1)
probability mass function = { p x = 1 }
{ 1-p x = 0 }
{ 0 x = others}
Parameters: p
-----------------------------------------
Continuous: Normal/Guassian Distribution
-----------------------------------------
X is in R
PDF: p(x) = 1/sqrt(2 * pi * sigma^2) * exp( (-1)/(2 * sigma^2) (x - mu)^2 )
Parameters: (mu, sigma)
E[X] = mu
Var[X] = sigma^2
==============
Jan. 17, 2017
==============
X ~ Ber(p)
X is in {0,1}
P(X = 1) = p
P(X = 0) = 1-p
statistics: inverse probability
--------------------------------
probability : parameters ----> samples/data
bernoulli distribution : p ----> X
normal distribution : (mu, sigma^2) ----> X
general distribution : theta ----> X
Example 1
----------
Toss a coin 5 times, observe 3 heads
Goal: find p
likelihood
-----------
X1, X2, X3, X4, X5
likelihood function
L(theta) = P(X1, X2, X3, X4, X5; theta)
each toss is a Bernoulli random variable
Xi = 1 if toss landed heads
= 0 otherwise
Xi ~ Ber(p)
we have 5 independent tosses
X1 X2 X3 X4 X5
1 0 1 1 0
L(theta) = P(X1, X2, X3, X4, X5 | theta)
= P(X1;theta)P(X2;theta)P(X3;theta)P(X4;theta)P(X5;theta)
= theta * (1-theta) * theta * theta * (1-theta)
= theta^3 (1-theta)^2
find theta that maximizes the likelihood: L(theta)
* ^theta = argmax L(theta)
^theta: Maximum Likelihood Estimator (MLE)
^theta = 3/5
Probabilistic/Statistical Models
---------------------------------
P(X; theta)
for coin tossing we have a Bernoulli model, other models for others
data/observation: samples drawn from the model
D = {x1, x2, ... , xn}
xi ~ P(xi, theta)
^
|
|
drawn independently from the model
likelihood
-----------
L(theta) = P(x1, ... , xN; theta)
= prod{i=1 -> n} P(xi; theta)
Optimization
-------------
max f(x)
x
^
f(x) |
|
| global max
| ____
| / \
| local max / \
| ____ / \____
| / \ /
| ___/ \_/
|
+-------------------------------> x
Fermat's Theorem: all optima of f(x) occrs where f'(x) = 0
Example 2
----------
X ~ N(mu, sigma^2)
^ ^
| |
| |
mean variance
p(x; (mu, sigma^2)) = 1/sqrt(2*pi*sigma^2) ((-1/2*sigma^2) * (x-mu)^2)
x1, x2, ... , xN
xn ~ N(mu, sigma^2)
^
|
|
independent
theta = ( mu )
( sigma^2 )
likelihood: L(mu, sigma^2) = P(x1, ... , xN; (mu, sigma^2))
= prod{n=1 -> N} P(xn; (mu, sigma^2))
(^mu, ^sigma^2) = argmax L(mu, sigma^2)
(mu, sigma^2)
need to optimize
optimization is an important field in ML
l(mu, sigma^2) = log(L(mu, sigma^2))
= log( prod{i=1 -> N} P(xn; (mu, sigma^2)) )
= sum{n=1 -> N} log( p(xn; (mu, sigma^2)) )
(^mu, ^sigma^2) = argmax l(mu, sigma^2)
(mu, sigma^2)
l(mu, sigma^2) = sum{n=1 -> N} log{ (1/sqrt(2*pi*sigma^2)) e^(-(xn-mu)/(2*sigma^2)) }
= sum{n=1 -> N} { ((-1/2*sigma^2)(xn-mu)^2) - (1/2)log(2*pi*sigma^2) }
dl/d(mu) (mu, sigma^2) = 0
dl/d(sigma^2) (mu, sigma^2) = 0
dl/d(mu) (mu, sigma^2) = sum{n=1 -> N} d/d(mu) (-1/2*sigma^2)(xn-mu)^2
= sum{n=1 -> N} (xn-mu)/sigma^2 = 0
^mu = (sum{n=1 -> N} xn) / N = bar(x) [sample mean]
^sigma^2 = (sum{n=1 -> N} (xn-bar(x))^2) / N [sample variance]
==============
Jan. 19, 2017
==============
Sample Dataset
---------------
columns denote features/attributes and labels/targets
rows denote instance-label pairs (xn,yn)
class label denotes whether tennis game was played
binary classification problem
For each decision tree
each internal node: test one feature
each edge: select one value for the feature
each leaf: predict class
allows to make a series of questions and choose next question to
get to the results we want
Tree
* <--------- root node
/ \ <------- edge
* * <----- internal node
/ \
* * <--- leaf
a decision tree partitions the feature space
Decision Tree Learning
-----------------------
setup
set of possible instances X
each instance x in X is a feature vector
(humidity=low, wind=weak, outlook=rain, temp=hot)
set of possible labels Y
Y is discrete valued
unknown target function f: X -> Y
model/hypothesis: H = {h|h: X -> Y}
each hypothesis h is a decision tree
goal : train/induce/learn a function h that maps instance to label
three things to learn
----------------------
(1) structure of the tree
(2) threshold values (theta_i)
(3) values for the leaves (A, B, ...)
-------------------
Learning Algorithms
-------------------
Ockham's Razor
---------------
The simplest consistent explanation is the best.
a form of inductive bias
find smallest decision tree that correctly classifies all training examples
NP-hard
instead, greedily construct a decision tree that is pretty small
Algorithm 1
------------
DecisionTreeTrain(data, features)
guess <- the most frequest label in data
if all labels in data are the same then
return LEAF(guess)
else
f <- the "best" feature in features
NO <- the subset of data on which f = NO
YES <- the subset of data on which f = YES
left <- DecisionTreeTrain(NO, features \ {f})
right <- DecisionTreeTrain(YES, features \ {f})
return NODE(f, left, right)
endif
how to choose the best feature
-------------------------------
possibilities
random: select a feature at random
highest accuracy: select feature with largest accuracy
max-gain: select feature with largest information gain
ID3 algorithm: one algorithm for decision tree learning
select the feature with largest information gain
which attribute to split at the root
idea: use information gain to choose the attribute
Entropy
--------
idea: gaining information reduces uncertainty
if a random variable X has K different values a_1, a_2, ..., a_K
it's entropy is given by
H[X] = -sum{k=1 -> K} P(X=a_k) * log(P(X=a_k))
^
|
if the base is 2, the unit of the entropy
is called "bit"
this measures the amount of uncertainty of a random variable with a
specific probability distribution
the higher it is, the less confident we are in its outcome
Conditional Entropy
--------------------
given two random variables X and Y
H[Y|X] = sum{k} P(X=a_k) * H[Y|X=a_k]
X : the attribute to be split
Y : outcome (binary)
information gain
GAIN = H[Y] - H[Y|X]
when H[Y] is fixed, we only need to compare conditional entropy
mutual information between Y and X
the expected reduction in entropy of the target variable Y due to
sorting on the feature X
example
-------
GAIN[Y,Patrons] = H[Y] - H[Y|Patrons]
= 1 - 0.45
= 0.55
GAIN[Y,Type] = H[Y] - H[Y|Type]
= 1 - 1
= 0
Algorithm 2
------------
DecisionTreeTrain(data, features)
guess <- the most frequest label in data
if all labels in data are the same then
return LEAF(guess)
else if features is empty then
return LEAF(guess)
else
f <- the "best" feature in features
NO <- the subset of data on which f = NO
YES <- the subset of data on which f = YES
left <- DecisionTreeTrain(NO, features \ {f})
right <- DecisionTreeTrain(YES, features \ {f})
return NODE(f, left, right)
endif
we need to careful to pick an appropriate tree depth
if the tree is too deep, we can overfit (memorize training data)
if the tree is too shallow, we underfit (not learn enough)
max depth is a hyperparameter that should be tuned by the data
a hyperparameter is
a parameter that controls the other parameters of the tree
max depth of 0 : underfitting
max depth of infty : overfitting
Reasons for Overfitting
------------------------
noisy data
two instances have the same feature values but different class labels
some of the feature or label values are incorrect
some features are irrelevant to classification
target variable is non-deterministic in the features
in general we cannot measure all the variables needed to predict
so target variable is not uniquely determined by the input feature values
training error is not guaranteed to be zero
* our decision tree learning procedure always increases traning set accuracy
* though training accuracy is increasing, test accuracy can decrease
how to get a realistic estimate of accuracy
--------------------------------------------
train model on training data
compute accuracy on test data
DOWNSIDE: throwing out some data
Cross-Validation (CV)
----------------------
don't just choose one particular split of the data
in principle, we should do this multiple times since performance may
be different for each split
K-Fold Cross Validation (e.g. K = 10)
randomly partition full data set of N instances into K disjoint subsets
each roughly of size N/K
choose each fold in turn as the test set
train model on the other folds and evaluate
compute average statistics over K test performances
can also do leave-one-out CV where K = N
recipe
-------
split training data into K equal parts
denote each part as D_k^{TRAIN}
for every k in [1,K]
train a model using D = D^{TRAIN} - D_k^{TRAIN}
evaluate the performance of the model on D_k^{TRAIN}
average the K performance metrics
Avoid Overfitting
------------------
get more training data
remove irrelevant features
decision tree pruning
prune while building tree (early stopping)
prune after building tree (post pruning)
Controlling the Size of the Tree
---------------------------------
if we cut the tree, not all traning sample would be classified correctly
we label the leaves of this smaller tree with the majority of traning
samples' labels
example
-------
****** <-- Wait: Yes
****** <-- Wait: No
Patrons?
/ | \
None Some Full
**** **
** **** <-- choose Wait: No
==============
Jan. 31, 2017
==============
special case: binary classification
instance (feature vectors): x in R^D
label: y in {-1, +1}
model/hypothesis:
H = {h|h: X -> Y, h(x) = sign(sum{})}
Perceptron Prediction
----------------------
input: x in R^D, w in R^D, b in R (D = dimensions)
a = sum{d=1 -> D} w_d * x_d + b = w^T * x + b
^y = sign(a)
output: ^y
sum{d=1 -> D} w_d * x_d + b: hyperplane in D dimensions with parameters (w,b)
w: weights
b: bias
a: activation
Hyperplane in D dimensions
---------------------------
x in R^D
sum{d=1 -> D} w_d * x_d + b = 0
x that satisfies the equation above is in the hyperplane
the set of points that satisfies the equation makes the hyperplane
D+1 dimensions
---------------
~x^T = (1, x_1, ..., x_D)
~w^T = (b, w_1, ..., w_D)
~w^T * ~x = 0
Perceptron Learning
--------------------
iteratively solving one case at a time
REPEAT
pick a data point x_n
compute a = w^T * x_n, using current w
if ay_n > 0, do nothing
else
w <- w + y_n * x_n
UNTIL converged
Properties of Perceptron Learning
----------------------------------
this is an online algorithm - looks at one instance at a time
convergence
if training data is not linearly separable, the algorithm does not converge
if the training data is linearly separable, the algorithm stops in a finite
number of steps (converges)
how long to convergence?
depends on the difficulty of the problem
how far apart are the neg and pos samples
instead of predicting the class, predict the probability of instance being
in a class
perceptron does not produce probability estimates
Logistic Classification
------------------------
setup for binary classification
input: x in R^D
output: y in {0,1}
training data: data = {(x_n, y_n), n = 1,2,...,N}
hypothesis/model:
h_w,b(x) = p(y=1 | x; b,w) = sigma(a(x))
where
a(x) = b + sum{d} w_d * x_d = b + w^T * x
and sigma() stands for the sigmoid function
sigma(a) = 1 / (1 + e^{-a})
where a = b + w^T * x
the sigmoid function will take the output and
map it to a number between 0 and 1
given training data N samples/instances:
data^train = {(x1, y1), ..., (x_N, y_N)}
train/learn/induce h_w,b
find values for (w,b)
properties of the sigmoid function