Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 : Classification (1)

Similar presentations


Presentation on theme: "Lecture 3 : Classification (1)"— Presentation transcript:

1 Lecture 3 : Classification (1)
楊立偉教授 本投影片修改自Introduction to Information Retrieval一書之投影片 Ch 13~14

2 近年來機器學習的進展 (1) 監督式學習 supervised learning 演算法及模型、計算複雜度及規模、巨量資料
Top 10 Data Mining Algorithm (IEEE ICDM 2006) 1 C4.5 and Beyond 決策樹及規則 2 k-Means 資料分群 3 Support Vector Machines (SVM) 自動分類 4 Apriori 關聯分析 5 Expectation Maximization (EM) 最大概似估計 6 PageRank 連結分析 7 AdaBoost 適應增強學習 8 k-Nearest Neighbor (kNN) 自動分類 9 Naive Bayes (NB) 自動分類 10 Classification and Regression Trees (CART) 決策樹

3 近年來機器學習的進展 (2) "What Artificial Intelligence Can and Can't Do Right Now" by Andrew Ng, Harvard Business Review, 2016 Nov. 由訓練到自動,「依樣畫葫蘆」 目標-嘗試-獎勵,相互對抗 臉部辨識 貸款核准 精準廣告 語音辨識 機器翻譯 自動駕駛

4 近年來機器學習的進展 (3) Neural Networks (NN) to Deep Learning (DL)
Backpropagation and Gradient Descent algorithm

5 Recurrent Neural Networks (RNN)
Ref: Recurrent neural network based language model. 2010

6 Long Short-Term Memory (LSTM) RNN
Ref:

7 Convolution Neural Networks (CNN)
Ref: Imagenet classification with deep convolutional neural networks. 2010

8 Text Classification

9 A text classification task: Email spam filtering
From: ‘‘’’ Subject: real estate is the only way... gem oalvgkay Anyone can buy real estate with no money down Stop paying rent TODAY ! There is no need to spend hundreds or even thousands for similar courses I am 22 years old and I have already purchased 6 properties using the methods outlined in this truly INCREDIBLE ebook. Change your life NOW ! ================================================= Click Below to order: How would you write a program that would automatically detect and delete this type of message? 9

10 Formal definition of TC: Training
Given: A document space X Documents are represented in this space – typically some type of high-dimensional space. A fixed set of classes C = {c1, c2, , cJ} The classes are human-defined for the needs of an application (e.g., relevant vs. nonrelevant). A training set D of labeled documents with each labeled document <d, c> ∈ X × C Using a learning method or learning algorithm, we then wish to learn a classifier ϒ that maps documents to classes: ϒ : X → C 10

11 Formal definition of TC: Application/Testing
Given: a description d ∈ X of a document Determine: ϒ (d) ∈ C, that is, the class that is most appropriate for d 11

12 Topic classification 有些分類具階層性 有些可能屬多個分類 12

13 Example Test Data: (AI) (Programming) (HCI) Classes: ML Planning
language proof intelligence" Test Data: (AI) (Programming) (HCI) Classes: ML Planning Semantics Garb.Coll. Multimedia GUI Training Data: learning intelligence algorithm reinforcement network... planning temporal reasoning plan language... programming semantics language proof... garbage collection memory optimization region... ... ... ML: machine learning HCI: human computer interface

14 More examples of TC Applications
Labeling 貼標籤 (歸類) Assign labels to each document : Labels are most often topics such as Yahoo-categories 主題 e.g., "finance," "sports," "news>world>asia>business" Labels may be language or genres 語言或型式 e.g., “English" “Chinese" “French“ e.g., "editorials" "movie-reviews" "news“ Labels may be sentiments 情緒 e.g., “like”, “hate”, “neutral” Labels may be domain-specific binary 是否屬於某領域 e.g., "interesting-to-me" : "not-interesting-to-me” e.g., “spam” : “not-spam” e.g., “contains adult language” :“doesn’t”

15 More examples of TC Applications
新聞自動分類:將每日新聞自動歸類重新編排 專利自動分類:將申請的專利給予適當的類別以利分派審查 1999案件自動分類:將民眾抱怨快速分派給正確的單位做處理ˋ 用文件分類預測股價走勢:出些某些文件,之後七日內該股票之 之平均收盤價走高或走低(看漲文件及看跌文件)

16 Classification Methods (1)
Manual classification 人工分類 Used by Yahoo, ODP, PubMed Very accurate when job is done by experts 靠領域與分類專家,所以很準 Consistent when the problem size and team is small 當資料量大,用人工判斷會有主觀不一致的問題 Difficult and expensive to scale need automatic methods for classification 註 : ODP - Open Directory Project 開放分類目錄計劃

17 Classification Methods (2)
Rule-based systems 規則式分類 Google Alerts is an example of rule-based classification Assign category if document contains a given boolean combination of words 使用布林條件,例如 (文化創意 | 文創) → 歸於文化類 Accuracy is often very high if a rule has been carefully refined over time by a subject expert Building and maintaining these rules is cumbersome and expensive 例如 (文化創意 | 文創 | 電影 | 工藝 | 藝術….) → 歸於文化類 需要很多的列舉與排除

18 Classification Methods (3)
Statistical/Probabilistic systems 統計機率式 Text classification as a learning problem (i) Supervised learning of a the classification function ϒ and (ii) its application to classifying new documents Examples Naive Bayes (simple, common method) k-Nearest Neighbors (simple, powerful) Support-vector machines (new, more powerful) No free lunch: requires hand-classified training data But data can be built up (and refined) by non-experts

19 Naïve Bayes

20 Overview The Naive Bayes classifier is a probabilistic classifier.
基於機率學的貝氏定理 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. 訣竅 : 將條件機率展開成可以計算的機率,然後計算之

21 Posterior probability 條件機率
已知學生為20歲中女性之機率 P(B|A)=21/35=0.6 或利用公式 P(B|A) = P(A∩B) / P(A) = 0.42/0.7 = 0.6 50 15 35 30 9 21 20 6 14 非20歲 20歲 性別 年齡

22 Bayes' Rule 基本貝式定理:將條件機率 (事後機率) 各自展開後搬移項所得 給定X文件 歸於C類的機率
其中P(X) : 給定X文件的機率是常數

23 Naive Bayes Classifiers (1)
Task: Classify a new instance D based on a tuple of attribute values into one of the classes cj  C Naive Bayes classification is to find the "best" class (the most likely or maximum a posteriori (MAP) class Cmap ) as P(D) is constant

24 Naive Bayes Classifiers (2)
P(cj) Can be estimated from the frequency of classes in the training examples. 可以由訓練資料中計算而得 P(x1,x2,…,xn|cj) Could be estimated if a very large number of training examples was available. applying Naïve Bayes Conditional Independence Assumption

25 Naïve Bayes Assumption
Flu X1 X2 X5 X3 X4 fever sinus cough runnynose muscle-ache Conditional Independence Assumption: Assume that the probability of observing the conjunction of attributes is equal to the product of the individual probabilities P(xi|cj). sinus: 彎曲, 廔管

26 Exercise Test Data: (AI : 10篇) (Programming : 6篇) (HCI : 4篇) Classes:
"planning language proof intelligence" Test Data: (AI : 10篇) (Programming : 6篇) (HCI : 4篇) Classes: ML Planning Semantics Garb.Coll. Multimedia GUI Training Data: learning 8次 intelligence 4次 algorithm 3次 reinforcement 2次 network 7次 planning 6次 temporal 2次 reasoning 3次 plan 7次 language 6次 programming 4次 semantics 5次 language 8次 proof 3次 garbage 2次 collection 4次 memory 8次 optimization 5次 region 3次 ... ... ML: machine learning HCI: human computer interface 共 =48 共 =42

27 Exercise (cont.) P(cj) P(x1,x2,…,xn|cj)
P(AI)=10/20, P(Programming)=6/20 P(x1,x2,…,xn|cj) P(intelligence|AI)=4/48, P(planning|AI)=6/48, P(language|AI)=6/48, P(language|Programming)=8/42, P(proof|Programming)=3/42 若不做smoothing 並以相加(不取log)取代相乘的近似算法 AI: 10/20*(4/48+6/48+6/48)= ←歸類為AI類 Programming: 6/20*(8/42+3/42)=0.078

28 Naïve Bayes: Learning From training corpus, extract Vocabulary 先取出所有可能的詞 Calculate required P(cj) and P(xk | cj) 能算的先算 For each cj in C do docsj  subset of documents for which the target class is cj Concatenate all docsj into a single document Textj for each word xk in Vocabulary nk  number of occurrences of xk in Textj 調整項:Smoothing to avoid over-fitting (avoid zero)

29 Naïve Bayes: Classifying
positions  all word positions in current document which contain tokens found in Vocabulary Return cNB, where 取機率最大的類別 有出現的詞, 其機率相乘

30 Smoothing to avoid over-fitting
If a document contains a term x which never appears in the category c, the p(x|c) will always be zero, and the product will be zero, too. to add one smoothing to avoid zeros : Before After

31 Naive Bayes: Training 31

32 Naive Bayes: Testing 由相乘積轉成log相加 32

33 Naïve Bayes : discussion

34 Violation of NB Assumptions
Conditional independence 是否可以假設兩詞的出現為獨立事件? 與 VSM 的問題類似:向量空間之兩兩詞間是否為正交? Conclusion Naive Bayes can work well even though conditional independence assumptions are badly violated Because classification is about predicting the correct class and not about accurately estimating probabilities.

35 NB with Feature Selection (1)
Text collections have a large number of features 10,000 – 1,000,000 unique words … and more Feature (文件或類別的特徵) 若是選得好 Reduces training time Training time for some methods is quadratic or worse in the number of features Can improve generalization (performance) Eliminates noise features Avoids over-fitting

36 NB with Feature Selection (2)
2 ideas beyond TF-IDF 兩種評估好壞的指標 Hypothesis testing statistics: Are we confident that the value of one categorical variable is associated with the value of another Chi-square test 卡方檢定 Information theory: How much information does the value of one categorical variable give you about the value of another Mutual information They’re similar, but 2 measures confidence in association, (based on available statistics), while MI measures extent of association (assuming perfect knowledge of probabilities)

37 Naive Bayes is not so naive
Naive Naive Bayes has won some bakeoffs (e.g., KDD-CUP 97) More robust to nonrelevant features than some more complex learning methods More robust to concept drift (changing of definition of class over time) than some more complex learning methods Better than methods like decision trees when we have many equally important features A good dependable baseline for text classification (but not the best) Optimal if independence assumptions hold (never true for text, but true for some domains) Very fast : Learning with one pass over the data; testing linear in the number of attributes, and document collection size Low storage requirements 37

38 Naïve Bayes : evaluation

39 Evaluation on Reuters 39

40 Example: The Reuters collection
40

41 A Reuters document 41

42 Evaluating classification
Evaluation must be done on test data that are independent of the training data (usually a disjoint set of instances). It’s easy to get good performance on a test set that was available to the learner during training (e.g., just memorize the test set). Measures: Precision, recall, F1, classification accuracy 42

43 Precision P and recall R
↑also known as confusion matrix P = TP / ( TP + FP) R = TP / ( TP + FN) 43

44 A combined measure: F F1 allows us to trade off precision against recall. This is the harmonic mean of P and R: 44

45 Averaging: Micro vs. Macro
We now have an evaluation measure (F1) for one class. But we also want a single number that measures the aggregate performance over all classes in the collection. Macroaveraging Compute F1 for each of the C classes Average these C numbers Microaveraging Compute TP, FP, FN for each of the C classes Sum these C numbers (e.g., all TP to get aggregate TP) Compute F1 for aggregate TP, FP, FN 45

46 Exercise Class A Class B # of records 800 200 accuracy % 80% 60% macro average accuracy % (80%+60%)/2=70% micro average accuracy % (800*80%+200*60%)/( )=76% 若類別A及B的資料筆數相同,則macro avg.等於micro avg. micro avg.可視為依類別大小加權後的平均數

47 Naive Bayes vs. other methods
47

48 Alternative measurement
Classification accuracy : c/n n : the total number of test instances c : the number of test instances correctly classified by the system. 當各類別大小差異大時,Classification accuracy 易受影響 Ex. Class A有800筆、Class B有200筆,則全部猜A的 accuracy至少有80% 此時仍應檢視各類別的Precision及Recall

49 Case study : WebKB Experiment
Classify webpages from CS departments into: student, faculty, course,project Train on ~5,000 hand-labeled web pages Cornell, Washington, U.Texas, Wisconsin Crawl and classify a new site (CMU) for testing Results:

50 NB Model Comparison

51

52 Case study : Apache SpamAssassin
Naïve Bayes classifier for spam filtering Widely used in spam filters Classic Naive Bayes superior when appropriately used 有很多衍生版本 Many filters use NB classifiers But also many other things: black hole lists, etc. 同時混用很多其它技巧,如黑名單

53 Naïve Bayes on spam email

54 kNN : K Nearest Neighbors

55 Vector space classification
As before, the training set is a set of documents, each labeled with its class. In vector space classification, this set corresponds to a labeled set of points or vectors in the vector space. Premise 1: Documents in the same class form a contiguous region. 同類別文件在空間中較相近 Premise 2: Documents from different classes don’t overlap. We define lines, surfaces, hypersurfaces to divide regions. 不同的類別間可找出一個分割線或(超)平面 55

56 Classes in a Vector Space
Government Science Arts

57 Test Document = Government
Similarity hypothesis true in general? Government Science Arts

58 k Nearest Neighbor Classification
To classify document d into class c Define k-neighborhood N as k nearest neighbors of d Count number of documents i in N that belong to c Estimate P(c|d) as i/k Choose as class argmaxc P(c|d) [ = majority class] 訣竅 : 挑最近的 k 個鄰居出來統計 (投票),    看最多人屬哪一類,自己標成那一類

59 Example: k=5 (5NN) P(science| )? P(Government| )? Government Science
Arts

60 Nearest-Neighbor Learning Algorithm
Learning is just storing the representations of the training examples in D. Testing instance x: Compute similarity between x and all examples in D. 計算文件 x 與其它訓練文件的相似度 Assign x the category of the most similar example in D. 決定文件 x 的類別 Does not explicitly compute a generalization or category Also called: 此方法又稱為 Case-based learning Memory-based learning Lazy learning

61 kNN algorithm 61

62 Time complexity of kNN kNN test time proportional to the size of the training set The larger the training set, the longer it takes to classify a test document. kNN is inefficient for very large training sets. 62

63 kNN : discussion

64 kNN classification kNN classification is vector space classification method. It also is very simple and easy to implement. kNN is more accurate (in most cases) than Naive Bayes and others. If you need to get a pretty accurate classifier up and running in a short time . . . . . . and you don’t care about efficiency that much . . . . . . use kNN. 64

65 Discussion of kNN (1) No training necessary
But linear preprocessing of documents is as expensive as training Naive Bayes. We always preprocess the training set, so in reality training time of kNN is linear. kNN is very accurate if training set is large. kNN Is Close to Optimal (ref. Cover and Hart, Nearest neighbor pattern classification, 1967) But kNN can be very inaccurate if training set is small. kNN scores is hard to convert to probabilities 65

66 Discussion of kNN (2) Using only the closest example to determine the categorization is subject to errors due to: k 若只取一個易受下列影響 A single atypical example. 特例 Noise (i.e. error) in the category label of a single training example. 同類別中的雜訊 More robust alternative is to find the k most-similar examples and return the majority category of these k examples. 選 k 個再用投票多數是較穩當的做法 Value of k is typically odd to avoid ties; 3 and 5 are most common. 通常 k 要取單數, 常見的是3或5個 66

67 Nearest Neighbor with Inverted Index
Finding nearest neighbors requires a linear search through |D| documents in collection Determining k nearest neighbors is the same as determining the k best retrievals using the test document as a query to a database of training documents. 查詢結果前 k 名投票即可得 配合使用Inverted Index可提升實作效率

68 Support Vector Machine (SVM)

69 Support Vector Machine (1)
A kind of large-margin classifier From Intensive machine-learning research in the last two decades to improve classifier effectiveness Vector space based machine-learning method aiming to find a decision boundary between two classes that is maximally far from any point in the training data possibly discounting some points as outliers or noise

70 Support Vector Machine (2)
2-class training data decision boundary → linear separator criterion: being maximally far away from any data point → determines classifier margin linear separator position defined by support vectors 70

71 Why maximize the margin?
Points near decision surface → uncertain classification decisions A classifier with a large margin makes certainty classification decisions. - to reduce errors in measurement or doc. variation 71

72 Linear programming Find a,b,c, such that ax + by  c for red points
求出多項式的係數 即可決定該平面 Find a,b,c, such that ax + by  c for red points ax + by  c for green points.

73 Which Hyperplane? In general, lots of possible solutions for a,b,c.
可能有多組解 但是要挑哪個超平面 In general, lots of possible solutions for a,b,c.

74 Which Hyperplane? Lots of possible solutions for a,b,c.
Some methods find a separating hyperplane, but not the optimal one, while the other methods find an optimal separating hyperplane Which points should influence optimality? All points 用全部的點去求最佳解 Linear regression Only “difficult points” close to decision boundary 只用邊界附近的困難點 Support vector machines (SVM)

75 Which Hyperplane? If you have to place a fat separator between classes, you have less choices, and so the capacity of the model has been decreased

76 Formalize an SVM with algebra
Hyperplane : an n-dimensional generalization of a plane Decision hyperplane : given a normal vector w (weight vector) which is perpendicular to the hyperplane all points x on the hyperplane satisfy any point in two training set will individually satisfy wT x + b = 0 wT x + b = +1 wT x + b = -1

77 Geometric Margin ρ x r x' Distance from example to the separator is
Examples closest to the hyperplane are support vectors. Margin ρ of the separator is the width of separation between support vectors of classes. ρ x r x' Looking for distance r. Dotted line x’-x is perpendicular to decision boundary so parallel to w. Unit vector is w/|w|, so this one is rw/|w|. x’ = x – rw/|w|. X’ satisfies wx+b = 0. So wT(x –rw/|w|) = 0 Recall that |w| = sqrt(wTw). So, solving for r gives: r = y(wTx + b)/|w|

78 Linear Support Vector Machine
wTxa + b = 1 ρ Hyperplane wT x + b = 0 This implies: wT(xa–xb) = 2 ρ = ||xa–xb|| = 2/||w|| wTxb + b = -1 wT x + b = 0

79 Soft Margin Classification
If the training set is not linearly separable, slack variables ξi can be added to allow misclassification of difficult or noisy examples. Make it allow some errors. ξi ξj

80 SVM Resources SVM Light, Cornell University
libSVM, National Taiwan University Reference

81 Reference 支持向量機教學文件(中文版)
Support Vector Machines 簡介 Support Vector Machine 簡介

82 Classification with SVMs
Given a new point score its projection onto the hyperplane: compute score: wx + b set confidence threshold t. 計算離哪邊較近,並給予門檻值 Score > t: yes Score < -t: no Else: don’t know 7 5 3

83 SVM : discussion

84 Multiclass SVMs SVMs: inherently two-class classifiers.
Most common technique in practice: build |C| one-versus- rest classifiers (commonly referred to as “one-versus-all” or OVA classification), and choose the class which classifies the test data with greatest margin Another strategy: build a set of one-versus-one classifiers, and choose the class that is selected by the most classifiers. this involves building |C|(|C| − 1)/2 classifiers use binary decision tree or majority votes. 84

85 Exercise 用二元分類器模擬多分類效果,假設有政治、娛樂、科技三類
建立政治-其他、娛樂-其他、科技-其他三個分類器,再 取離得最近的當作分類結果;若有n個類別,需要建立n 個分類器 任二類建立一個分類器,最後採總積分制決定分類結果。 例如 政治-娛樂 歸類為政治 政治-科技 歸類為科技 娛樂-科技 歸類為科技 若採積分制,最後結果為科技>政治>娛樂 若有n個類別,需要建立 C 2 𝑛 個分類器 85

86 Non-linear SVMs Datasets that are linearly separable (with some noise) work out great: But what are we going to do if the dataset is just too hard? 分不開怎麼辦 How about … mapping data to a higher-dimensional space: 想辦法映射到不同空間 x x x2 x

87 Non-linear SVMs: Feature spaces
General idea: the original feature space can always be mapped to some higher-dimensional feature space where the training set is separable: Φ: x → φ(x)

88 SVM is good for Text Classification
Documents are zero along almost all axes Most document pairs are very far apart (i.e., not strictly orthogonal, but only share very common words and a few scattered others) 其實文件很多軸上的值是 0 ; 在空間軸上離很開 Virtually all document sets are separable, for most any classification. This is why linear classifiers are quite successful in this domain 文件混在一起的情況不多, 所以用Linear分類器, 在文件分類上很適合

89 Issues in Text Classification

90 (1) What kind of classifier to use
Document Classification is useful for many commercial applications What kind of classifier to use ? How much training data do you have? 需要準備多少訓練資料 None Very little Quite a lot A huge amount and its growing

91 If you have no labeled training data
Try hand-written rules solution 人工規則 If (Baseball OR Basketball) then categorize as Sport In practice, rules get a lot bigger than this 通常規則很多 With careful crafting (human tuning on development data) performance is high: 但是效果很好 Amount of work required is huge 但仍大量人工檢查與維護

92 If you have fairly little data ?
Naïve Bayes should do well in such circumstances (Ng and Jordan NIPS) 適合訓練用文件只有很少的時候 The practical answer is to get more labeled data as soon as you can How to let people be willing to label data for you ? 可思考如何運用網路上的資料 Ex. 信件分類、書籤、Social Tagging

93 If you have a huge amount of data ?
Great in theory for doing accurate classification But expensive methods like SVMs (train time) or kNN (test time) are quite impractical 運算量大的演算法不合用 Try Naïve Bayes again.

94 (2) Large and difficult categories
Easy for small number of well-separated categories Accurate classification over large sets of closely related classes is inherently difficult. Ex. Web directories (e.g. the Yahoo! Directory consists of over 200,000 categories or the Open Directory Project) Ex. library classification schemes (Library of Congress) Classifier combination is a useful technique Voting of multiple classifiers Use a hybrid automatic/manual solution

95 (3) Other techniques Try differentially weighting contributions from different document zones: Upweighting title words helps (Cohen & Singer 1996) 提高標題權重 Upweighting the first sentence of each paragraph helps (Murata, 1999) 提高每段的第一句權重 Upweighting sentences that contain title words helps (Ko et al, 2002) 提高包含標題之句子的權重 Summarization as feature selection for text categorization (Kolcz, Prabakarmurthi, and Kolita, CIKM 2001) 先做自動摘要

96 (4) Problem of Concept drift
Categories change over time 類別是會隨時間變的 Example: “president of the united states” 1999: clinton is great feature 2002: clinton is bad feature 已經不是總統了 One measure of a text classification system is how well it protects against concept drift. 多久需要檢視訓練資料,並重新訓練?

97 Dumais et al. 1998 : Reuters - Accuracy
97

98 Results for Kernels (Joachims 1998)
NB should be over 80!!

99 Yang & Liu: SVM vs. Other Methods

100 Text Classification : conclusion
Choose a approach Do no classification 不分類 Do it all manually 人工分類 Do it all with an automatic classifier 全自動分類 Mistakes have a cost 要挑出錯也要成本 Do it with a combination of automatic classification and manual review of uncertain/difficult/“new” cases Commonly the last method is most cost efficient and is adopted

101 Discussions


Download ppt "Lecture 3 : Classification (1)"

Similar presentations


Ads by Google