1 Classification: Definition Given a collection of records (training set ) Each record contains a set of attributes, one of the attributes is the class. Find a model for class attribute as a function of the values of other attributes. Goal: previously unseen records should be assigned a class as accurately as possible. A test set is used to determine the accuracy of the model. Usually, the given data set is divided into training and test sets, with training set used to build the model and test set used to validate it.
2 Clustering versus classification Intra-cluster distances are minimized Inter-cluster distances are maximized Clusteing: group similar records together
3 Clustering versus classification Classification: assign class label to unseen records Class 1 Class 3 Class 2 Test point
4 Illustrating Classification Task
5 Nearest Neighbor Classifiers Basic idea: If it walks like a duck, quacks like a duck, then it’s probably a duck Training Records Test Record Compute Distance Choose k of the “nearest” records
6 Nearest-Neighbor Classifiers l Requires three things –The set of stored records –Distance Metric to compute distance between records –The value of k, the number of nearest neighbors to retrieve l To classify an unknown record: –Compute distance to other training records –Identify k nearest neighbors –Use class labels of nearest neighbors to determine the class label of unknown record (e.g., by taking majority vote)
7 Definition of Nearest Neighbor K-nearest neighbors of a record x are data points that have the k smallest distance to x
8 1 nearest-neighbor Voronoi Diagram
9 Nearest Neighbor Classification Compute distance between two points: Euclidean distance Determine the class from nearest neighbor list take the majority vote of class labels among the k- nearest neighbors Weigh the vote according to distance weight factor, w = 1/d 2
10 Nearest Neighbor Classification Choosing the value of k: If k is too small, sensitive to noise points If k is too large, neighborhood may include points from other classes
11 Nearest Neighbor Classification Scaling issues Attributes may have to be scaled to prevent distance measures from being dominated by one of the attributes Example: height of a person may vary from 1.5m to 1.8m weight of a person may vary from 90lb to 300lb income of a person may vary from $10K to $1M
12 Nearest Neighbor Classification Problem with Euclidean measure: High dimensional data curse of dimensionality Can produce counter-intuitive results vs d = Solution: Normalize the vectors to unit length
13 Nearest neighbor Classification k-NN classifiers are lazy learners It does not build models explicitly Classifying unknown records are relatively expensive
14 SVM-Intuitions X X O O O O O O X X X X X X O O
15 Intuitions X X O O O O O O X X X X X X O O
16 Intuitions X X O O O O O O X X X X X X O O
17 Intuitions X X O O O O O O X X X X X X O O
18 A “Good” Separator X X O O O O O O X X X X X X O O
19 Noise in the Observations X X O O O O O O X X X X X X O O
20 Ruling Out Some Separators X X O O O O O O X X X X X X O O
21 Lots of Noise X X O O O O O O X X X X X X O O
22 Maximizing the Margin X X O O O O O O X X X X X X O O
23 “Fat” Separators X X O O O O O O X X X X X X O O
24 Support Vector Machine (SVM) Support vectors Maximize margin SVMs maximize the margin around the separating hyperplane. A.k.a. large margin classifiers The decision function is fully specified by a subset of training samples, the support vectors. Quadratic programming problem Seen by many as most successful current text classification method
25 w: decision hyperplane normal x i : data point i y i : class of data point i (+1 or -1) NB: Not 1/0 Classifier is: f(x i ) = sign(w T x i + b) Functional margin of x i is: y i (w T x i + b) But note that we can increase this margin simply by scaling w, b…. Functional margin of dataset is minimum functional margin for any point Maximum Margin: Formalization
26 The planar decision surface in data-space for the simple linear discriminant function:
27 Geometric Margin Distance from example to the separator is Examples closest to the hyperplane are support vectors. Margin ρ of the separator is the width of separation between support vectors of classes. r ρ x x′x′
28 Linear SVM Mathematically Assume that all data is at least distance 1 from the hyperplane, then the following two constraints follow for a training set {(x i,y i )} For support vectors, the inequality becomes an equality Then, since each example’s distance from the hyperplane is The margin is: w T x i + b ≥ 1 if y i = 1 w T x i + b ≤ -1 if y i = -1
29 Linear Support Vector Machine (SVM) Hyperplane w T x + b = 0 Extra scale constraint: min i=1,…,n |w T x i + b| = 1 This implies: w T (x a –x b ) = 2 ρ = ||x a –x b || 2 = 2/||w|| 2 w T x + b = 0 w T x a + b = 1 w T x b + b = -1 ρ
30 Linear SVMs Mathematically (cont.) Then we can formulate the quadratic optimization problem: A better formulation (min ||w|| = max 1/ ||w|| ): Find w and b such that is maximized; and for all { ( x i, y i )} w T x i + b ≥ 1 if y i =1; w T x i + b ≤ -1 if y i = -1 Find w and b such that Φ(w) =½ w T w is minimized; and for all { ( x i,y i )} : y i (w T x i + b) ≥ 1
31 Non-linear SVMs Datasets that are linearly separable (with some noise) work out great: But what are we going to do if the dataset is just too hard? How about … mapping data to a higher-dimensional space: 0 x2x2 x 0 x 0 x
32 Nonlinear SVMs: The Clever Bit! Project the linearly inseparable data to high dimensional space where it is linearly separable and then we can use linear SVM (1,0) (0,0) (0,1) + + -
Not linearly separable data. Need to transform the coordinates: polar coordinates, kernel transformation into higher dimensional space (support vector machines). Distance from center (radius) Angular degree (phase) Linearly separable data. polar coordinates
34 Non-linear SVMs: Feature spaces Φ: x → φ(x)
35 (cont’d) Kernel functions and the kernel trick are used to transform data into a different linearly separable feature space (.) ( ) Feature space Input space
36 Mathematical Details : SKIP
37 Solving the Optimization Problem This is now optimizing a quadratic function subject to linear constraints Quadratic optimization problems are a well-known class of mathematical programming problems, and many (rather intricate) algorithms exist for solving them The solution involves constructing a dual problem where a Lagrange multiplier α i is associated with every constraint in the primary problem: Find w and b such that Φ(w) =½ w T w is minimized; and for all { ( x i,y i )} : y i (w T x i + b) ≥ 1 Find α 1 …α N such that Q( α ) = Σ α i - ½ ΣΣ α i α j y i y j x i T x j is maximized and (1) Σ α i y i = 0 (2) α i ≥ 0 for all α i
38 The Optimization Problem Solution The solution has the form: Each non-zero α i indicates that corresponding x i is a support vector. Then the classifying function will have the form: Notice that it relies on an inner product between the test point x and the support vectors x i. Also keep in mind that solving the optimization problem involved computing the inner products x i T x j between all pairs of training points. w = Σ α i y i x i b= y k - w T x k for any x k such that α k 0 f(x) = Σ α i y i x i T x + b
39 Soft Margin Classification If the training set is not linearly separable, slack variables ξ i can be added to allow misclassification of difficult or noisy examples. Allow some errors Let some points be moved to where they belong, at a cost Still, try to minimize training set errors, and to place hyperplane “far” from each class (large margin) ξjξj ξiξi
40 Soft Margin Classification Mathematically The old formulation: The new formulation incorporating slack variables: Parameter C can be viewed as a way to control overfitting – a regularization term Find w and b such that Φ(w) =½ w T w is minimized and for all { ( x i,y i )} y i (w T x i + b) ≥ 1 Find w and b such that Φ(w) =½ w T w + C Σ ξ i is minimized and for all { ( x i,y i )} y i (w T x i + b) ≥ 1- ξ i and ξ i ≥ 0 for all i
41 Soft Margin Classification – Solution The dual problem for soft margin classification: Neither slack variables ξ i nor their Lagrange multipliers appear in the dual problem! Again, x i with non-zero α i will be support vectors. Solution to the dual problem is: Find α 1 …α N such that Q( α ) = Σ α i - ½ ΣΣ α i α j y i y j x i T x j is maximized and (1) Σ α i y i = 0 (2) 0 ≤ α i ≤ C for all α i w = Σ α i y i x i b= y k (1- ξ k ) - w T x k where k = argmax α k k f(x) = Σ α i y i x i T x + b But w not needed explicitly for classification!
42 Classification with SVMs Given a new point (x 1,x 2 ), we can score its projection onto the hyperplane normal: In 2 dims: score = w 1 x 1 +w 2 x 2 +b. I.e., compute score: wx + b = Σα i y i x i T x + b Set confidence threshold t Score > t: yes Score < -t: no Else: don’t know
43 Linear SVMs: Summary The classifier is a separating hyperplane. Most “important” training points are support vectors; they define the hyperplane. Quadratic optimization algorithms can identify which training points x i are support vectors with non-zero Lagrangian multipliers α i. Both in the dual formulation of the problem and in the solution training points appear only inside inner products: Find α 1 …α N such that Q( α ) = Σ α i - ½ ΣΣ α i α j y i y j x i T x j is maximized and (1) Σ α i y i = 0 (2) 0 ≤ α i ≤ C for all α i f(x) = Σ α i y i x i T x + b
44 Non-linear SVMs: Feature spaces General idea: the original feature space can always be mapped to some higher-dimensional feature space where the training set is separable: Φ: x → φ(x)
45 The “Kernel Trick” The linear classifier relies on an inner product between vectors K(x i,x j )=x i T x j If every datapoint is mapped into high-dimensional space via some transformation Φ: x → φ(x), the inner product becomes: K(x i,x j )= φ(x i ) T φ(x j ) A kernel function is some function that corresponds to an inner product in some expanded feature space. Example: 2-dimensional vectors x=[x 1 x 2 ]; let K(x i,x j )=(1 + x i T x j ) 2, Need to show that K(x i,x j )= φ(x i ) T φ(x j ): K(x i,x j )=(1 + x i T x j ) 2, = 1+ x i1 2 x j x i1 x j1 x i2 x j2 + x i2 2 x j x i1 x j1 + 2x i2 x j2 = = [1 x i1 2 √2 x i1 x i2 x i2 2 √2x i1 √2x i2 ] T [1 x j1 2 √2 x j1 x j2 x j2 2 √2x j1 √2x j2 ] = φ(x i ) T φ(x j ) where φ(x) = [1 x 1 2 √2 x 1 x 2 x 2 2 √2x 1 √2x 2 ]
46 Kernels Why use kernels? Make non-separable problem separable. Map data into better representational space Common kernels Linear Polynomial K(x,z) = (1+x T z) d Radial basis function (infinite dimensional space)