Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
NLTK 6: 텍스트분류학습(nltk.org/book/ch06.html)
유실수
2015년 10월 27일
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 1 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
개요Learning to Classify Text1 Supervised Classification
Gender IdentificationChoosing The Right FeaturesDocument Classification
2 Further Examples of Supervised Classification3 Evaluation
The Test SetAccuracyPrecision and RecallConfusion MatricesCross-Validation
4 Maximum Entropy modeling assignment
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 2 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised Classification
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 3 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised Classification
지도분류(Supervised Classification)1 훈련(Training): 정답이표시된(=이미분류된)자료→패턴학습
▶ 자질(Feature): 자료의패턴을식별하고기술하는기준▶ 알고리듬(Algorithm): 자질값으로부터분류결과를계산해내는방법
2 실험(Test)=예측(Prediction): 학습된패턴→새로운자료분류
주의 분류가능한범주의목록은미리정해져있다.→분류중에새로운목록을만들수없다!
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 4 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationGender Identification
이름성별분류: 자질추출패턴 a,e,i로끝남→여성
k,o,r,s,t로끝남→남성자질 이름의끝글자가무엇인가?
함수정의 주어진이름의자질값을뽑기 (변수명 gender_features)입력 문자열 (변수명 word)출력 딕셔너리
키 ’last_letter’값 word[-1] (word의마지막한글자)
1 >>> def gender_features(word):2 ... return {’last_letter’: word[-1]}3 >>> gender_features(’Shrek’)4 {’last_letter’: ’k’}
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 5 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationGender Identification
이름성별분류: 자료확보출전 nltk.corpus.names
코퍼스설치: nltk.download()유형 리스트 (변수명 labeled_names)
원소 2-tuple: (이름, 레이블=성별)예시 (u’Aaron’, ’male’)
(u’Zoe’, ’female’)순서섞기 random.shuffle()
1 >>> from nltk.corpus import names2 >>> labeled_names = (3 ... [(name, ’male’) for name in names.words(’male.txt’)] +4 ... [(name, ’female’) for name in names.words(’female.txt’)])5 >>> import random6 >>> random.shuffle(labeled_names)
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 6 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationGender Identification이름성별분류: 분류기구성→훈련집합에서학습
1 자질집합구성: 리스트 (변수명 featuresets)원소 2-tuple: (자질=딕셔너리, 레이블=성별)예시 ({’last_letter’: n}, ’male’)
({’last_letter’: e}, ’female’)▶ 주의: Aaron, Zoe 등구체적인이름이자질로환원됨
2 코퍼스분할평가집합 자질집합의앞500개원소훈련집합 자질집합의나머지원소 (7,944 - 500 = 7,444개)
3 분류기 (변수명 classifier)알고리듬 Naive Bayes: nltk.NaiveBayesClassifier
1 >>> featuresets = [(gender_features(n), gender)2 ... for (n, gender) in labeled_names]3 >>> train_set, test_set = featuresets[500:], featuresets[:500]4 >>> classifier = nltk.NaiveBayesClassifier.train(train_set)
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 7 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationGender Identification이름성별분류: 분류기적용
함수 분류기.classify()입력 자질딕셔너리출력 레이블=성별
1 >>> classifier.classify(gender_features(’Neo’))2 ’male’3 >>> classifier.classify(gender_features(’Trinity’))4 ’female’
이름성별분류: 분류기성능평가함수 nltk.classify.accuracy()
입력 분류기, 실험집합출력 정확도 (실험집합에서분류기적용결과가정답과일치하는것의개수/실험집합원소개수)
결과 0.77→우연히맞출0.5보다높다!1 >>> print(nltk.classify.accuracy(classifier, test_set))2 0.77 유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 8 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationGender Identification
이름성별분류: 자질검토자질정보량평가
함수 분류기.show_most_informative_features()입력 개수n출력 정보량이높은상위n개자질예시 a로끝나면여성일확률이남성일확률보다33.2배높다.
1 >>> classifier.show_most_informative_features(5)2 Most Informative Features3 last_letter = ’a’ female : male = 33.2 : 1.04 last_letter = ’k’ male : female = 32.6 : 1.05 last_letter = ’p’ male : female = 19.7 : 1.06 last_letter = ’v’ male : female = 18.6 : 1.07 last_letter = ’f’ male : female = 17.3 : 1.0
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 9 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features이름성별분류: 자질추가
목표 기존모형성능(정확도 0.77) 개선자질 끝글자(”last_letter”) 외에추가할정보
첫글자 (”first_letter”)글자개수 (”count(letter)”)글자포함여부 (”has(letter”)
1 >>> def gender_features2(name):2 ... features = {}3 ... features[”first_letter”] = name[0].lower()4 ... features[”last_letter”] = name[-1].lower()5 ... for letter in ’abcdefghijklmnopqrstuvwxyz’:6 ... features[”count({})”.format(letter)] = name.lower().count(letter)7 ... features[”has({})”.format(letter)] = (letter in name.lower())8 ... return features9 >>>
10 >>> gender_features2(’John’)11 {’count(j)’: 1, ’has(d)’: False, ’count(b)’: 0, ...}
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 10 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features
이름성별분류: 성능비교gender_features() v. gender_features2()
1 gender_features2()를바탕으로자질집합구성2 동일한학습집합에서새분류기학습3 새분류기를동일한실험집합에적용4 정확도평가: 0.768 < 0.77
→자질이추가되었지만결과가나쁘다!1 >>> featuresets = [(gender_features2(n), gender)2 ... for (n, gender) in labeled_names]3 >>> train_set, test_set = featuresets[500:], featuresets[:500]4 >>> classifier = nltk.NaiveBayesClassifier.train(train_set)5 >>> print(nltk.classify.accuracy(classifier, test_set))6 0.768
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 11 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features이름성별분류: 오류분석개발집합(development set) 도입
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 12 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features이름성별분류: 오류분석코퍼스재분할
실험 1-500번째이름개발-평가 501‒1500번째이름
훈련 나머지이름→분류기성능을개발-평가집합에서먼저평가: 정확도 0.75
1 >>> train_names = labeled_names[1500:]2 >>> devtest_names = labeled_names[500:1500]3 >>> test_names = labeled_names[:500]4 >>> train_set = [(gender_features(n), gender)5 ... for (n, gender) in train_names]6 >>> devtest_set = [(gender_features(n), gender)7 ... for (n, gender) in devtest_names]8 >>> test_set = [(gender_features(n), gender)9 ... for (n, gender) in test_names]
10 >>> classifier = nltk.NaiveBayesClassifier.train(train_set)11 >>> print(nltk.classify.accuracy(classifier, devtest_set))12 0.75
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 13 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features
이름성별분류: 오류분석개발-평가집합에서정답(변수명 tag)과예측(변수명 guess)이다른목록
1 >>> errors = []2 >>> for (name, tag) in devtest_names:3 ... guess = classifier.classify(gender_features(name))4 ... if guess != tag:5 ... errors.append( (tag, guess, name) )
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 14 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features이름성별분류: 오류분석출력된결과의패턴관찰→마지막두글자가중요하다!
yn 여성ch 남성
1 >>> for (tag, guess, name) in sorted(errors):2 ... print(’correct={:<8} guess={:<8s} name={:<30}’.3 ... format(tag, guess, name))4 correct=female guess=male name=Abigail5 ...6 correct=female guess=male name=Cindelyn7 ...8 correct=female guess=male name=Katheryn9 correct=female guess=male name=Kathryn
10 ...11 correct=male guess=female name=Aldrich12 ...13 correct=male guess=female name=Mitch14 ...15 correct=male guess=female name=Rich16 ...
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 15 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationChoosing The Right Features
이름성별분류: 자질추출기수정자질 목록수정
마지막한글자마지막두글자
결과 정확도 0.782→마지막글자만사용했을때(0.77)보다높아졌다!
1 >>> def gender_features(word):2 ... return {’suffix1’: word[-1:],3 ... ’suffix2’: word[-2:]}4 >>> train_set = [(gender_features(n), gender)5 ... for (n, gender) in train_names]6 >>> devtest_set = [(gender_features(n), gender)7 ... for (n, gender) in devtest_names]8 >>> classifier = nltk.NaiveBayesClassifier.train(train_set)9 >>> print(nltk.classify.accuracy(classifier, devtest_set))
10 0.782
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 16 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationDocument Classification
영화평분류: 자료확보출전 nltk.corpus.movie_reviews유형 리스트 (변수명 documents)
원소 2-tuple: (문서내단어목록, 레이블=긍정·부정)예시 ([u’plot’, ..., u’echoes’], u’neg’)
([u’if’, u’there’, ...], u’pos’)순서섞기 random.shuffle()
1 >>> from nltk.corpus import movie_reviews2 >>> documents = [(list(movie_reviews.words(fileid)), category)3 ... for category in movie_reviews.categories()4 ... for fileid in movie_reviews.fileids(category)]5 >>> random.shuffle(documents)
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 17 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationDocument Classification영화평분류: 자질추출
자질 고빈도단어 (총 2,000개)빈도분포추출함수 nltk.FreqDist()
유형 딕셔너리키 contains(word)값 단어가문서에있는지여부 (True/False)
1 >>> all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())2 >>> word_features = list(all_words)[:2000] [1]3 >>>4 >>> def document_features(document): [2]5 ... document_words = set(document) [3]6 ... features = {}7 ... for word in word_features:8 ... features[’contains({})’.format(word)] = (word in document_words)9 ... return features
10 >>> print(document_features(movie_reviews.words(’pos/cv957_8737.txt’)))11 {’contains(waste)’: False, ’contains(lot)’: False, ...}
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 18 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised ClassificationDocument Classification이름성별분류: 분류기구성→학습→평가
1 자질집합구성2 코퍼스분할—평가:훈련=100:19003 분류기알고리듬: Naive Bayes4 성능: 정확도 0.81
▶ 주요자질: outstanding, seagal, wonderfully, damon, wasted, ...
1 >>> featuresets = [(document_features(d), c) for (d,c) in documents]2 >>> train_set, test_set = featuresets[100:], featuresets[:100]3 >>> classifier = nltk.NaiveBayesClassifier.train(train_set)4 >>> print(nltk.classify.accuracy(classifier, test_set))5 0.816 >>> classifier.show_most_informative_features(5)7 Most Informative Features8 contains(outstanding) = True pos : neg = 11.1 : 1.09 contains(seagal) = True neg : pos = 7.7 : 1.0
10 contains(wonderfully) = True pos : neg = 6.8 : 1.011 contains(damon) = True pos : neg = 5.9 : 1.012 contains(wasted) = True neg : pos = 5.8 : 1.0유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 19 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
개요Learning to Classify Text1 Supervised Classification
Gender IdentificationChoosing The Right FeaturesDocument Classification
2 Further Examples of Supervised Classification3 Evaluation
The Test SetAccuracyPrecision and RecallConfusion MatricesCross-Validation
4 Maximum Entropy modeling assignment
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 20 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
개요Learning to Classify Text1 Supervised Classification
Gender IdentificationChoosing The Right FeaturesDocument Classification
2 Further Examples of Supervised Classification3 Evaluation
The Test SetAccuracyPrecision and RecallConfusion MatricesCross-Validation
4 Maximum Entropy modeling assignment
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 21 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationThe Test Set
실험집합구성방법: (1) 순서뒤섞기출전 브라운코퍼스뉴스카테고리분할 문장순서뒤섞기 random.shuffle(tagged_sents)
평가집합 문장목록앞 10%훈련집합 문장목록뒤90%
문제점 1 평가집합과훈련집합의장르동일→다른장르에적용되는지확인불가능
2 평가집합과훈련집합의문서중복→훈련효과와정답단순암기의구별불가능
1 >>> import random2 >>> from nltk.corpus import brown3 >>> tagged_sents = list(brown.tagged_sents(categories=’news’))4 >>> random.shuffle(tagged_sents)5 >>> size = int(len(tagged_sents) * 0.1)6 >>> train_set, test_set = tagged_sents[size:], tagged_sents[:size]
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 22 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationThe Test Set
실험집합구성방법: (2) 문서파일아이디로구별출전 브라운코퍼스뉴스카테고리분할 문서파일아이디로구별 (file_ids)
평가집합 파일아이디목록중앞 10%훈련집합 파일아이디목록중뒤90%
개선점 1 평가집합과훈련집합의문서중복문제해결→분류기의신뢰성확보
문제점 1 평가집합과훈련집합의장르동일→다른장르에적용되는지확인불가능
1 >>> file_ids = brown.fileids(categories=’news’)2 >>> size = int(len(file_ids) * 0.1)3 >>> train_set = brown.tagged_sents(file_ids[size:])4 >>> test_set = brown.tagged_sents(file_ids[:size])
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 23 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationThe Test Set
실험집합구성방법: (3) 문서장르로구별출전 브라운코퍼스뉴스카테고리분할 카테고리값으로구별
평가집합 픽션장르문장목록훈련집합 뉴스장르문장목록
개선점 1 평가집합과훈련집합의문서중복문제해결→분류기의신뢰성확보
2 평가집합과훈련집합의장르동일문제해결→분류기의일반성확인가능
1 >>> train_set = brown.tagged_sents(categories=’news’)2 >>> test_set = brown.tagged_sents(categories=’fiction’)
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 24 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationAccuracy
다양한분류성능평가척도정확도 (Accuracy)정밀도 (Precision)재현도 (Recall)F-점수 (F-score)…
정확도(Accuracy)전체자료에서제대로예측한것이얼마나되는가?
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 25 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationAccuracy
예시: 스팸메일감지스팸취급 멀쩡취급
진짜스팸
진짜멀쩡
정확도 =+
+ + +
=실제=예측인것들의개수
전체개수
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 26 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationAccuracy
예시: 스팸메일감지분류방법
양성(Positive): 스팸음성(Negative): 스팸아님→정상
분류결과스팸을스팸이라고함: (진양성True positive)정상을스팸이라고함: (위양성 False positive — Type I Error)스팸을정상이라고함: (위음성 False negative — Type II Error)정상을정상이라고함: (진음성True negative)
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 27 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationAccuracy
정확도측정의문제한쪽부류의빈도가압도적으로높은경우→무조건한쪽부류로예측해도정확도가높다.
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 28 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationAccuracy
예시: 스팸메일감지이메일 1,000통중 990통이스팸이고 10통이멀쩡할때→무조건스팸으로취급해도99%의정확도를얻는다!
스팸을스팸이라고함: 990정상을스팸이라고함: 10스팸을정상이라고함: 0 (무조건스팸이라고하기로했으므로)정상을정상이라고함: 0 (무조건스팸이라고하기로했으므로)
정확도 =+
+ + +
=990 + 0
990 + 10 + 0 + 0=
990
1000= 99%
→받아야하는메일 10통을모두놓치고도99%의정확도로칭찬받는다!
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 29 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationPrecision and Recall
정밀도와재현도정밀도(Precision): 찾은것중에서제대로찾은것이얼마나되는가?재현도(Recall): 찾아야하는것중에서제대로찾은것이얼마나되는가?F-Score: 정밀도와재현도의조화평균
찾지말아야하는것을찾지않은경우(진음성, True Negative) 무시
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 30 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationPrecision and Recall
정보검색(Information Retrieval)에서자주쓰이는척도
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 31 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationPrecision and Recall예시: 스팸메일감지
스팸취급 멀쩡취급진짜스팸 990 0진짜멀쩡 10 0
스팸메일기준척도“제대로찾은것” 스팸을스팸이라고함 = 990“찾은것” 스팸이라고함 + = 990 + 10 = 1000“찾아야하는것” 진짜스팸임 + = 990 + 0 = 990
정밀도 990/1000 = 99%재현도 990/990 = 100%
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 32 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationPrecision and Recall예시: 스팸메일감지
멀쩡취급 스팸취급진짜멀쩡 0 10진짜스팸 0 990
정상메일기준척도“제대로찾은것” 정상을정상이라고함 = 0“찾은것” 정상이라고함 + = 0 + 0 = 0“찾아야하는것” 진짜정상임 + = 10 + 0 = 10
정밀도 0/0 = (분모가 0이므로계산불가, 관례상 0%으로도표현)재현도 0/10 = 0%유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 33 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationConfusion Matrices세가지이상의범주로분류하는경우1 >>> def tag_list(tagged_sents):2 ... return [tag for sent in tagged_sents for (word, tag) in sent]3 >>> def apply_tagger(tagger, corpus):4 ... return [tagger.tag(nltk.tag.untag(sent)) for sent in corpus]5 >>> gold = tag_list(brown.tagged_sents(categories=’editorial’))6 >>> test = tag_list(apply_tagger(t2,7 ... brown.tagged_sents(categories=’editorial’)))8 >>> cm = nltk.ConfusionMatrix(gold, test)9 >>> print(cm.pretty_format(sort_by_count=True, show_percents=True,
10 truncate=9))11 | N |12 | N I A J N V N |13 | N N T J . S , B P |14 ----+----------------------------------------------------------------+15 NN | <11.8%> 0.0% . 0.2% . 0.0% . 0.3% 0.0% |16 IN | 0.0% <9.0%> . . . 0.0% . . . |17 AT | . . <8.6%> . . . . . . |18 JJ | 1.7% . . <3.9%> . . . 0.0% 0.0% |19 . | . . . . <4.8%> . . . . |20 NNS | 1.5% . . . . <3.2%> . . 0.0% |21 , | . . . . . . <4.4%> . . |22 VB | 0.9% . . 0.0% . . . <2.4%> . |23 NP | 1.0% . . 0.0% . . . . <1.8%>|24 ----+----------------------------------------------------------------+25 (row = reference; col = test)
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 34 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
EvaluationCross-Validation
교차검증법 (Cross-validation)코퍼스N등분→훈련집합-실험집합분할N회반복
예시: 10-fold cross-validation1 코퍼스 10등분: C1, C2, …, C102 분류기학습-적용-평가 10회반복
▶ 훈련집합C2, C3, …, C10 / 실험집합C1▶ 훈련집합C1, C3, …, C10 / 실험집합C2▶ …▶ 훈련집합C1, …, C8, C10 / 실험집합C9▶ 훈련집합C1, C2, …, C9 / 실험집합C10
3 총 10회성능척도의평균계산
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 35 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
개요Learning to Classify Text1 Supervised Classification
Gender IdentificationChoosing The Right FeaturesDocument Classification
2 Further Examples of Supervised Classification3 Evaluation
The Test SetAccuracyPrecision and RecallConfusion MatricesCross-Validation
4 Maximum Entropy modeling assignment
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 36 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguationhttp://www-rohan.sdsu.edu/~gawron/compling/course_core/assignments/new_maxent_assignment.htm
과제단어의미중의성해소 (WSD: Word Sense Disambiguation)
분류대상 동음이의어나다의어의의미예시 영어형용사 ‘hard’
1 difficult to do▶ I find it hard to believe that …
2 potent (as in “the hard stuff”)▶ They look old and dumpy, with stolid, hard faces.
3 physically resistant to denting, bending, orscratching
▶ Water becomes stiff and hard as clear stone.
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 37 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguation코퍼스
파일 senseval-hard.xml규모 4,333문장
전처리 소문자화, 품사부착레이블 단어-품사(word=”hard-a”), 의미(”HARD1”), 위치(”20”)예시 <senseval_instance word=”hard−a” sense=”
HARD1” posit ion=”20”>‘ ‘ _ ‘ ‘ he_PRP may_MD lose_VB all_DT
popular_JJ support_NN ,_ , but_CCsomeone_NN has_VBZ to_TO ki l l_VBhim_PRP to_TO defeat_VB him_PRP and_CCthat_DT ’s_VBZ hard_JJ to_TO do_VB . _ .’ ’ _ ’ ’
</ senseval_instance >유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 38 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguation
자질추출기파일 call_extract_event.py자질 문장내단어존재여부 (1: 있음, 0: 없음)입력 코퍼스파일 senseval-hard.xml출력 이벤트파일 senseval-hard.evt (1문장 = 1이벤트)예시 BEGIN EVENT
HARD1soft_JJ 0has_VBZ 1. . .only_RB 0END EVENT
문제 자질을어떻게선택할것인가?유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 39 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguation
분류기학습파일 call_maxent.py모형 최대엔트로피모형nltk.classify.maxent.MaxentClassifier
(IIS 알고리듬: improved iterative scaling)반복횟수 50회
코퍼스분할 4,333개중 4,100개사용→순서뒤섞기훈련집합 앞90%문장실험집합 뒤 10%문장
결과 실험집합410개문장분류
주의 NLTK 3.0에서변경된함수명변경classify.batch_classify()→ classify.classify_many()
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 40 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguation
분류기학습 (계속)예시1 Testing classifier...
2 Accuracy: 0.82203 Total: 410456 Label Precision Recall7 _____________________________________________8 HARD1 0.822 1.0009 HARD2 0.000 0.000
10 HARD3 0.000 0.000111213 Label Num Corr14 HARD1 33715 HARD2 016 HARD3 0
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 41 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguation분류기학습 (계속)
해석 (전체정확도) = (HARD1정밀도) = 0.8220 = 337/410→ 410개문장전체가HARD1으로분류됨1 Testing classifier...2 Accuracy: 0.82203 Total: 410456 Label Precision Recall7 _____________________________________________8 HARD1 0.822 1.0009 HARD2 0.000 0.000
10 HARD3 0.000 0.000111213 Label Num Corr14 HARD1 33715 HARD2 016 HARD3 0
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 42 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Maximum Entropy modeling assignmentWord Sense Disambiguation1 def extract_vocab(event_list, n=100):2 # Google’s stoplist with most preps removed. ”and” added, word added3 stopwords = [ ’I’, ’a’, ’an’, ’are’, ’as’, ’and’,4 ’be’, ’com’, ’how’, ’is’, ’it’, ’of’, ’or’,5 ’that’, ’the’, ’this’, ’was’, ’what’,6 ’when’, ’where’, ’who’, ’will’, ’with’,7 ’the’, ’www’, ’was’]8 vocab = Counter()9 for (s_inst, sense) in event_list:
10 for (i,item) in enumerate(s_inst.context):11 if i == int(s_inst.position):12 continue13 (item, wd, pos) = get_lex_components(item)14 if wd in stopwords:15 continue16 if pos in [’PRP’,’IN’,’CC’,’DT’]:17 continue18 vocab[item] += 119 il = vocab.items()20 il.sort(key=lambda x: x[1],reverse=True)21 il = il[:n]22 vocab = dict(il)23 return vocab
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 43 / 44
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Supervised Classification1 자질집합: [({자질딕셔너리}, 분류결과)튜플을원소로가지는리스트]
1 featsets = [({’feat1’: ’val11’, ’feat2’: ’val21’, ...}, ’class1’),2 ({’feat1’: ’val12’, ’feat2’: ’val22’, ...}, ’class2’),3 ...,4 ({’feat1’: ’val1N’, ’feat2’: ’val2N’, ...}, ’classN’)]
2 집합분할: 일반적으로학습:실험=9:1로분할1 N = len(featsets)2 train_set = featsets[:int(N*0.9)]3 test_set = featsets[int(N*0.9):]
3 분류기학습: 분류기클래스의 train메소드로학습집합에서학습1 classifier = nltk.classify.naivebayes.NaiveBayesClassifier2 classifier = nltk.classify.maxent.MaxentClassifier3 classifier.train(train_set)4 dir(nltk.classify)
4 분류기적용및평가: 학습한분류기로실험집합을예측한결과평가1 nltk.classify.accuracy(classifier, test_set))
유실수 NLTK 6: 텍스트분류학습 2015년 10월 27일 44 / 44