Mining di dati web

48
Mining di dati web Classificazioni di Documenti Web Fondamenti di Classificazione e Naive Bayes A.A 2006/2007

description

Mining di dati web. Classificazioni di Documenti Web Fondamenti di Classificazione e Naive Bayes A.A 2006/2007. Categorization / Classification. Given: A description of an instance, x  X , where X is the instance language or instance space . E.g: how to represent text documents. - PowerPoint PPT Presentation

Transcript of Mining di dati web

Page 1: Mining di dati web

Mining di dati webClassificazioni di Documenti Web

Fondamenti di Classificazione e Naive Bayes

A.A 2006/2007

Page 2: Mining di dati web

Categorization / Classification

Given:A description of an instance, xX, where X is the instance language or instance space.E.g: how to represent text documents.

A fixed set of categories C = {c1, c2,…, cn}Determine:

The category of x: c(x)C, where c(x) is a categorization function whose domain is X and whose range is C.

Page 3: Mining di dati web

Classification Task

Input: a training set of tuples, each labeled with one class label

Output: a model (classifier) that assigns a class label to each tuple based on the other attributes

The model can be used to predict the class of new tuples, for which the class label is missing or unknown

Page 4: Mining di dati web

What is Classification

Data classification is a two-step process first step: a model is built describing a predetermined set of data classes or concepts.

second step: the model is used for classification.

Each tuple is assumed to belong to a predefined class, as determined by one of the attributes, called the class label attribute.

Data tuples are also referred to as samples, examples, or objects.

Page 5: Mining di dati web

Train and testThe tuples (examples, samples) are divided into training set + test set

Classification model is built in two steps:training - build the model from the training set

test - check the accuracy of the model using test set

Page 6: Mining di dati web

Train and testKind of models:

if - then ruleslogical formulaedecision trees

Accuracy of models:the known class of test samples is matched against the class predicted by the model

accuracy rate = % of test set samples correctly classified by the model

Page 7: Mining di dati web

Training step

Age Car Type Risk20 Combi High18 Sports High40 Sports High50 Family Low35 Minivan Low30 Combi High32 Family Low40 Combi Low

trainingdata

Classification algorithm

Classifier(model)

if age < 31or Car Type =Sportsthen Risk = High

Page 8: Mining di dati web

Test Step

Age Car Type Risk27 Sports High34 Family Low66 Family High44 Sports High

testdata

Classifier(model)

RiskHighLowLowHigh

Page 9: Mining di dati web

Classification (Prediction)

Age Car Type Risk27 Sports 34 Minivan 55 Family 34 Sports

newdata

Classifier(model)

RiskHighLowLowHigh

Page 10: Mining di dati web

Classification vs. Prediction

There are two forms of data analysis that can be used to extract models describing data classes or to predict future data trends:classification: predict categorical labels

prediction: models continuous-valued functions

Page 11: Mining di dati web

Comparing Classification

Methods (1) Predictive accuracy: this refers to the ability of the model to correctly predict the class label of new or previously unseen data

Speed: this refers to the computation costs involved in generating and using the model

Robustness: this is the ability of the model to make correct predictions given noisy data or data with missing values

Page 12: Mining di dati web

Comparing Classification

Methods (2) Scalability: this refers to the ability to construct the model efficiently given large amount of data

Interpretability: this refers to the level of understanding and insight that is provided by the model

Simplicity: decision tree size rule compactness

Domain-dependent quality indicators

Page 13: Mining di dati web

Problem Formulation

Given records in a database with a class label - find a model for each class.

Age Car Type Risk20 Combi High18 Sports High40 Sports High50 Family Low35 Minivan Low30 Combi High32 Family Low40 Combi Low

Age < 31

High

Car Type is sports

High Low

Page 14: Mining di dati web

Classifier Accuracy

The accuracy of a classifier on a given test set of samples is defined as the percentage of test samples correctly classified by the classifier, and it measures the overall performance of the classifier.

Note that the accuracy of the classifier is not estimated on the training dataset, since it would not be a good indicator of the future accuracy on new data.

The reason is that the classifier generated from the training dataset tends to overfit the training data, and any estimate of the classifier's accuracy based on that data will be overoptimistic.

Page 15: Mining di dati web

Classifier Accuracy

In other words, the classifier is more accurate on the data that was used to train the classifier, but very likely it will be less accurate on independent set of data.

To predict the accuracy of the classifier on new data, we need to asses its accuracy on an independent dataset that played no part in the formation of the classifier.

This dataset is called the test set It is important to note that the test dataset

should not to be used in any way to built the classifier.

Page 16: Mining di dati web

Classifier Accuracy

There are several methods for estimating classifier accuracy. The choice of a method depends on the amount of sample data available for training and testing.

If there are a lot of sample data, then the following simple holdout method is usually applied.

The given set of samples is randomly partitioned into two independent sets, a training set and a test set (typically, 70% of the data is used for training, and the remaining 30% is used for testing)

Provided that both sets of samples are representative, the accuracy of the classifier on the test set will give a good indication of accuracy on new data.

Page 17: Mining di dati web

Classifier Accuracy

In general, it is difficult to say whether a given set of samples is representative or not, but at least we may ensure that the random sampling of the data set is done in such a way that the class distribution of samples in both training and test set is approximately the same as that in the initial data set.

This procedure is called stratification

Page 18: Mining di dati web

Testing - Large Datasets

Available examples

Training Set

70% Divide randomly

Test Set

30%

used to develop one model check accuracy

Page 19: Mining di dati web

Classifier Accuracy

If the amount of data for training and testing is limited, the problem is how to use this limited amount of data for training to get a good classifier and for testing to obtain a correct estimation of the classifier accuracy?

The standard and very common technique of measuring the accuracy of a classifier when the amount of data is limited is k-fold cross-validation

In k-fold cross-validation, the initial set of samples is randomly partitioned into k approximately equal mutually exclusive subsets, called folds, S_1, S_2, ..., S_k.

Page 20: Mining di dati web

Classifier Accuracy

Training and testing is performed k times. At each iteration, one fold is used for testing while remainder k-1 folds are used for training. So, at the end, each fold has been used exactly once for testing and k-1 for training.

The accuracy estimate is the overall number of correct classifications from k iterations divided by the total number of samples N in the initial dataset.

Often, the k-fold cross-validation technique is combined with stratification and is called stratified k-fold cross-validation.

Page 21: Mining di dati web

Testing - Small Datasets

Available examples

Training Set

90%

Test Set

10%

used to develop 10 different models check accuracy

* cross-validationRepeat 10 times

Page 22: Mining di dati web

Classifier Accuracy There are many other methods of estimating

classifier accuracy on a particular dataset Two popular methods are leave-one-out cross-

validation and the bootstrapping Leave-one-out cross-validation is simply N-fold

cross-validation, where N is the number of samples in the initial dataset

At each iteration, a single sample from the dataset is left out for testing, and remaining samples are used for training. The result of testing is either success or failure.

The results of all N evaluations, one for each sample from the dataset, are averaged, and that average represents the final accuracy estimate.

Page 23: Mining di dati web

Classifier Accuracy Bootstrapping is based on the sampling with

replacement The initial dataset is sampled N times, where N is

the total number of samples in the dataset, with replacement, to form another set of N samples for training.

Since some samples in this new "set" will be repeated, so it means that some samples from the initial dataset will not appear in this training set. These samples will form a test set.

Both mentioned estimation methods are interesting especially for estimating classifier accuracy for small datasets. In practice, the standard and most popular technique of estimating a classifier accuracy is stratified tenfold cross-validation

Page 24: Mining di dati web

Text Classification

This concerns you as a patient.

Our medical records indicate you have had a history of illness. We are now encouraging all our patients to use this highly effective and safe solution.

Proven worldwide, feel free to read the many reports on our site from the BBC & ABC News.

We highly recommend you try this Anti-Microbial Peptide as soon as possible since its world supply is limited. The results will show quickly.

Regards, http://www.superbiograde.us/bkhog/ 85% of all email!!

Page 25: Mining di dati web

Examples of Text Categorization

LABELS=BINARY “spam” / “not spam”

LABELS=TOPICS “finance” / “sports” / “asia”

LABELS=OPINION “like” / “hate” / “neutral”

LABELS=AUTHOR “Shakespeare” / “Marlowe” / “Ben Jonson” The Federalist papers

Page 26: Mining di dati web

Methods (1)

Manual classification Used by Yahoo!, Looksmart, about.com, ODP, Medline

very accurate when job is done by experts consistent when the problem size and team is small

difficult and expensive to scaleAutomatic document classification

Hand-coded rule-based systemsReuters, CIA, Verity, …Commercial systems have complex query languages (everything in IR query languages + accumulators)

Page 27: Mining di dati web

Methods (2)

Supervised learning of document-label assignment function: Autonomy, Kana, MSN, Verity, …

Naive Bayes (simple, common method) k-Nearest Neighbors (simple, powerful)Support-vector machines (new, more powerful)… plus many other methodsNo free lunch: requires hand-classified training data

But can be built (and refined) by amateurs

Page 28: Mining di dati web

Bayesian Methods Learning and classification methods based on probability theory

Bayes theorem plays a critical role Build a generative model that approximates how data is produced

Uses prior probability of each category given no information about an item.

Categorization produces a posterior probability distribution over the possible categories given a description of an item.

Page 29: Mining di dati web

Bayes’ Rule

P(C, X) = P(C | X)P(X) = P(X | C)P(C)

P(C | X) = P(X | C)P(C)P(X)

Page 30: Mining di dati web

Maximum a posteriori Hypothesis

hMAP ≡ argmaxh∈H

P(h | D)

hMAP = argmaxh∈H

P(D | h)P(h)P(D)

hMAP = argmaxh∈H

P(D | h)P(h)

Page 31: Mining di dati web

Maximum likelihood Hypothesis

If all hypotheses are a priori equally likely, we only

need to consider the P(D|h) term:

hML ≡ argmaxh∈H

P(D | h)

Page 32: Mining di dati web

Naive Bayes Classifiers

Task: Classify a new instance based on a tuple of attribute values

x1,x2,K ,xn

cMAP = argmaxc j ∈C

P(c j | x1,x2,K ,xn )

cMAP = argmaxc j ∈C

P(x1, x2,K ,xn | c j )P(c j )P(x1,x2,K , xn )

cMAP = argmaxc j ∈C

P(x1,x2,K ,xn | c j )P(c j )

Page 33: Mining di dati web

Naïve Bayes Classifier: Assumptions

P(cj) Can be estimated from the frequency of classes in the training examples.

P(x1,x2,…,xn|cj) Need very, very large number of training examples

Conditional Independence Assumption:

Assume that the probability of observing the conjunction of attributes is equal to the product of the individual probabilities.

Page 34: Mining di dati web

Flu

X1 X2 X5X3 X4feversinus coughrunnynose muscle-ache

The Naïve Bayes Classifier

Conditional Independence Assumption: features are independent of each other given the class:

P(X1,K , X5 | C) = P(X1 | C) • P(X2 | C) • L • P(X5 | C)

Page 35: Mining di dati web

Learning the Model

Common practice:maximum likelihoodsimply use the frequencies in the data

ˆ P (x i | c j ) =N(X i = x i,C = c j )

N(C = c j )

C

X1 X2 X5X3 X4 X6

ˆ P (c j ) =N(C = c j )

N

Page 36: Mining di dati web

Using Naive Bayes Classifiers to Classify Text: Basic method

Attributes are text positions, values are words.

Still too many possibilities Assume that classification is independent of the positions of the wordsUse same parameters for each position

cNB = argmaxcj ∈C

P(c j ) P(x i | c j )i

∏= argmax

cj ∈CP(c j )P(x1 ="our"| c j )L P(xn ="text"| c j )

Page 37: Mining di dati web

Textj single document containing all docsj

for each word xk in Vocabularynk number of occurrences of xk in Textj

Text Classification

Algorithms: Learning

From training corpus, extract Vocabulary Calculate required P(cj) and P(xk | cj) terms

For each cj in C dodocsj subset of documents for which the target class is cj

P(xk | c j ) ← nk +1n+ |Vocabulary |

P(c j ) ←| docs j |

| total # documents |

Page 38: Mining di dati web

Text Classification

Algorithms: Classifying

positions all word positions in current document which contain tokens

found in Vocabulary

Return cNB, where

cNB = argmaxc j ∈C

P(c j ) P(x i | c j )i∈positions

Page 39: Mining di dati web

Naïve Bayes Posterior

Probabilities Classification results of naïve Bayes (the class with maximum posterior probability) are usually fairly accurate.

However, due to the inadequacy of the conditional independence assumption, the actual posterior-probability numerical estimates are not. Output probabilities are generally very close to 0 or 1.

Page 40: Mining di dati web

Feature selection via Mutual Information

We might not want to use all words, but just reliable, good discriminators

In training set, choose k words which best discriminate the categories.

One way is in terms of Mutual Information:

For each word w and each category c

I(w,c) = p(ew,ec )log p(ew,ec )p(ew )p(ec )ec ∈{0,1}

∑ew ∈{0,1}∑

Page 41: Mining di dati web

Other Approaches to Feature Selection

T-TESTCHI SQUARETF/IDF (CFR. IR lectures)Yang & Pedersen 1997: eliminating features leads to improved performance

Page 42: Mining di dati web

Naive Bayes is not so Naive

Naïve Bayes: First and Second place in KDD-CUP 97 competition, among 16 (then) state of the art algorithms

Robust to Irrelevant FeaturesIrrelevant Features cancel each other without affecting resultsInstead Decision Trees & Nearest-Neighbor methods can heavily suffer from this.

Very good in Domains with many equally important featuresDecision Trees suffer from fragmentation in such cases – especially if little data

A good dependable baseline for text classification (but not the best)!

Optimal if the Independence Assumptions hold: If assumed independence is correct, then it is the Bayes Optimal

Classifier for problem Very Fast:

Learning with one pass over the data; testing linear in the number of attributes, and document collection size

Low Storage requirements Handles Missing Values

Page 43: Mining di dati web

Pantel and Lin: spamcop

Uses a Naïve Bayes classifier M is spam if P(Spam|M) > P(NonSpam|M) Method

Tokenize message using Porter Stemmer Estimate P(W|C) using m-estimate (a form of smoothing)

Remove words that do not satisfy certain conditions

Train: 160 spams, 466 non-spams Test: 277 spams, 346 non-spams

Results: ERROR RATE of 4.33% Worse results using trigrams

Page 44: Mining di dati web

K-Nearest Neighbor Classifiers

Nearest neighbor classifier belongs to instance-based learning methods.

Instance-based learning methods differ from other classification methods discussed earlier in that they do not build a classifier until a new unknown sample needs to be classified.

Each training sample is described by n-dimensional vector representing a point in an n-dimensional space called pattern space. When a new unknown sample has to be classified, a distance function is used to determine a member of the training set which is closest to the unknown sample.

Page 45: Mining di dati web

K-Nearest Neighbor Classifiers

Once the nearest training sample is located in the pattern space, its class label is assigned to the unknown sample.

The main drawback of this approach is that it is very sensitive to noisy training samples.

The common solution to this problem is to adopt the k-nearest neighbor strategy.

When a new unknown sample has to be classified, the classifier searches the pattern space for the k training samples which are closest to the unknown sample. These k training samples are called the k "nearest neighbors" of the unknown sample and the most common class label among k "nearest neighbors" is assigned to the unknown sample.

To find the k "nearest neighbors" of the unknown sample a multidimensional index is used (e.g. R-tree, Pyramid tree, etc.).

Page 46: Mining di dati web

K-Nearest Neighbor Classifiers

Two different issues need to be addressed regarding k-nearest neighbor method: the distance function, and the transformation from a sample to a point in the

pattern space. The first issue is to define the distance

function. If the attributes are numeric, most k-nearest neighbor classifiers use Euclidean distance.

Instead of the Euclidean distance, we may also apply other distance metrics like Manhattan distance, maximum of dimensions, or Minkowski distance.

Page 47: Mining di dati web

K-Nearest Neighbor Classifiers

The second issue is how to transform a sample to a point in the pattern space.

Note that different attributes may have different scales and units, and different variability. Thus, if the distance metric is used directly, the effects of some attributes might be dominated by other attributes that have larger scale or higher variability.

A simple solution to this problem is to weight the various attributes. One common approach is to normalize all attribute values into the range [0, 1].

Page 48: Mining di dati web

REFERENCES Mosteller, F., & Wallace, D. L. (1984). Applied Bayesian and Classical Inference: the Case of the Federalist Papers (2nd ed.). New York: Springer-Verlag.

P. Pantel and D. Lin, 1998. “SPAMCOP: A Spam classification and organization program”, In Proc. Of the 1998 workshop on learning for text categorization, AAAI

Sebastiani, F., 2002, “Machine Learning in Automated Text Categorization”, ACM Computing Surveys, 34(1), 1-47