0% found this document useful (0 votes)
5 views27 pages

Classification

The document discusses the differences between supervised and unsupervised learning, highlighting the role of labeled data in classification and the absence of labels in clustering. It outlines the two-step process of classification involving model construction and model usage, along with issues related to data preparation and evaluation of classification methods. Additionally, it covers decision tree induction, attribute selection measures like information gain and Gini index, and the principles of Bayesian classification.

Uploaded by

try.admerch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Classification

The document discusses the differences between supervised and unsupervised learning, highlighting the role of labeled data in classification and the absence of labels in clustering. It outlines the two-step process of classification involving model construction and model usage, along with issues related to data preparation and evaluation of classification methods. Additionally, it covers decision tree induction, attribute selection measures like information gain and Gini index, and the principles of Bayesian classification.

Uploaded by

try.admerch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

09-10-2024

CLASSIFICATION

Supervised vs. Unsupervised Learning


Supervised learning (classification)
◦ Supervision: The training data (observations, measurements, etc.) are accompanied by labels
indicating the class of the observations
◦ New data is classified based on the training set

Unsupervised learning (clustering)


◦ The class labels of training data is unknown
◦ Given a set of measurements, observations, etc. with the aim of establishing the existence of
classes or clusters in the data

1
09-10-2024

Classification vs. Prediction


Classification
◦ predicts categorical class labels (discrete or nominal)
◦ classifies data (constructs a model) based on the training set and the values (class labels) in a classifying
attribute and uses it in classifying new data
Prediction
◦ models continuous-valued functions, i.e., predicts unknown or missing values
Typical applications
◦ Credit approval
◦ Target marketing
◦ Medical diagnosis
◦ Fraud detection

Classification—A Two-Step Process

Model construction: describing a set of predetermined classes


◦ Each tuple/sample is assumed to belong to a predefined class, as determined by the class label attribute
◦ The set of tuples used for model construction is training set
◦ The model is represented as classification rules, decision trees, or mathematical formulae

Model usage: for classifying future or unknown objects


◦ Estimate accuracy of the model
◦ The known label of test sample is compared with the classified result from the model
◦ Accuracy rate is the percentage of test set samples that are correctly classified by the model
◦ Test set is independent of training set, otherwise over-fitting will occur
◦ If the accuracy is acceptable, use the model to classify data tuples whose class labels are not known

2
09-10-2024

Process (1): Model Construction


Classification
Algorithms
Training
Data

NAME RANK YEARS TENURED Classifier


M ik e A ssistan t P ro f 3 no (Model)
M ary A ssistan t P ro f 7 yes
B ill P ro fesso r 2 yes
J im A sso c iate P ro f 7 yes
D ave A ssistan t P ro f 6 no IF rank = ‘professor’
Anne A sso c iate P ro f 3 no
OR years > 6
THEN tenured = ‘yes’
5

Process (2): Using the Model in Prediction

Classifier

Testing
Data Unseen Data

(Jeff, Professor, 4)
NAME RANK YEARS TENURED
Tom A ssistan t P ro f 2 no Tenured?
M erlisa A sso c iate P ro f 7 no
G eo rg e P ro fesso r 5 yes
J o sep h A ssistan t P ro f 7 yes
6

3
09-10-2024

Issues: Data Preparation

Data cleaning
◦ Preprocess data in order to reduce noise and handle missing values

Relevance analysis (feature selection)


◦ Remove the irrelevant or redundant attributes

Data transformation
◦ Generalize and/or normalize data

Issues: Evaluating Classification Methods


Accuracy
◦ classifier accuracy: predicting class label

◦ predictor accuracy: guessing value of predicted attributes

Speed
◦ time to construct the model (training time)

◦ time to use the model (classification/prediction time)

Robustness: handling noise and missing values

Scalability: efficiency in disk-resident databases

Interpretability
◦ understanding and insight provided by the model

Other measures, e.g., goodness of rules, such as decision tree size or compactness of classification rules

4
09-10-2024

Decision Tree Induction: Training Dataset

age income student credit_rating buys_computer


<=30 high no fair no
<=30 high no excellent no
31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
>40 low yes excellent no
31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no
9

Output: A Decision Tree for “buys_computer”

age?

<=30 31..40 >40

student? yes credit rating?


no yes excellent fair
no yes yes

10

10

5
09-10-2024

Algorithm for Decision Tree Induction


Basic algorithm (a greedy algorithm)
◦ Tree is constructed in a top-down recursive divide-and-conquer manner
◦ At start, all the training examples are at the root
◦ Attributes are categorical (if continuous-valued, they are discretized in advance)
◦ Examples are partitioned recursively based on selected attributes
◦ Test attributes are selected on the basis of a heuristic or statistical measure (e.g., information gain)

Conditions for stopping partitioning


◦ All samples for a given node belong to the same class
◦ There are no remaining attributes for further partitioning – majority voting is employed for classifying the leaf
◦ There are no samples left

11

11

12

12

6
09-10-2024

Attribute Selection Measure: Information Gain


(ID3/C4.5)
◼ Select the attribute with the highest information gain
◼ Let pi be the probability that an arbitrary tuple in D belongs to class Ci, estimated by |Ci, D|/|D|
◼ Expected information (entropy) needed to classify a tuple in D:
m
Info( D) = − pi log 2 ( pi )
i =1

◼ Information needed (after using A to split D into v partitions) to classify D:

◼ Information gained by branching on attribute A

Gain(A) = Info(D) − InfoA(D)

13

13

Attribute Selection: Information Gain


Class P: buys_computer
= “yes”
Class N: buys_computer
= “no”
age income student credit_rating buys_computer
<=30 high no fair no
<=30 high no excellent no
31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
>40 low yes excellent no
31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no
m
Info( D) = − pi log 2 ( pi )
i =1

14

14

7
09-10-2024

Attribute Selection: Information Gain


age income student credit_rating buys_computer
<=30 high no fair no 5 4
<=30 high no excellent no Infoage ( D ) = I ( 2,3) + I ( 4,0)
31…40 high no fair yes 14 14
>40 medium no fair yes
5
>40
>40
low
low
yes
yes
fair
excellent
yes
no
+ I (3,2) = 0.694
31…40 low yes excellent yes
14
<=30 medium no fair no
<=30 low yes fair yes 5
>40 medium yes fair yes
I ( 2,3) means “age <=30” has 5 out of 14 samples,
<=30 medium yes excellent yes
14
31…40 medium no excellent yes
with 2 yes’es and 3 no’s. Hence
31…40 high yes fair yes
>40 medium no excellent no

age pi ni I(pi, ni)


<=30 2 3 0.971
31…40 4 0 0
>40 3 2 0.971

Gain ( age) = Info( D ) − Infoage ( D ) = 0.246


15

15

Attribute Selection: Information Gain


age income student credit_rating buys_computer
<=30 high no fair no
<=30
31…40
high
high
no
no
excellent
fair
no
yes
Similarly, find
>40 medium no fair yes
>40 low yes fair yes
>40
31…40
low
low
yes
yes
excellent
excellent
no
yes
Gain(income)
<=30
<=30
medium
low
no
yes
fair
fair
no
yes Gain(student)
>40
<=30
medium
medium
yes
yes
fair
excellent
yes
yes Gain(credit_rating)
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no
Gain(income) = 0.029
Gain( student) = 0.151
Gain(credit _ rating ) = 0.048

Since age has highest information gain, it is selected as a splitting attribute.

16

16

8
09-10-2024

age income student credit_rating buys_computer


<=30 high no fair no
<=30 high no excellent no
31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
>40 low yes excellent no
31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no

17

17

18

9
09-10-2024

19

19

20

10
09-10-2024

21

Gain Ratio for Attribute Selection (C4.5)


Information gain measure is biased towards attributes with a large number of values

C4.5 (a successor of ID3) uses gain ratio to overcome the problem (normalization to information gain)
v | Dj | | Dj |
SplitInfoA ( D) = −  log 2 ( )
j =1 |D| |D|

◦ GainRatio(A) = Gain(A)/SplitInfo(A)

The attribute with the maximum gain ratio is selected as the splitting attribute

4 4 6 6 4 4
𝑆𝑝𝑙𝑖𝑡𝐼𝑛𝑓𝑜𝐴 (𝐷) = − × log 2( ) − × log 2( ) − × log 2( ) =1.557
14 14 14 14 14 14

gain_ratio(income) = 0.029/1.557 = 0.019

26

26

11
09-10-2024

Gini index (CART, IBM IntelligentMiner)


If a data set D contains examples from n classes, gini index, gini(D) is defined as
𝑚

𝑔𝑖𝑛𝑖 𝐷 = 1 − ෍ 𝑝𝑖 2
𝑖=1
where pj is the probability that a tuple in D belongs to class Ci and is estimated by |Ci, D|/|D|. The sum is
computed over m classes.
If a data set D is split on A into two subsets D1 and D2, the gini index gini(D) is defined as
| D1| | D2 |
gini A ( D) = gini( D1) + gini( D 2)
| D| | D|

gini( A) = gini(D) − giniA(D)


Reduction in Impurity:

The attribute provides the smallest ginisplit(D) (or the largest reduction in impurity) is chosen to split the node (need
to enumerate all the possible splitting points for each attribute)
27

27

Gini index (CART, IBM IntelligentMiner)


Ex. D has 9 tuples in buys_computer = “yes” and 5 in “no”
2 2
9 5
gini( D) = 1 −   −   = 0.459
 14   14 
Suppose the attribute income partitions D into 10 in D1: {low, medium} and 4 in D2

Similarly, the Gini index values for splits on the remaining subsets are 0.458 (for the subsets {low, high} and {medium} and 0.450 (for the subsets {medium,
high} and {low}.
Therefore, the best binary split for attribute income is on {low, medium} {high} because it minimizes the Gini index.

28

28

12
09-10-2024

Comparing Attribute Selection Measures


The three measures, in general, return good results but
◦ Information gain:
◦ biased towards multivalued attributes
◦ Gain ratio:
◦ tends to prefer unbalanced splits in which one partition is much smaller than the others
◦ Gini index:
◦ biased to multivalued attributes
◦ has difficulty when # of classes is large
◦ tends to favor tests that result in equal-sized partitions and purity in both partitions

29

29

Bayesian Classification: Why?


A statistical classifier: performs probabilistic prediction, i.e., predicts class membership probabilities

Foundation: Based on Bayes’ Theorem.

Performance: A simple Bayesian classifier, naïve Bayesian classifier, has comparable performance with decision

tree and selected neural network classifiers

Incremental: Each training example can incrementally increase/decrease the probability that a hypothesis is

correct — prior knowledge can be combined with observed data

Standard: Even when Bayesian methods are computationally intractable, they can provide a standard of optimal

decision making against which other methods can be measured

37

13
09-10-2024

38

Bayesian Theorem
Given training data X, posteriori probability of a hypothesis H, P(H|X), follows the Bayes theorem

P(H | X) = P(X | H )P(H )


P(X)
Informally, this can be written as
posteriori = likelihood x prior/evidence

Predicts X belongs to C2 iff the probability P(Ci|X) is the highest among all the P(Ck|X) for all the k classes

40

14
09-10-2024

Towards Naïve Bayesian Classifier


Let D be a training set of tuples and their associated class labels, and each tuple is represented by an n-D attribute vector X =
(x1, x2, …, xn)
Suppose there are m classes C1, C2, …, Cm.
Classification is to derive the maximum posteriori, i.e., the maximal P(Ci|X)
This can be derived from Bayes’ theorem

P(X | C ) P(C )
P(C | X) = i i
i P(X)
where
P( X ) =  P(X | C ) P(C )
i i

Since P(X) is constant for all classes, only needs to be maximized P(C | X) = P(X | C )P(C )
i i i

41

Naïve Bayesian Classifier: Training Dataset


Class: age income studentcredit_rating
buys_computer
C1:buys_computer = ‘yes’ <=30 high no fair no
C2:buys_computer = ‘no’ <=30 high no excellent no
31…40 high no fair yes
Data sample >40 medium no fair yes
X = (age <=30, Income = medium, >40 low yes fair yes
Student = yes, Credit_rating = Fair) >40 low yes excellent no
31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no

43

15
09-10-2024

Naïve Bayesian Classifier: An Example


Compute likelihood P(X|Ci) of each attribute value for each class
P(Ci) – Prior probability of each class : age income studentcredit_rating
buys_computer
<=30 high no fair no
P(buys_computer = “yes”) = 9/14 = 0.643 <=30 high no excellent no
P(buys_computer = “no”) = 5/14= 0.357 31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
P(age = “<=30” | buys_computer = “yes”) =
P(age = “<= 30” | buys_computer = “no”) = >40 low yes excellent no
31…40 low yes excellent yes
P(income = “medium” | buys_computer = “yes”) = <=30 medium no fair no
P(income = “medium” | buys_computer = “no”) = <=30 low yes fair yes
>40 medium yes fair yes
P(student = “yes” | buys_computer = “yes) = <=30 medium yes excellent yes
P(student = “yes” | buys_computer = “no”) = 31…40 medium no excellent yes
31…40 high yes fair yes
P(credit_rating = “fair” | buys_computer = “yes”) =
P(credit_rating = “fair” | buys_computer = “no”) = >40 medium no excellent no

44

Naïve Bayesian Classifier: An Example


For the unseen data, classify using bayes classifier

X = (age <= 30 , income = medium, student = yes, credit_rating = fair)

P(X|Ci) : P(X|buys_computer = “yes”) = 0.222 x 0.444 x 0.667 x 0.667 = 0.044


P(X|buys_computer = “no”) = 0.6 x 0.4 x 0.2 x 0.4 = 0.019

P(Ci |X) = (P(X|Ci)*P(Ci)


P(buys_computer = yes |X) = P(X|buys_computer = “yes”) * P(buys_computer = “yes”)
=
P(buys_computer = no |X) = P(X|buys_computer = “no”) * P(buys_computer = “no”)
=
P(buys_computer = yes |X) > P(buys_computer = no |X)
Therefore, X belongs to class (“buys_computer = yes”)

45

16
09-10-2024

46

Another method for Bayes

X = <Adam, M, 1.95m>

Prior Probabilities: P(short) = 4/15 = 0.267; P(medium) = 8/15 = 0.533; P(tall) = 3/15 = 0.2

Since height is continuous data -> split it into ranges


Height Range: (0,1.6] , (1.6,1.7] , (1.7, 1.8] , (1.8, 1.9] , (1.9, 2.0] , (2.0, ∞)

Posteriori probability of X conditioned on C


P(X|short) = 1/4 x 0 = 0
P(X|medium) = 2/8 x 1/8 = 0.031
P(X|tall) = 3/3 x 1/3 = 0.333

50

17
09-10-2024

Another method for Bayes

X = <Adam, M, 1.95m>

Prior Probabilities: P(short) = 4/15 = 0.267; P(medium) = 8/15 = 0.533; P(tall) = 3/15 = 0.2

Since height is continuous data -> split it into ranges


Height Range: (0,1.6] , (1.6,1.7] , (1.7, 1.8] , (1.8, 1.9] , (1.9, 2.0] , (2.0, ∞)

Posteriori probability of X conditioned on C


P(X|short) = 1/4 x 0 = 0
P(X|medium) = 2/8 x 1/8 = 0.031
P(X|tall) = 3/3 x 1/3 = 0.333

Posteriori probability of C conditioned on X


P(short|X) = 0 x 0.267 = 0
P(medium|X) = 0.031 x 0.533 = 0.016
P(tall|X) = 0.333 x 0.2 = 0.066

Since the probability of Tall is highest we classify X as Tall.

51

Naïve Bayesian Classifier: Comments


Advantages
◦ Easy to implement
◦ Good results obtained in most of the cases
Disadvantages
◦ Assumption: class conditional independence, therefore loss of accuracy
◦ Practically, dependencies exist among variables
◦ E.g., hospitals: patients: Profile: age, family history, etc.
Symptoms: fever, cough etc., Disease: lung cancer, diabetes, etc.
◦ Dependencies among these cannot be modeled by Naïve Bayesian Classifier
How to deal with these dependencies?
◦ Bayesian Belief Networks

55

18
09-10-2024

What Is Prediction?
(Numerical) prediction is similar to classification
◦ construct a model
◦ use model to predict continuous or ordered value for a given input
Prediction is different from classification
◦ Classification refers to predict categorical class label
◦ Prediction models continuous-valued functions
Major method for prediction: regression
◦ model the relationship between one or more independent or predictor variables and a dependent or response
variable
Regression analysis
◦ Linear and multiple regression
◦ Non-linear regression
◦ Other regression methods: generalized linear model, Poisson regression, log-linear models, regression trees

56

56

Linear Regression
Linear regression: involves a response variable y and a single predictor variable x
y = w0 + w1 x
where w0 (y-intercept) and w1 (slope) are regression coefficients

Method of least squares estimates the best-fitting straight line


| D|

 (x − x )( yi − y )
w = w = y −w x
i
i =1

1 | D|
0 1
 ( xi − x ) 2
i =1

Multiple linear regression involves more than one predictor variable


◦ Training data is of the form (X1, y1), (X2, y2),…, (X|D|, y|D|)
◦ Ex. For 2-D data, we may have: y = w0 + w1 x1+ w2 x2
◦ Solvable by extension of least square method or using SAS, S-Plus
◦ Many nonlinear functions can be transformed into the above

57

57

19
09-10-2024

Linear Regression Poor Fit

61
61

Regression
Assume data fits a predefined function.
Determine best values for regression coefficients c0,c1,…,cn.
Assume an error: y = c0+c1x1+…+cnxn+
To minimize the error, least mean squared error is used for training set:

Taking derivatives w.r.t c0 and c1, and setting it equal to zero, we can obtain least squares estimates for the
coefficients

62

62

20
09-10-2024

Classification Using Regression

Division: Use regression function to divide area into regions.


Prediction: Use regression function to predict a class membership function. Input
includes desired class.

63
63

Division
Classifying a person as short and medium based on his/her height
Height: {1.6, 1.9, 1.88, 1.7, 1.85, 1.6, 1.7, 1.8, 1.95, 1.9, 1.8, 1.75}

64

64

21
09-10-2024

Prediction
{ (1.6, 0), ( 1.9, 1), ( 1.88, 1), ( 1.7, 0), (1.85, 1), (1.6, 0), (1.7, 0), (1.8, 1), (1.95, 1), (1.9, 1), (1.8, 1), (1.75, 1) } .

65

65

66

66

22
09-10-2024

Metrics for evaluating classifier


Training data
Testing data
Positive tuples – tuples of main class of interest
Negative tuples – other tuples
True Positives (TP)
True Negatives (TN)
False Positives (FP)
False Negatives (FN)

70

70

Confusion matrix
Tool for analysing how well your classifier can recognize tuples of different classes
TP & TN – indicates when classifier is getting things right
FP & FN – indicates when classifier is getting things wrong

Predicted class
Yes No Total
Actual Yes True positive (TP) False negative (FN) P
class

No False positive (FP) True negative (TN) N

Total P’ N’ P+N

Given m classes (m ≥ 2), confusion matrix is m x m table


Entry CMi,j no. of tuples of class i labeled as class j
Ideal confusion matrix – diagonal entries have more values and others are zero or close to zero

71

71

23
09-10-2024

Confusion matrix

Predicted class
Yes No Total
Actual Yes True positive (TP) False negative (FN) P
class

No False positive (FP) True negative (TN) N

Total P’ N’ P+N

P = positive class tuples (TP + FN)


N = negative class tuples (FP + TN)
P’= tuples classified as positive (TP + FP)
N’= tuples classified as negative (FN + TN)
Total = TP+TN+FP+FN = P+N = P’+ N’

72

72

Alternative Evaluation Measures


Sensitivity = TP/P
◦ true positive (recognition) rate
◦ Proportion of positive tuples that are correctly identified
Specificity = TN/N
◦ true negative rate
◦ Proportion of negative tuples that are correctly identified
accuracy = sensitivity * P/(P + N) + specificity * N/(P + N)
Precision = TP/(TP + FP)
◦ Measure of exactness
◦ What percentage of tuples labeled as positive are actually positive
Recall = TP / (TP + FN)
◦ Measure of completeness
◦ What percentage of positive tuples are labeled as positive

73

73

24
09-10-2024

classes buy_computer = yes buy_computer = no total recognition(%)


buy_computer = yes 6954 46 7000 99.34
buy_computer = no 412 2588 3000 86.27
total 7366 2634 10000 95.42

Accuracy = 6954+2588/10000 = 95.42


Sensitivity = 6954/7000 = 99.34
Specificity = 2588/3000 = 86.27
Precision = 6954/7366 = 94.40
Recall = 6954/7000 = 99.34

74

74

Evaluation Measures

75

75

25
09-10-2024

Alternative Evaluation Measures

76

76

Other aspects to compare classifiers


Speed – computational costs involved in generating and using the given classifier

Robustness – ability of classifier to make correct predictions give noisy data or data with missing values

Scalability – ability to construct the classifier efficiently given large amount of data

Interpretability – level of understanding and insight provided by classifier

◦ E.g. decision tree and classification rules are easy to interpret

77

77

26
09-10-2024

Evaluating the Accuracy of a Classifier or Predictor


Holdout method
◦ Given data is randomly partitioned into two independent sets
◦ Training set (e.g., 2/3) for model construction
◦ Test set (e.g., 1/3) for accuracy estimation

Random subsampling: a variation of holdout


◦ Repeat holdout k times,
◦ accuracy = avg. of the accuracies obtained from each iteration

78

78

27

You might also like