TEORIE E TECNICHE DEL RICONOSCIMENTO CLASSIFICAZIONE AUTOMATICA CLASSIFICAZIONE DI PDD...

Preview:

Citation preview

TEORIE E TECNICHE DEL RICONOSCIMENTO

CLASSIFICAZIONE AUTOMATICACLASSIFICAZIONE DI PDDCLASSIFICAZIONE DI TESTI

CLASSIFICAZIONE

• Un CLASSIFICATORE e’ una FUNZIONE da oggetti che si vogliono classificare a etichette– Determinate la parte del discorso di un token– Assegnare valore SPAM/NO SPAM a email– Positivo / negativo

• La disambiguazione delle parti del discorso e molti altri problemi in LC possono essere visti come problemi di classificazione

DISAMBIGUAZIONE DELLE PARTI DEL DISCORSO COME CLASSIFICAZIONE

• La disambiguazione delle parti del discorso (POS tagging) puo’ essere vista come un classificatore che determina l’interpretazione piu’ probabile di ogni parola (nome, verbo, etc)

UNA VISIONE GEOMETRICA DELLA CLASSIFICAZIONE

SPAM

NON-SPAM

x1

x2

1 2 3

1

2

3

Predici “ “ if

ESEMPIO DI CLASSIFICATORE: REGRESSIONE LOGISTICA

ESEMPIO DI CLASSIFICATORE: DECISION TREE

IL RUOLO DELL’APPRENDIMENTO AUTOMATICO

• Un decision tree come quello appena visto potrebbe essere scritto a mano

• Ma nella linguistica computazionale moderna, questi classificatori non vengono specificati a mano, ma vengono APPRESI AUTOMATICAMENTE a partire da esempi.

METODI PER L’APPRENDIMENTO

• SUPERVISIONATI (SUPERVISED)– Imparano da esempi etichettati– Modellano l’apprendimento tramite insegnanti

• NON SUPERVISIONATI (UNSUPERVISED)– Scoprono da soli la struttura del problema– Modellano l’apprendimento del linguaggio

• SEMI-SUPERVISED– Ricevono come input pochi esempi poi procedono

per somiglianza

SUPERVISED CLASSIFICATION FOR POS TAGGING

• L’algoritmo di apprendimento riceve come input un corpus di TRAINING classificato con POS tags– La/Art gatta/N fece/V un/Art salto/N ./.– Giuseppe/PN e’/V matto/Adj ./.

• Si estrae le features / calcola le probabilita’• Costruisce un MODELLO che puo’ poi essere

usato per classificare ALTRI testi

TRAIN/TEST

POS TAGGER BASATI SU N-GRAMS

• Un POS TAGGER BASATO SU N-GRAMS e’ un classificatore che riceve come input informazioni sulla parola e le parole che la precedono– UNIGRAM PROBABILITY: P(N|salto), P(V|salto)– BI-GRAM PROBABILITY: P(NN|un salto)– ….

• Produce in output una probabilita’– P(N|Uprob, 2Prob, ….) = …– P(V|UProb, 2Prob, …) = …

• Sceglie la PDD con probabilita’ maggiore

16

POS-TAGGING A N-GRAMS

• Scelta della soluzione con la probabilita’ maggiore:

)..|..(argmax 11Tt

nn wwttPi

17

Classificazione di PDD usando n-grammi

FREQUENZA

CONTESTO

18

Stima delle probabilita’

Can be done using Maximum Likelihood Estimation as usual, for BOTH probabilities:

19

Esempio

• Secretariat/NNP is/VBZ expected/VBN to/TO race/VB tomorrow/NN

• People/NNS continue/VBP to/TO inquire/VB the/DT reason/NN for/IN the/DT race/NN for/IN outer/JJ space/NN

• Problem: assign a tag to race given the subsequences– to/TO race/???– the/DT race/???

• Solution: we choose the tag that has the greater of these probabilities:– P(VB|TO) P(race|VB)– P(NN|TO)P(race|NN)

20

Tagging with MMs (2)• Actual estimates from the Switchboard corpus:• LEXICAL FREQUENCIES:

– P(race|NN) = .00041– P(race|VB) = .00003

• CONTEXT:– P(NN|TO) = .021– P(VB|TO) = .34

• The probabilities:– P(VB|TO) P(race|VB) = .00001– P(NN|TO)P(race|NN) = .000007

N-GRAM POS TAGGER IN NLTK

• NLTK, 5.5:– Addestramento di unigrammi– Addestramento di bigrammi

ALTRI METODI PER L’APPRENDIMENTO

• NAÏVE BAYES• DECISION TREES

NAÏVE BAYES

• Metodi Bayesiani: decisione su classificazione basata su – un modello PROBABILISTICO – che coniuga uso di informazioni A PRIORI ed A

POSTERIORI come nella regola di Bayes• Metodi NAÏVE BAYES: si fanno assunzioni che

semplificano molto il calcolo delle probabilità

Legge di Bayes

)()|()()|(),( CPCXPXPXCPXCP

)(

)()|()|(

XP

CPCXPXCP

Bayes applicata alla classificazione

P(Classe|Proprietà) = P(Proprietà|Classe)*P(Classe) / P(Proprietà)

Maximum a posteriori Hypothesis

)|(argmax DhPhHh

MAP

)(

)()|(argmax

DP

hPhDPh

HhMAP

)()|(argmax hPhDPhHh

MAP

Naive Bayes Classifiers

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

nxxx ,,, 21

),,,|(argmax 21 njCc

MAP xxxcPcj

),,,(

)()|,,,(argmax

21

21

n

jjn

CcMAP cccP

cPcxxxPc

j

)()|,,,(argmax 21 jjnCc

MAP cPcxxxPcj

Naïve Bayes Classifier: Assumptions

• P(cj)– Can be estimated from the frequency of classes in

the training examples.

• P(x1,x2,…,xn|cj) – O(|X|n•|C|)– Could only be estimated if a very, very large number

of training examples was available.Conditional Independence Assumption: Assume that the probability of observing the conjunction of

attributes is equal to the product of the individual probabilities.

Flu

X1 X2 X5X3 X4

feversinus coughrunnynose muscle-ache

The Naïve Bayes Classifier

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

)|()|()|()|,,( 52151 CXPCXPCXPCXXP

Learning the Model

• Common practice:maximum likelihood– simply use the frequencies in the data

)(

),()|(ˆ

j

jiiji cCN

cCxXNcxP

C

X1 X2 X5X3 X4 X6

N

cCNcP jj

)()(ˆ

Problem with Max Likelihood

0)(

),()|(ˆ 5

5

nfCN

nfCtXNnfCtXP

• What if we have seen no training cases where patient had no flu and muscle aches?

• Zero probabilities cannot be conditioned away, no matter the other evidence!

i ic cxPcP )|(ˆ)(ˆmaxarg

Flu

X1 X2 X5X3 X4

feversinus coughrunnynose muscle-ache

)|()|()|()|,,( 52151 CXPCXPCXPCXXP

Smoothing to Avoid Overfitting

kcCN

cCxXNcxP

j

jiiji

)(

1),()|(ˆ

• Somewhat more subtle version

# of values of Xi

mcCN

mpcCxXNcxP

j

kijkiijki

)(

),()|(ˆ ,,

,

overall fraction in data where Xi=xi,k

extent of“smoothing”

Using Naive Bayes Classifiers to Classify Text: Basic method

• Attributes are text positions, values are words.

Naive Bayes assumption is clearly violated. Example?

Still too many possibilities Assume that classification is independent of the positions of the

words Use same parameters for each position

)|text""()|our""()(argmax

)|()(argmax

1j

j

jnjjCc

ijij

CcNB

cxPcxPcP

cxPcPc

ESEMPIO DI CLASSIFICAZIONE: DOCUMENT CLASSIFICATION

(NLTK book, p. 227-228)

VALUTAZIONE

• ACCURACY: percentuale di risposte corrette• Nel caso di problemi in cui la classe di

interesse rappresenta una percentuale minima del totale: PRECISION e RECALL

PRECISION E RECALL

ESEMPIO DI CLASSIFICAZIONE: GENDER IDENTIFICATION

(NLTK book, p. 222-227)

APPRENDERE DECISION TREES

• Top-down: dato un certo insieme di esempi, trovare la proprieta’ che permette di dividerli in sottogruppi piu’ COERENTI

• Poi si procede ricorsivamente• Scelta della proprieta’: INFORMATION GAIN

DECISION TREE PER IL NAME GENDER TASK

Selezione della proprieta’

• La scelta della proprieta’ da usare per dividere l’insieme corrente in sottinsiemi piu’ coerenti si basa su un criterio di RIDUZIONE DEL DISORDINE basato sulla nozione di ENTROPIA

ENTROPIA

Entropia di decision trees

• Entropia del DT per gender identification: vedi NLTK, 6.4

INFORMATION GAIN

TEXT CATEGORIZATION WITH DT

• Build a separate decision tree for each category

• Use WORDS COUNTS as features

54

Reuters Data Set (21578 - ModApte split)

• 9603 training, 3299 test articles; ave. 200 words• 118 categories

– An article can be in more than one category– Learn 118 binary category distinctions

• Earn (2877, 1087) • Acquisitions (1650, 179)• Money-fx (538, 179)• Grain (433, 149)• Crude (389, 189)

• Trade (369,119)• Interest (347, 131)• Ship (197, 89)• Wheat (212, 71)• Corn (182, 56)

Common categories(#train, #test)

Foundations of Statistical Natural Language Processing, Manning and Schuetze

AN EXAMPLE OF REUTERS TEXT

Foundations of Statistical Natural Language Processing, Manning and Schuetze

Decision Tree for Reuter classification

Information gain & text classification