Classification k-nearest neighbor classifier Naïve Bayes Logistic ... · Classification k-nearest...
Transcript of Classification k-nearest neighbor classifier Naïve Bayes Logistic ... · Classification k-nearest...
Classification
k-nearest neighbor classifier
Naïve Bayes
Logistic Regression
Support Vector Machines
Applications of Gradient Descent, Lagrange and KKT are countless I am
sure that each of you will have to use them some day (If you stay in
engineering)
� Gradient descent is a first-order optimization algorithm. To find a local minimum of a function using gradient descent, one takes steps proportional to the negative of the gradient (or of the approximate gradient) of the function at the current point. If instead one takes steps proportional to the positive of the gradient, one approaches a local maximum of that function; the procedure is then known as gradient ascent.
f = function(x){(x[1] - 5)^2 + (x[2] - 6)^2}
initial_x = c(10, 11)
x_optimal = optim(initial_x, f, method="CG")
x_min = x_optimal$par
x_min
� In mathematical optimization, the method of Lagrange multipliers (named after Joseph Louis Lagrange) is a strategy for finding the local maxima and minima of a function subject to equality constraints.
� maximize f(x, y)subject to g(x, y) = c.� We need both f and g to have continuous first partial
derivatives. We introduce a new variable (λ) called a Lagrange multiplier and study the Lagrange function (or Lagrangian) defined by
� where the λ term may be either added or subtracted.
Maximize expected return: f(x1, x2, x3) = x1*5% + x2*4% + x3*6%
Subjected to constraints:10% < x1, x2, x3 < 100%x1 + x2 + x3 = 1x3 < x1 + x2x1 < 2 * x2
library(lpSolve)library(lpSolveAPI)# Set the number of varsmodel <- make.lp(0, 3)# Define the object function: for Minimize, use -veset.objfn(model, c(-0.05, -0.04, -0.06))# Add the constraintsadd.constraint(model, c(1, 1, 1), "=", 1)add.constraint(model, c(1, 1, -1), ">", 0)add.constraint(model, c(1, -2, 0), "<", 0)# Set the upper and lower boundsset.bounds(model, lower=c(0.1, 0.1, 0.1), upper=c(1, 1, 1))# Compute the optimized modelsolve(model)# Get the value of the optimized parametersget.variables(model)# Get the value of the objective functionget.objective(model)# Get the value of the constraintget.constraints(model)
� n mathematical optimization, the Karush–Kuhn–Tucker (KKT) conditions (also known as the Kuhn–Tucker conditions) are first order necessary conditions for a solution in nonlinear programming to be optimal, provided that some regularity conditions are satisfied. Allowing inequality constraints, the KKT approach to nonlinear programming generalizes the method of Lagrange multipliers, which allows only equality constraints. The system of equations corresponding to the KKT conditions is usually not solved directly, except in the few special cases where a closed-form solution can be derived analytically. In general, many optimization algorithms can be interpreted as methods for numerically solving the KKT system of equations.
Minimize quadratic objective function:f(x1, x2) = c1.x12 + c2. x1x2 + c2.x22 - (d1. x1 + d2.x2) Subject to constraintsa11.x1 + a12.x2 == b1a21.x1 + a22.x2 == b2a31.x1 + a32.x2 >= b3a41.x1 + a42.x2 >= b4a51.x1 + a52.x2 >= b5
library(quadprog)mu_return_vector = c(0.05, 0.04, 0.06) sigma = matrix(c(0.01, 0.002, 0.005,
0.002, 0.008, 0.006, 0.005, 0.006, 0.012),
nrow=3, ncol=3)D.Matrix = 2*sigmad.Vector = rep(0, 3)A.Equality = matrix(c(1,1,1), ncol=1)A.Matrix = cbind(A.Equality, mu_return_vector,
diag(3))b.Vector = c(1, 0.052, rep(0, 3))out = solve.QP(Dmat=D.Matrix, dvec=d.Vector,
Amat=A.Matrix, bvec=b.Vector, meq=1)
out$solutionout$value
Apply
Model
Induction
Deduction
Learn
Model
Model
Tid Attrib1 Attrib2 Attrib3 Class
1 Yes Large 125K No
2 No Medium 100K No
3 No Small 70K No
4 Yes Medium 120K No
5 No Large 95K Yes
6 No Medium 60K No
7 Yes Large 220K No
8 No Small 85K Yes
9 No Medium 75K No
10 No Small 90K Yes 10
Tid Attrib1 Attrib2 Attrib3 Class
11 No Small 55K ?
12 Yes Medium 80K ?
13 Yes Large 110K ?
14 No Small 95K ?
15 No Large 67K ? 10
Test Set
Learning
algorithm
Training Set
Atr1 ……... AtrN Class
A
B
B
C
A
C
B
Set of Stored Cases
Atr1 ……... AtrN
Unseen Case
• Store the training records
• Use training records to
predict the class label of
unseen cases
� Examples:
� Rote-learner
▪ Memorizes entire training data and performs
classification only if attributes of record match one of
the training examples exactly
� Nearest neighbor classifier
▪ Uses k “closest” points (nearest neighbors) for
performing classification
� Basic idea:
� “If it walks like a duck, quacks like a duck, then it’s
probably a duck”
Training
Records
Test RecordCompute
Distance
Choose k of the
“nearest” records
� 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
� To classify an unknown record:
1. Compute distance to other
training records
2. Identify k nearest neighbors
3. Use class labels of nearest
neighbors to determine the
class label of unknown record
(e.g., by taking majority vote)
Unknown record
X X X
(a) 1-nearest neighbor (b) 2-nearest neighbor (c) 3-nearest neighbor
K-nearest neighbors of a record x are data points that
have the k smallest distance to x
Voronoi Diagram defines the classification boundary
The area takes the
class of the green
point
� 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/d2
∑ −=i ii
qpqpd2)(),(
� 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
� 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
� Problem with Euclidean measure:
� High dimensional data
▪ curse of dimensionality
� Can produce counter-intuitive results
1 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1
vs
d = 1.4142 d = 1.4142
� Solution: Normalize the vectors to unit length
� k-NN classifiers are lazy learners
� It does not build models explicitly
� Unlike eager learners such as decision trees
� Classifying unknown records are relatively
expensive
� Naïve algorithm: O(n)
� Need for structures to retrieve nearest neighbors
fast.
▪ The Nearest Neighbor Search problem.
� Two-dimensional kd-trees� A data structure for answering nearest neighbor
queries in R2
� kd-tree construction algorithm� Select the x or y dimension (alternating between the
two)
� Partition the space into two with a line passing from the median point
� Repeat recursively in the two partitions as long as there are enough points
2-dimensional kd-trees
2-dimensional kd-trees
2-dimensional kd-trees
2-dimensional kd-trees
2-dimensional kd-trees
2-dimensional kd-trees
region(u) – all the black points in the subtree of u
2-dimensional kd-trees
� A binary tree:
� Size O(n)
� Depth O(logn)
� Construction time O(nlogn)
� Query time: worst case O(n), but for many cases O(logn)
Generalizes to d dimensions
� Example of Binary Space Partitioning
2-dimensional kd-trees
install.packages(c("class"))library(class)train = rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])test = rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])cl = factor(c(rep("s",25), rep("c",25), rep("v",25)))knn(train, test, cl, k = 3, prob=TRUE)attributes(.Last.value)
� Find a linear hyperplane (decision boundary) that will separate the data
� One Possible Solution
� Another possible solution
� Other possible solutions
� Which one is better? B1 or B2?� How do you define better?
� Find hyperplane maximizes the margin => B1 is better than B2
0=+• bxwrr
1−=+• bxwrr 1+=+• bxw
rr
−≤+•−
≥+•=
1bxw if1
1bxw if1)( rr
rrrxf
||||
2Margin
wr=
� We want to maximize:
� Which is equivalent to minimizing:
� But subjected to the following constraints:
▪ This is a constrained optimization problem▪ Numerical approaches to solve it (e.g., quadratic programming)
2||||
2 Margin
wr=
2
||||)(
2w
wL
r
=
� ∙ �� � � � 1 if � 1� ∙ �� � � � �1 if � �1
� What if the problem is not linearly separable?
� What if the problem is not linearly separable?
��
� What if the problem is not linearly separable?
� Introduce slack variables
▪ Need to minimize:
▪ Subject to:
+= ∑
=
N
i
k
iCw
wL1
2
2
||||)( ξ
r
� ∙ �� � � � 1 � � if � 1� ∙ �� � � � �1 � � if � �1
� What if decision boundary is not linear?
� Transform data into higher dimensional
space
Use the Kernel Trick
A kernel is a function k(xi, xj) that given twovectors in input space, returns the dot product oftheir images in feature spaceK(xi. xj)= ϕ(xi). ϕ(xj)So by computing the dot product directly using akernel function, one avoid the mapping ϕ(x).This is desirable because Z has possibly infinitedimensions and ϕ(x) can be tricky orimpossible to compute. Using a kernel function,one need not explicitly know what ϕ(x) is. Byusing a kernel function, a SVM that operates ininfinite dimensional space can be constructed.
# Example 1: XOR Functionx = array(data = c(0,0,1,1,0,1,0,1),dim=c(4,2))y = factor(c(1,-1,-1,1))model = svm(x,y)predict(model,x)
# Example 2: IRIS datasetdata(iris)attach(iris)x = subset(iris, select = -Species)y = Speciesmodel = svm(x, y)print(model)summary(model)pred <- predict(model, x)table(pred, y)
� Instead of predicting the class of an record we
want to predict the probability of the class
given the record
� The problem of predicting continuous values
is called regression problem
� General approach: find a continuous function
that models the continuous points.
� Given a dataset of the form ���, ��, … , ���, �� find
a linear function that given the vector �� predicts the � value as �� ����� Find a vector of weights �
that minimizes the sum of square errors
� �� �� ��
� Several techniques for solving the problem.
� Assume a linear classification boundary
� ⋅ � 0
� ⋅ � � 0
� ⋅ � � 0
For the positive class the bigger the
value of � ⋅ �, the further the point is from the classification boundary, the
higher our certainty for the
membership to the positive class
• Define ����|�� as an increasing
function of � ⋅ �
For the negative class the smaller the
value of � ⋅ �, the further the point is from the classification boundary, the
higher our certainty for the
membership to the negative class
• Define ���!|�� as a decreasing
function of � ⋅ �
" # 11 � $!%
� �� � 11 � $!&⋅'
� �! � $!&⋅'
1 � $!&⋅'
log � �� �� �! � � ⋅ �
Logistic Regression: Find the
vector�that maximizes the probability of the observed data
The logistic function
Linear regression on the log-odds ratio
� Produces a probability estimate for the class
membership which is often very useful.
� The weights can be useful for understanding
the feature importance.
� Works for relatively large datasets
� Fast to apply.
# Example 1: Linear Regressionx = c(1, 2, 3, 4)y = 2*x + 3lm(y~x)
# Example 2: Logistic Regressiondata(iris)iris.sub <- subset(iris, Species%in%c("versicolor","virginica"))model <- glm(Species ~ Sepal.Length + Sepal.Width, data = iris.sub,
family = binomial(link = "logit"))hist(predict(model), type = "response")
� A probabilistic framework for solving classification problems
� A, C random variables� Joint probability: Pr(A=a,C=c)� Conditional probability: Pr(C=c | A=a)� Relationship between joint and conditional
probability distributions
� Bayes Theorem:)(
)()|()|(
AP
CPCAPACP =
)Pr()|Pr()Pr()|Pr(),Pr( CCAAACAC ×=×=
� Consider each attribute and class label as
random variablesTid Refund Marital
Status Taxable Income Evade
1 Yes Single 125K No
2 No Married 100K No
3 No Single 70K No
4 Yes Married 120K No
5 No Divorced 95K Yes
6 No Married 60K No
7 Yes Divorced 220K No
8 No Single 85K Yes
9 No Married 75K No
10 No Single 90K Yes 10
categoric
al
categoric
al
continuous
class
Evade C
Event space: {Yes, No}
P(C) = (0.3, 0.7)
Refund A1
Event space: {Yes, No}
P(A1) = (0.3,0.7)
Martial Status A2
Event space: {Single, Married, Divorced}
P(A2) = (0.4,0.4,0.2)
Taxable Income A3
Event space: R
P(A3) ~ Normal(µ,σ)
� Given a record X over attributes (A1, A2,…,An) � E.g., X = (‘Yes’, ‘Single’, 125K)
� The goal is to predict class C� Specifically, we want to find the value c of C that
maximizes P(C=c| X)▪ Maximum Aposteriori Probability estimate
� Can we estimate P(C| X) directly from data?� This means that we estimate the probability for all
possible values of the class variable.
� Approach:
� compute the posterior probability P(C | A1, A2, …, An) for all values of C using the Bayes theorem
� Choose value of C that maximizes P(C | A1, A2, …, An)
� Equivalent to choosing value of C that maximizesP(A1, A2, …, An|C) P(C)
� How to estimate P(A1, A2, …, An | C )?
)(
)()|()|(
21
21
21
n
n
n
AAAP
CPCAAAPAAACP
K
KK =
� Assume independence among attributes Ai when class is
given:
� ��+�, +�, … , +�|�� ��+�|����+� � ⋯ ��+�|��
� We can estimate P(Ai| C) for all values of Ai and C.
� New point X is classified to class c if
� � - . � � - ∏ ��+�|-��is maximum over all possible values of C.
� Class Prior Probability: � � - 01
0e.g., P(C = No) = 7/10,
P(C = Yes) = 3/10
� For discrete attributes:
� +� 2 � - 34,535
where 34,5 is number of instances having attribute +� 2andbelongs to class -
� Examples:
P(Status=Married|No) = 4/7P(Refund=Yes|Yes)=0
Tid Refund Marital Status
Taxable Income Evade
1 Yes Single 125K No
2 No Married 100K No
3 No Single 70K No
4 Yes Married 120K No
5 No Divorced 95K Yes
6 No Married 60K No
7 Yes Divorced 220K No
8 No Single 85K Yes
9 No Married 75K No
10 No Single 90K Yes 10
categoric
al
categoric
al
continuous
class
� For continuous attributes: � Discretize the range into bins ▪ one ordinal attribute per bin
▪ violates independence assumption
� Two-way split: (A < v) or (A > v)▪ choose only one of the two splits as new attribute
� Probability density estimation:▪ Assume attribute follows a normal distribution
▪ Use data to estimate parameters of distribution (i.e., mean µ and standard deviation σ)
▪ Once probability distribution is known, we can use it to estimate the conditional probability P(Ai|c)
� Normal distribution:
� One for each (ai,ci) pair
� For (Income, Class=No):
� If Class=No
▪ sample mean = 110
▪ sample variance = 2975
Tid Refund Marital Status
Taxable Income Evade
1 Yes Single 125K No
2 No Married 100K No
3 No Single 70K No
4 Yes Married 120K No
5 No Divorced 95K Yes
6 No Married 60K No
7 Yes Divorced 220K No
8 No Single 85K Yes
9 No Married 75K No
10 No Single 90K Yes 10
categoric
al
categoric
al
continuous
class
2
2
2
)(
22
1)|( ij
ija
ij
ji ecaAPσ
µ
πσ
−−
==
0072.0)54.54(2
1)|120( )2975(2
)110120( 2
===−
−
eNoIncomePπ
� Creating a Naïve Bayes Classifier, essentially
means to compute counts:Total number of records: N = 10
Class No:
Number of records: 7
Attribute Refund:
Yes: 3
No: 4
Attribute Marital Status:
Single: 2
Divorced: 1
Married: 4
Attribute Income:
mean: 110
variance: 2975
Class Yes:
Number of records: 3
Attribute Refund:
Yes: 0
No: 3
Attribute Marital Status:
Single: 2
Divorced: 1
Married: 0
Attribute Income:
mean: 90
variance: 25
120K)IncomeMarried,No,Refund( ===X
� P(X|Class=No) = P(Refund=No|Class=No)
× P(Married| Class=No)
× P(Income=120K| Class=No)
= 4/7 × 4/7 × 0.0072 = 0.0024
� P(X|Class=Yes) = P(Refund=No| Class=Yes)
× P(Married| Class=Yes)
× P(Income=120K| Class=Yes)
= 1 × 0 × 1.2 × 10-9 = 0
P(No) = 0.3, P(Yes) = 0.7
Since P(X|No)P(No) > P(X|Yes)P(Yes)
Therefore P(No|X) > P(Yes|X)
=> Class = No
Given a Test Record:
� If one of the conditional probability is zero,
then the entire expression becomes zero
� Probability estimation:
mN
mpNcCaAP
NN
NcCaAP
N
NcCaAP
c
aci
ic
aci
c
aci
+
+===
+
+===
===
)|(:estimate-m
1)|(:Laplace
)|( :OriginalNi: number of attribute
values for attribute Ai
p: prior probability
m: parameter
120K)IncomeMarried,No,Refund( ===X
� P(X|Class=No) = P(Refund=No|Class=No)
× P(Married| Class=No)
× P(Income=120K| Class=No)
= 5/9 × 5/10 × 0.0072
� P(X|Class=Yes) = P(Refund=No| Class=Yes)
× P(Married| Class=Yes)
× P(Income=120K| Class=Yes)
= 4/5 × 1/6 × 1.2 × 10-9
P(No) = 0.7, P(Yes) = 0.3
Since P(X|No)P(No) > P(X|Yes)P(Yes)
Therefore P(No|X) > P(Yes|X)
=> Class = No
Given a Test Record: With Laplace Smoothing
� Computing the conditional probabilities involves multiplication of many very small numbers � Numbers get very close to zero, and there is a danger
of numeric instability
� We can deal with this by computing the logarithm of the conditional probability
log � � + ~ log � + � � log � + � log +� � � log ��+�
�
� Naïve Bayes is commonly used for text classification
� For a document 7 �#�, … , #8�� - 7 ��-� 9 ��#�|-�
%:∈<� � #� - = Fraction of terms from all documents in
c that are#�.
� Easy to implement and works relatively well� Limitation: Hard to incorporate additional
features (beyond words).
� Robust to isolated noise points
� Handle missing values by ignoring the instance during probability estimate calculations
� Robust to irrelevant attributes
� Independence assumption may not hold for some attributes� Use other techniques such as Bayesian Belief Networks
(BBN)
� Naïve Bayes can produce a probability estimate, but it is usually a very biased one� Logistic Regression is better for obtaining probabilities.
install.packages(e1071)
library(e1071)
classifier<-naiveBayes(iris[,1:4], iris[,5])
table(predict(classifier, iris[,-5]), iris[,5],
dnn=list('predicted','actual'))
classifier$apriori
classifier$tables$Petal.Length
� Naïve Bayes is a type of a generative model� Generative process: ▪ First pick the category of the record
▪ Then given the category, generate the attribute values from the distribution of the category
▪ Conditional independence given C
� We use the training data to learn the distribution of the values in a class
C
+� +� +�
� Logistic Regression and SVM are discriminative models� The goal is to find the boundary that discriminates
between the two classes from the training data
� In order to classify the language of a document, you can � Either learn the two languages and find which is
more likely to have generated the words you see
� Or learn what differentiates the two languages.