05 classification 1 decision tree and rule based classification
description
Transcript of 05 classification 1 decision tree and rule based classification
1
สอนโดย ดร.หทั�ยร�ตน เกต�มณี�ชั�ยร�ตน ภาควิ�ชัาการจั�ดการเทัคโนโลย�การผล�ตและสารสนเทัศ
บทัทั�! 5: การจั"าแนกประเภทัข้%อม&ล 1 (Classification 1)
2
Classification 1 Basic Concept Decision Tree Rule-Base Classification
3
การจั"าแนกประเภทัข้%อม&ล (Classification)
เป็�นงานทำาเหมื�องข้�อมื�ลป็ระเภทำหน��ง โดยมื�ตั�วอย�างในชุ!ดข้�อมื�ลสอน (training set) ทำ��ใชุ� จะมื�คุ!ณล�กษณะหน��งซึ่��งบอกคุาป็ระเภทำข้องตั�วอย�างน�*น เราเร�ยกคุ�าคุ!ณล�กษณะน�*ว�า ฉลากบอกป็ระเภทำ (class label) ซึ่��งเป็�นคุ�าข้�อมื�ลแบบ categorical
การป็ระย!กตั-ใชุ�งานการจาแนกป็ระเภทำข้�อมื�ล มื�กพบใน การอน!มื�ตั/คุาข้อมื�บ�ตัรเคุรด/ตั (Credit Approval) การทำาตัลาดล�กคุ�ากล!�มืเป็0าหมืาย (Target marketing) การตัรวจว/น/จฉ�ย (Medical Diagnosis) การว/เคุราะห-ป็ระส/ทำธิ/ผลการร�กษา (Treatment
effectiveness analysis)
4
Classification—ม� 2 ข้�'นตอนหล�ก ค(อ
การสร%างแบบจั"าลอง (Model construction): เซึ่ตัข้องตั�วอย�างทำ��ใชุ�ในการสร�างแบบจาลอง เร�ยกว�า ชุ!ด
ข้�อมื�ลสอน ตั�วอย�างแตั�ละตั�วจะมื�คุ!ณล�กษณะหน��ง ซึ่��งบอกคุ�าป็ระเภทำ
ทำ��กาหนดไว�ล�วงหน�า แบบจาลองทำ��สร�างข้�*น แสดงผลล�พธิ-การเร�ยนร� �การ
จาแนกป็ระเภทำในร�ป็แบบ กฎการจาแนกป็ระเภทำ (classification rules) ตั�นไมื�ตั�ดส/นใจ (decision trees) หร�อส�ตัรทำางคุณ/ตัศาสตัร- ตั�างๆ
การน"าแบบจั"าลองทั�!ได%ไปใชั% (Model usage): เพ��อการจาแนกป็ระเภทำตั�วอย�างในอนาคุตั โดยจะตั�องมื�
การป็ระมืาณคุ�าคุวามืแมื�นยา (accuracy) ข้องแบบจาลองทำ��ได�เส�ยก�อนการนาไป็ใชุ� โดย
เป็ร�ยบเทำ�ยบคุ�าฉลากป็ระเภทำทำ��ทำราบล�วงหน�าข้องตั�วอย�างในชุ!ดทำดสอบ (test set) ก�บคุ�าผลล�พธิ-การจาแนกป็ระเภทำทำ��ได�จากแบบจาลอง
อ�ตัราคุวามืแมื�นยา (accuracy rate) คุานวณได�จากเป็อร-เซึ่7นตั-ผลการจาแนกป็ระเภทำได�อย�างถู�กตั�องข้องแบบจาลองทำ��สร�างข้�*น
ชุ!ดข้�อมื�ลทำดสอบเป็�นอ/สระไมื�ข้�*นตั�อชุ!ดข้�อมื�ลสอน มื/เชุ�นน�*นจะเก/ด overfitting
5
ข้�'นตอนทั�! 1: Model Construction
TrainingData
NAME RANK YEARS TENUREDMike Assistant Prof 3 noMary Assistant Prof 7 yesBill Professor 2 yesJim Associate Prof 7 yesDave Assistant Prof 6 noAnne Associate Prof 3 no
ClassificationAlgorithms
IF rank = ‘professor’OR (rank = ‘Assistant Prof’and years > 6)THEN tenured = ‘yes’
Classifier(Model)
6
ข้�'นตอนทั�! 2: Use the Model in Prediction
Classifier
TestingData
NAME RANK YEARS TENUREDTom Assistant Prof 2 noMerlisa Associate Prof 7 noGeorge Professor 5 yesJoseph Assistant Prof 7 yes
Unseen Data
(Jeff, Professor, 4)
Tenured?
7
Over fitting
คุ�อ ป็รากฎการทำ��แบบจาลองหร�อตั�วจาแนกป็ระเภทำทำ��ได�มื�คุวามืพอด�เก/นไป็ก�บชุ!ดข้�อมื�ลสอน กล�าวคุ�อ จะให�ผลการจาแนกป็ระเภทำทำ��ถู�กตั�องสาหร�บตั�วอย�างในชุ!ดข้�อมื�ลสอน แตั�ใชุ�ไมื�ได�ด�ก�บกรณ�ตั�วอย�างทำ��วไป็
ป็รากฎการณ-ด�งกล�าวเก/ดข้�*นเน��องจาก เซึ่ตัข้�อมื�ลมื�ข้นาดเล7กเก/นไป็ มื�คุวามืผ/ดป็กตั/ข้องข้�อมื�ลทำ��ใชุ�สอน
ต�วิอย,างเชั,น ถ้%าทั�กคนทั�!ชั(!อ “John” ในชั�ดข้%อม&ลสอนเป.นล&กค%าทั�!สร%างก"าไรให%ก�บบร�ษั�ทั ต�วิจั"าแนกประเภทัข้%อม&ลอาจัให%ผลสร�ปทั�!ผ�ดวิ,า ล&กค%าใดๆ ก1ตามทั�!ชั(!อ John จัะเป.นล&กค%าทั�!สร%างก"าไรให%ก�บบร�ษั�ทั
8
Supervised vs. Unsupervised Learning
Supervised learning (classification) เราทำราบคุ�าป็ระเภทำข้องตั�วอย�างในชุ!ดข้�อมื�ลสอน
ล�วงหน�า การจาแนกป็ระเภทำตั�วอย�างใหมื�ข้�*นอย��ก�บแบบจาลองทำ��สร�างจากชุ!ดข้�อมื�ลสอน การจาแนกป็ระเภทำเป็�นตั�วอย�างการเร�ยนร� �แบบ supervised
Unsupervised learning (clustering) เราไมื�ทำราบคุ�าป็ระเภทำข้องตั�วอย�างในชุ!ดข้�อมื�ลสอน
ล�วงหน�า แตั�จะพยายามืเร�ยนร� �เพ��อระบ!ป็ระเภทำหร�อกล!�มืทำ��ซึ่�อนอย��ในข้�อมื�ล ตั�วอย�างการเร�ยนร� �แบบ unsupervised ได�แก� การจ�ดกล!�มื
9
การเตร�ยมข้%อม&ลส"าหร�บการจั"าแนกประเภทัข้%อม&ล การทำาคุวามืสะอาดข้�อมื�ล (Data cleaning)
เพ��อจ�ดการก�บข้�อมื�ลทำ��ส�ญหาย (missing) และลดข้�อมื�ลรบกวน (noise) หร�อข้�อมื�ลทำ��มื�คุ�าผ/ดป็กตั/ (outlier)
การว/เคุราะห-คุวามืเก��ยวข้�องข้องข้�อมื�ล (Relevance analysis) คุ�ดเล�อกคุ!ณล�กษณะ (feature selection) ทำ��
เก��ยวข้�องก�บงานทำาเหมื�องข้�อมื�ล และกาจ�ดคุ!ณล�กษณะทำ��ซึ่*าซึ่�อน หร�อทำ��ไมื�เก��ยวออกไป็
การแป็ลงข้�อมื�ล (Data transformation) ได�แก� การนอร-มือลไลซึ่-ข้�อมื�ล (Normalization)
เพ��อให�การกระจายข้องข้�อมื�ลอย��ในชุ�วงทำ��กาหนด หร�อการทำาข้�อมื�ลให�อย��ในร�ป็ทำ��วไป็ (Generalization)
10
วิ�ธี�การจั"าแนกประเภทัข้%อม&ล ต%นไม%ต�ดส�นใจั (Decision tree) การเร�ยนร� �แบบเบย- (Bayesian
Classification) ข้�ายงานป็ระสาทำเทำ�ยมื (Neural Networks)
11
ต%นไม%ต�ดส�นใจั (Decision Tree) การเร�ยนร� �ข้องตั�นไมื�ตั�ดส/นใจ (Decision Tree) เป็�นการ
เร�ยนร� �โดยการจาแนกป็ระเภทำ (Classification) ข้�อมื�ลออกเป็�นกล!�มื (class) ตั�างๆ โดยใชุ�คุ!ณล�กษณะ (attribute) ข้�อมื�ลในการจาแนกป็ระเภทำ ตั�นไมื�ตั�ดส/นใจทำ��ได�จากการเร�ยนร� �ทำาให�ทำราบว�า คุ!ณล�กษณะใดเป็�นตั�วกาหนดการจาแนกป็ระเภทำ และคุ!ณล�กษณะแตั�ละตั�วมื�คุวามืสาคุ�ญมืากน�อยตั�างก�นอย�างไร
เพราะฉะน�*น การจาแนกป็ระเภทำมื�ป็ระโยชุน-ชุ�วยให�ผ��สามืารถูว/เคุราะห-ข้�อมื�ลและตั�ดส/นใจได�ถู�กตั�องย/�งข้�*น
12
Decision Trees ชุ!ดข้�อมื�ล
age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no
attribute
Attribute value
13
ส,วินประกอบข้องผลล�พธี ข้องการเร�ยนร&%ต%นไม%ต�ดส�นใจั:
student? credit rating?
no
yes fairexcellent
<=30 >40
no noyes yes
yes
30..40
age? Internal node
Branch
Leaf node
Splitting Attribute
14
โนดภายใน (internal node) คุ�อ คุ!ณล�กษณะตั�าง ๆ ข้องข้�อมื�ล ซึ่��งเมื��อข้�อมื�ลใดๆ ตักลงมืาทำ��โนด จะใชุ�คุ!ณล�กษณะน�*เป็�นตั�วตั�ดส/นใจว�าข้�อมื�ลจะไป็ในทำ/ศทำางใด โดยโนดภายในทำ��เป็�นจ!ดเร/�มืตั�นข้องตั�นไมื� เร�ยกว�า โนดราก
ก�!ง (branch, link) เป็�นคุ�าข้องคุ!ณล�กษณะในโนดภายในทำ��แตักก/�งน�*ออกมืา ซึ่��งโนดภายในจะแตักก/�งเป็�นจานวนเทำ�าก�บจานวนคุ�าข้องคุ!ณล�กษณะในโนดภายในน�*น
โนดใบ (leaf node) คุ�อ กล!�มืตั�าง ๆ ซึ่��งเป็�นผลล�พธิ-ในการจาแนกป็ระเภทำข้�อมื�ล
ส,วินประกอบข้องผลล�พธี ข้องการเร�ยนร&%ต%นไม%ต�ดส�นใจั:
15
ทั"าไมถ้4งต%องใชั% Decision Tree?
เป็�นเทำคุน/คุทำ��ให�ผลเร7วเมื��อเทำ�ยบก�บเทำคุน/คุอ��น ผลล�พธิ-ทำ��ได�สามืารถูนาไป็ใชุ�ได�งาน และสามืารถู
แป็ลงเป็�นกฎได� สามืารถูนาไป็ป็ระย!กตั-ใชุ�ก�บการคุ�นหาข้�อมื�ล SQL ให�คุวามืแมื�นยาส�ง
16
ต�วิอย,างข้องประเภทัการพ�จัารณีา
Add Your Text
Nominal Attribute
Ordinal Attribute
Continuous Attribute
accept
Yes No
color
Red,Blue Green
color
Red blue Green
level
Low,medium high
level
lowmedium
high
income
< 50 >= 50
income
< 10
[10, 20) [20, 30)
>= 30
17
ป5จัจั�ยทั�!ใชั%ในการพ�จัารณีา Attribute Type
Nominal แบ�งเป็�นกล!�มื Ordinal แบ�งเป็�นกล!�มืโดยมื�การเร�ยงลาด�บข้องกล!�มื Continuous เป็�นข้�อมื�ลมื�คุวามืตั�อเน��องก�น
จานวนข้องทำางทำ��จะแยก 2 way split (Binary split) Multi-way split
18
ข้�'นตอนวิ�ธี�ทั"า Decision Tree ตั�นไมื�ตั�ดส/นใจสร�างโดยว/ธิ�แบบ top-down
recursive เร/�มืตั�นด�วยนาตั�วอย�างการสอน มืาสร�างเป็�นราก Attribute คุวรอย��ในร�ป็ข้อง Categorical คุ�อ ข้�อมื�ล
ชุน/ดกล!�มื หากเป็�นข้�อมื�ลทำ��อย��ในร�ป็ Continuous หร�อ Numeric เป็�นข้�อมื�ลมื�คุวามืตั�อเน��องก�น คุวรทำาแบ�งข้�อมื�ลให�เป็�นกล!�มืก�อน
การสร�างตั�นไมื�ตั�ดส/นใจมื�พ�*นฐานมืาจากว/ธิ�การเล�อก Attribute
เมื��อไหร�ถู�งจะหย!ดการสร�างตั�นไมื� เมื��อทำ!กข้�อมื�ลใน node น�*นเป็�น Class เด�ยวก�น เมื��อทำ!กข้�อมื�ลใน node น�*นมื�คุ�าข้อง Attribute
เหมื�อนก�น
19
Attribute ใดเป.นต�วิจั"าแนกประเภทัทั�!ด�ทั�!ส�ด?
age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no
Attribute Selection- age?- income?- student?- credit_rating?
20
การเล(อก Best Split
C0 : 4C1 : 6
C0 : 5C1 : 5
Non- Homogeneousข้%อม&ลม�ล�กษัณีะไม,เหม(อนก�น
C0 : 9C1 : 1
C0 : 0C1 : 10
Homogeneousข้%อม&ลม�ล�กษัณีะเหม(อนก�น
Input Data : 9 record of class 0,
11 record of class 1
Node ทั�!ม�ควิามเป.น Homogeneous มากกวิ,า จัะม�ค�ณีสมบ�ต�แยกข้%อม&ลได%ด�กวิ,า
? ?
21
Measure ทั�!ใชั%ในการเล(อก Attribute:
**Gini Index ( ค,าทั�!บ,งบอกวิ,า attribute สมควิรน"ามาใชั%เป.นค�ณีล�กษัณีะในการแบ,ง )
Entropy ( การค"านวิณีหาค,าควิามย�,งเหย�งข้องข้%อม&ลกล�,มหน4!ง ) ใชั%อ�ลกอร4ทั4ม ชั(!อวิ,า ID3
Misclassification error (การค"านวิณีควิามผ�ดพลาดทั�!เก�ดก�บโนด t)
Classification error(ti) = 1 – Max[p(ti)]
Gini(ti) = 1 -å [p(ti)]2
i=1
Entropy(ti) = 1 -å [p(ti)] log2p(ti)
i=0
N
N
22
การเล(อก Attribute ข้�'นตอนทั�! 1 ถู�าข้�อมื�ลนาเข้�า T มื�การแบ�งป็ระเภทำคุลาสทำ�*งหมืด
n classes, Gini index, Gini(T) คุ�อ
โดยทำ�� pj เป็�นคุวามืถู��ข้องคุลาส j ในข้�อมื�ลนาเข้�า T
ข้�'นตอนทั�! 2 ถู�าข้�อมื�ลนาเข้�า T แบ�งออกเป็�น 2 กล!�มื คุ�อ T1 and T2 และมื�คุวามืถู��โดยรวมื N1 and N2 ตัามืลาด�บ, Ginispit(T) is defined as
Attribute ทำ��ทำาให� Ginisplit(T) น�อยทำ��ส!ดจะทำาเป็�นคุ�าทำ��ด�ทำ��ส!ด
)()()( 22
11 tgini
NN
tginiNNTGinisplit
Gini(ti) = 1 -å [p(ti)]2
i=1
n
23
Gini Index
ต�วิอย,างการค"านวิณี
C0 : 8C1 : 2
C0 : 0C1 : 4
yes No
t1 t2
N1
N2
C0
8 0
C1
2 4Gini(t1) = 1 – (8/10)2 – (2/10)2 = 0.32
Gini(t2) = 1 – (0/4)2 – (4/4)2 = 0
Ginisplit (T) = (10/14)(0.32) + (4/14)(0)
= 0.2286
Owner
24
ต�วิอย,างการเล(อก Attribute
age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no
25
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute age ก�อนคุานวณตัามื Gini Index ได�ด�งตั�อไป็น�*
Gini(t1) = 1 – (2/5)2 – (3/5)2 = 0.48
Gini(t2) = 1 – (4/4)2 – (0/4)2 = 0
Ginisplit (T) = (5/14)(0.48) + (4/14)(0)+(5/14)(0.48) = 0.343
C0 : 2C1 : 3
C0 : 3C1 : 2
<=30
t1
31..40 >40
C0 : 4C1 : 0
t2 t3
ageC0 = yesC1 = no
Gini(t3) = 1 – (3/5)2 – (2/5)2 = 0.48
N1 N2 N3
C0 2 4 3
C1 3 0 2
26
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute income ก�อนคุานวณตัามื Gini Indexได�ด�งตั�อไป็น�*
Gini(t1) = 1 – (3/4)2 – (1/1)2 = 0.56
Gini(t2) = 1 – (4/6)2 – (2/6)2 = 0.44
Ginisplit (T) = (4/14)(0.56) + (6/14)(0.44)+(4/14)(0.5) = 0.491
C0 : 3C1 : 1
C0 : 2C1 : 2
low
t1
highmediumC0 : 4C1 : 2
t2 t3
income
C0 = yesC1 = no
Gini(t3) = 1 – (2/4)2 – (2/4)2 = 0.5
N1 N2 N3
C0 3 4 2
C1 1 2 2
27
ตั�วอย�าง การเล�อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute student ก�อนคุานวณตัามื Gini Indexได�ด�งตั�อไป็น�*
Gini(t1) = 1 – (6/7)2 – (1/7)2 = 0.24
Gini(t2) = 1 – (3/7)2 – (4/7)2 = 0.49
Ginisplit (T) = (7/14)(0.24) + (7/14)(0.49) = 0.365
C0 : 6C1 : 1
C0 : 3C1 : 4
yes
t1
no
t3
student
C0 = yesC1 = no
N1 N2
C0 6 3
C1 1 4
28
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute income ก�อนคุานวณตัามื Gini Indexได�ด�งตั�อไป็น�*
Gini(t1) = 1 – (6/8)2 – (2/8)2 = 0.37
Gini(t2) = 1 – (3/6)2 – (3/6)2 = 0.5
Ginisplit (T) = (8/14)(0.37) + (6/14)(0.5) = 0.426
C0 : 6C1 : 2
C0 : 3C1 : 3
fair
t1
excellent
t2
credit
C0 = yesC1 = no
N1 N2
C0 6 3
C1 2 3
29
ต�วิอย,าง การเล(อก Attribute
จากการคุานวณ เล�อก Attribute ทำ��มื�คุ�า Ginisplit (T) น�อยทำ��ส!ด
Ginisplit (age) = (5/14)(0.48) + (4/14)(0)+(5/14)(0.48)= 0.343
Ginisplit (income) = (4/14)(0.56) + (6/14)(0.44)+(4/14)(0.5) = 0.491
Ginisplit (student) = (7/14)(0.24) + (7/14)(0.49) = 0.365
Ginisplit (Credit) = (8/14)(0.37) + (6/14)(0.5) = 0.426
ต�วิอย,าง การเล(อก Attributeจากการพ/จารณาคุ�า Ginisplit (T) เล�อก age เป็�น Attribute ทำ��ใชุ�พ/จารณาเป็�น Root
C0 : 2C1 : 3
C0 : 3C1 : 2
<=30
t1
31..40 >40
C0 : 4C1 : 0
t2 t3
age
C0 : 2C1 : 3
C0 : 3C1 : 2
<=30
t1
31..40 >40
t3
age
yes
select attribute??
30
31
ต�วิอย,าง การเล(อก Attribute
age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no
เล�อก Attribute ตั�อโดยพ/จารณาเฉพาะก/�งข้อง age <=30 ก�อน โดยเล�อกAttribute ทำ��เหล�อคุ�อ income, student และ credit
32
ต�วิอย,าง การเล(อก Attribute
จากตัารางข้�างตั�น ลองเล�อก Attribute income เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวณตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 C0 : 3
C1 : 2
<=30 31..40 >40
age
yesincome
C0 : ?C1 : ?
C0 : ?C1 : ?
low
t1
highmediumC0 : ?C1 : ?
t2 t3
33
ต�วิอย,าง การเล(อก Attribute
จากตัารางข้�างตั�น ลองเล�อก Attribute income เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวนตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 C0 : 3
C1 : 2
<=30 31..40 >40
age
yesincome
C0 : 1C1 : 0
C0 : 0C1 : 2
low
t1
highmediumC0 : 1C1 : 1
t2 t3
34
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute income เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวนตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 income
C0 : 1C1 : 0
C0 : 0C1 : 2
low
t1
highmediumC0 :1
C1 : 1
t2 t3
Gini(t1) = 1 – (1/1)2 – (0/1)2 = 0
Gini(t2) = 1 – (1/2)2 – (1/2)2 = 0.5
Ginisplit (T) = (1/5)(0) + (2/5)(0.5)+(2/5)(0)= 0.2
C0 = yesC1 = no
Gini(t3) = 1 – (0/2)2 – (2/2)2 = 0
N1 N2 N3
C0 1 1 0
C1 0 1 2
35
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute student เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวนตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 C0 : 3
C1 : 2
<=30 31..40 >40
age
yesstudent
C0 : 2C1 : 0
C0 : 0C1 : 3
yes
t1
no
t3
36
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute student เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวนตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 student
C0 : 2C1 : 0
C0 : 0C1 : 3
yes
t1
no
t3
Gini(t1) = 1 – (2/2)2 – (0/2)2 = 0
Gini(t2) = 1 – (0/3)2 – (3/3)2 = 0
Ginisplit (T) = (2/5)(0) + (3/5)(0) = 0
C0 = yesC1 = no
N1 N2
C0 2 0
C1 0 3
37
ตั�วอย�าง การเล�อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute credit เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวนตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 C0 : 3
C1 : 2
<=30 31..40 >40
age
yescredit
C0 : 1C1 : 2
C0 : 1C1 : 1
fair
t1
excellent
t2
38
ต�วิอย,าง การเล(อก Attributeจากตัารางข้�างตั�น ลองเล�อก Attribute credit เป็�นตั�วพ/จารณาก/�งข้อง age <=30 ก�อน คุานวนตัามื Gini Indexได�ด�งตั�อไป็น�*
C0 : 2C1 : 3 credit
C0 : 1C1 : 2
C0 : 1C1 : 1
fair
t1
excellent
t2
Gini(t1) = 1 – (1/3)2 – (2/3)2 = 0.44
Gini(t2) = 1 – (1/2)2 – (1/2)2 = 0.5 Ginisplit (T) = (3/5)(0.44) + (2/5)(0.5) = 0.464
C0 = yesC1 = no
N1 N2
C0 1 1
C1 2 1
39
จากการพ/จารณาคุ�า Ginisplit (age,?) เล�อก Attribute ไหนทำ��ใชุ�พ/จารณาเป็�น Attribute ถู�ดไป็
C0 : 2C1 : 3 income
C0 : 1C1 : 0
C0 : 0C1 : 2
low
t1
highmediumC0 : 0C1 : 1
t2 t3
C0 : 2C1 : 3 student
C0 : 2C1 : 0
C0 : 0C1 : 3
yes
t1
no
t3
C0 : 2C1 : 3 credit
C0 : 1C1 : 2
C0 : 1C1 : 1
fair
t1
excellent
t2Ginisplit (T) = 0.464
Ginisplit (T) = 0.2 Ginisplit (T) = 0
40
จากการพ/จารณา Attribute income และ student ซึ่��งมื�คุ�า Gini เทำ�าก�นแตั� student มื� จานวนก/�งน�อยกว�า จ�งเล�อก Attribute student
C0 : 2C1 : 3 C0 : 3
C1 : 2
<=30 31..40 >40
age
yesstudent
C0 : 2C1 : 0
C0 : 0C1 : 3
yes
t1
no
t3
yes no
41
จากการพ/จารณา Attribute income และ student ซึ่��งมื�คุ�า Gini เทำ�าก�นแตั� student มื�จานวนก/�งน�อยกว�า จ�งเล�อก Attribute student
C0 : 3C1 : 2
<=30 31..40 >40
age
yesstudent
yes no
yes no
ตั�อไป็ Select Attribute เพ��อเป็�นตั�วพ/จารณาก/�งข้อง age > 40
42
ต�วิอย,าง การเล(อก Attribute
age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no
เล�อก Attribute ตั�อโดยพ/จารณาเฉพาะก/�งข้อง age > 40 เป็�นก/�งส!ดทำ�ายโดยเล�อก Attribute ทำ��เหล�อคุ�อ income และ credit
43
C0 : 3C1 : 2
<=30 31..40 >40
age
yesstudent
yes no
yes no
income
C0 : 1C1 : 1
C0 : 0C1 :0
low
t1
highmediumC0 : 2C1 : 1
t2 t3
ต�วิอย,าง การเล(อก Attribute
Gini(t1) = 1 – (1/2)2 – (1/2)2 = 0.5
Gini(t2) = 1 – (2/3)2 – (1/3)2 = 0.67
Ginisplit (T) = (2/5)(0.5) + (3/5)(0.67)+ (0/5)(1) = 0.602
Gini(t2) = 1 – (0/0)2 – (0/0)2 = 1
44
C0 : 3C1 : 2
<=30 31..40 >40
age
yesstudent
yes no
yes no
credit
C0 : 3C1 : 0
C0 : 0C1 : 2
fair
t1
excellent
t2
ต�วิอย,าง การเล(อก Attribute
Gini(t1) = 1 – (3/3)2 – (0/3)2 = 0
Gini(t2) = 1 – (0/2)2 – (2/2)2 = 0 Ginisplit (T) = (3/5)(0) + (2/5)(0) = 0
45
<=30 31..40 >40
age
yesstudent
yes no
yes no
credit
fair excellent
ผลล�พธี จัากการเร�ยนร&%ต%นไม%ต�ดส�น
yes no
46
age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no
<=30 31..40 >40
age
yesstudent
yes no
yes no
credit
fair excellent
yes no
Age Income Student Credit_rate Buy computer
<=30 low yes fair ?
การน"าไปใชั%ส"าหร�บต%นไม%ต�ดส�นใจั
47
กฎข้�อทำ�� 1:: If (age<=30) and (student = yes)Then buy computer = yes
กฎข้�อทำ�� 2:: If (age<=30) and (student = no)Then buy computer = no
กฎข้�อทำ�� 3:: If (age> 30 and age < 40) Then buy computer = yes
กฎข้�อทำ�� 4:: If (age > 40) and (credit = fair)Then buy computer = yes
กฎข้�อทำ�� 5:: If (age > 40) and (credit = excellent)Then buy computer = no
การน"าไปใชั%ส"าหร�บต%นไม%ต�ดส�นใจั (แปลงเป.น กฎ)
48
Overfitting in Decision Treeถู�าเพ/�มืข้�อมื�ลเข้�าไป็ training example #15:
Age Income Student Credit_rate Buy computer
<=30 high yes fair no
What effect on this Tree?
<=30 31..40 >40
age
yesstudent
yes no
yes no
credit
fair excellent
yes no
49
การหล�กเล�!ยงเหต�การณี Overfitting
Overfitting หากมื�หลายก/�งอาจจะทำาให�การจาแนกป็ระเภทำข้�อมื�ล
ผ/ดเพ�*ยนไป็ และมื�ผลทำาให�เก/ดข้�อมื�ลรบกวนหร�อข้�อมื�ลผ/ดป็กตั/ได�
คุวามืถู�กตั�องน�อยสาหร�บตั�วอย�างทำ��ไมื�เคุยเห7นมืาก�อน (unseen samples)
มื� 2 แนวทำาง Prepruning Postpruning
50
ตั�ดเล7มืก/�งไมื�ข้ณะเร�ยนร� � เก/ดข้�*นข้ณะทำ��กาล�งเร�ยนร� �และสร�างตั�นไมื�ตั�ดส/นใจโดยด�ว�าถู�าโนดล�กทำ��สร�างน�*นมื�คุวามืผ/ดพลาดในการจาแนกป็ระเภทำกล!�มืมืากกว�าคุวามืผ/ดพลาดข้องกล!�มืทำ��มื�อย��เด/มืเมื��อย�งไมื�ได�จาแนกป็ระเภทำก7ไมื�จาเป็�นทำ��จะตั�องสร�างโนดน�*นและตั�ดตั�นไมื�ย�อยทำ��มื�โดนน�*นเป็�นรากออกไป็
Preprunning
51
Postpruning
การตั�ดเล7มืหล�งการเร�ยนร� �จะเส�ยเวลาในการคุานวณมืากกว�าการตั�ดเล7มืข้ณะเร�ยนร� � แตั�มื�กจะให�ตั�นไมื�ตั�ดส/นใจทำ��น�าเชุ��อถู�อได�มืากกว�า ด�งน�*น จ�งใชุ�คุ�าคุวามืผ/ดพลาด (Error-based prunning) ในการรวมืโนดย�อยทำ��ตั�องการตั�ดด�ก�อน เพ��อด�ว�าในโนดน�*นๆ จะไมื�ทำาให�คุ�าคุวามืผ/ดพลาดเพ/�มืข้�*น จากน�*นจ�งคุ�อยตั�ดออกไป็
52
ค�ณีภาพข้องต%นไม%ต�ดส�นใจั คุวามืแมื�นยา (Accuracy)
คุ�อ การทำ��ตั�นไมื�ตั�ดส/นใจซึ่��งเป็�นผลล�พธิ-จากกระบวนการเร�ยนร� �สามืารถูทำานายกล!�มืข้องตั�วอย�างใหมื�ได�อย�างถู�กตั�อง
คุวามืซึ่�บซึ่�อน (Complexity)ว�ดได�จากข้นาดข้องตั�นไมื� และจานวนโนดใบ
คุวามืเร7ว (Speed)ว�ดจากคุ�าใชุ�จ�ายการคุานวณในการสร�างและใชุ�ตั�นไมื�ตั�ดส/นใจในการทำานายกล!�มืข้องข้�อมื�ลใหมื�
รองร�บข้�อมื�ลข้นาดใหญ� (Scalability) คุวามืาสามืารถูในการจาแนกป็ระเภทำข้�อมื�ลข้นาดใหญ�
(จานวนเป็�นล�าน) ทำ��ป็ระกอบด�วยจานวนคุ!ณล�กษณะเป็�นร�อยๆ ตั�ว ด�วยคุวามืเร7วทำ��ร �บได�
53
Holdout Method
เป็�นว/ธิ�เหมืาะก�บชุ!ดข้�อมื�ลข้นาดใหญ� ตั�วอย�างในชุ!ดข้�อมื�ลจะถู�กแบ�งออกเป็�น 2 ส�วน แบบส!�มื ด�วยอ�ตัราส�วนข้นาดข้องข้�อมื�ลสอนเทำ�าก�บ 2/3 และข้นาดข้�อมื�ลทำดสอบเทำ�าก�บ 1/3 ใชุ�ชุ!ดข้�อมื�ลสอนในการสร�างแบบจาลองการทำานาย และตัรวจสอบคุวามืถู�กตั�องในการจาแนกป็ระเภทำข้�อมื�ลใหมื�หร�อทำ��ไมื�เคุยเห7นมืาก�อนด�วยชุ!ดข้�อมื�ลทำดสอบ คุ�าคุวามืแมื�นยาคุานวณได�จากอ�ตัราส�วนระหว�างจานวนตั�วอย�างในชุ!ดข้�อมื�ลทำดสอบทำ��ทำานายกล!�มืได�ก�บจานวนตั�วอย�างทำ�*งหมืดในชุ!ดข้�อมื�ลทำดสอบ
N
i
i
NError
1
1 hit
0 Miss
N = No. Test Data Set
การประเม�นค,าควิามแม,นย"า
54
Data Set
Training set Test set
การประเม�นค,าควิามแม,นย"า
แบ,งเป.น 2/3 แบ,งเป.น 1/3
55
K-fold Cross validation
เหมืาะสาหร�บชุ!ดข้�อมื�ลจานวนไมื�มืาก สมืมืตั/ว�าข้นาดข้องข้�อมื�ลเทำ�าก�บ N ตั�วอย�างในชุ!ดข้�อมื�ลจะถู�กแบ�งออกเป็�น k ส�วน โดยแตั�ละชุ!ดข้�อมื�ลจะมื�ข้นาด N/k ว/ธิ�น�*จะเร�ยนร� �ด�วยชุ!ดข้�อมื�ลสอนและตัรวจสอบคุวามืถู�กตั�งในการจาแนกป็ระเภทำด�วย ชุ!ดข้�อมื�ลทำ�*งหมืด k รอบ โดยทำ��
รอบทำ�� i จะใชุ�ชุ!ดข้�อมื�ลทำดสอบชุ!ดทำ�� i เป็�นชุ!ดข้�อมื�ลทำดสอบ ชุ!ดทำ��เหล�อเป็�นชุ!ดข้�อมื�ลสอน เป็�นตั�น
ด�งน�*นคุ�าคุวามืแมื�นยาจะคุานวณได�จากอ�ตัราส�วนระหว�างจานวนตั�วอย�างในชุ!ดหารด�วยจานวนทำ�*งหมืด k รอบ
fold
i
fold
j
ij
TotalError
1 1
1 hit
0 Miss
N = No. Test Data Set
การประเม�นค,าควิามแม,นย"า
56
การประเม�นค,าควิามแม,นย"า
K-Fold Cross Validation
1 2 3 4 5
Data Set
1 2 4 53
21 3 4 5
51 3 42
#1
#2
#5
.
.
.
57
การประเม�นค,าควิามแม,นย"า Confusion Matrixคุ�อการป็ระเมื/นผลล�พธิ-การทำานาย (หร�อผลล�พธิ-จากโป็รแกรมื) เป็ร�ยบเทำ�ยบก�บผลล�พธิ-จร/งๆ True Positive (TP) คุ�อ ส/�งทำ��โป็รแกรมืทำานายว�าจัร�ง และผลการทำานายบอกว�าจัร�ง True Negative (TN) คุ�อ ส/�งทำ��โป็รแกรมืทำานายว�าไม,จัร�ง และผลการทำานายบอกว�าไม,จัร�ง False Positive (FP) คุ�อ ส/�งทำ��โป็รแกรมืทำานายว�าจัร�ง แตั�ผลการทำานายบอกว�าไม,จัร�ง False Negative (FN) คุ�อ ส/�งทำ��โป็รแกรมืทำานายว�าไม,จัร�ง แตั�ผลการทำานายบอกว�าจัร�ง
58
Sensitivity or Recall คุ�อ คุ�าทำ��บอกว�าโป็รแกรมืทำานายได�ว�าจร/ง เป็�นอ�ตัราส�วนเทำ�าไรข้องจร/งทำ�*งหมืด
Specificity คุ�อ คุ�าทำ��บอกว�าโป็รแกรมืทำานายได�ว�าไมื�จร/ง เป็�นอ�ตัราส�วนเทำ�าไรข้องจร/งทำ�*งหมืด
Precision คุ�อ คุ�าทำ��บอกว�าโป็รแกรมืทำานายว�าจร/ง ถู�กตั�องเทำ�าไร
การประเม�นค,าควิามแม,นย"า
59
การประเม�นค,าควิามแม,นย"า
Accuracy = (TP+TN) (TP+TN+FP+FN)
Accuracy คุ�อ คุ�าทำ��บอกว�าโป็รแกรมืสามืารถูทำานายได�แมื�นยาข้นาดไหน
60
Example:
Recall : 6,954/7,000 = 0.993 Specificity: 2,588/ 3,000 = 0.863 Precision: 6,954/7,366 = 0.944 Accuracy: (6,954+2,588)/10,000 = 0.954
Rule-Based Classifier Classify records by using a collection of “if…
then…” rules Rule: (Condition) y
where Condition is a conjunctions of attributes y is the class label
LHS: rule antecedent or condition RHS: rule consequent Examples of classification rules:
(Blood Type=Warm) (Lay Eggs=Yes) Birds (Taxable Income < 50K) (Refund=Yes)
Evade=No61
Rule-based Classifier (Example)
R1: (Give Birth = no) (Can Fly = yes) BirdsR2: (Give Birth = no) (Live in Water = yes)
FishesR3: (Give Birth = yes) (Blood Type = warm)
MammalsR4: (Give Birth = no) (Can Fly = no) ReptilesR5: (Live in Water = sometimes) Amphibians
Name Blood Type Give Birth Can Fly Live in Water Classhuman warm yes no no mammalspython cold no no no reptilessalmon cold no no yes fisheswhale warm yes no yes mammalsfrog cold no no sometimes amphibianskomodo cold no no no reptilesbat warm yes yes no mammalspigeon warm no yes no birdscat warm yes no no mammalsleopard shark cold yes no yes fishesturtle cold no no sometimes reptilespenguin warm no no sometimes birdsporcupine warm yes no no mammalseel cold no no yes fishessalamander cold no no sometimes amphibiansgila monster cold no no no reptilesplatypus warm no no no mammalsowl warm no yes no birdsdolphin warm yes no yes mammalseagle warm no yes no birds
62
Application of Rule-Based Classifier A rule r covers an instance x if the
attributes of the instance satisfy the condition of the ruleR1: (Give Birth = no) (Can Fly = yes) Birds
R2: (Give Birth = no) (Live in Water = yes) Fishes
R3: (Give Birth = yes) (Blood Type = warm) Mammals
R4: (Give Birth = no) (Can Fly = no) Reptiles
R5: (Live in Water = sometimes) Amphibians
The rule R1 covers a hawk => Bird
The rule R3 covers the grizzly bear => Mammal
Name Blood Type Give Birth Can Fly Live in Water Classhawk warm no yes no ?grizzly bear warm yes no no ?
63
Rule Coverage and Accuracy Coverage of a rule:
Fraction of records that satisfy the antecedent of a rule
Accuracy of a rule: Fraction of records
that satisfy both the antecedent and consequent of a rule
Tid Refund Marital Status
Taxable Income Class
1 Yes Single 125K No
2 No Married 100K No
3 No Single 70K No
4 Yes Married 120K No
5 No Divorced 95K Yes
6 No Married 60K No
7 Yes Divorced 220K No
8 No Single 85K Yes
9 No Married 75K No
10 No Single 90K Yes 10
(Status=Single) No
Coverage = 40%, Accuracy = 50%64
How does Rule-based Classifier Work?
R1: (Give Birth = no) (Can Fly = yes) Birds
R2: (Give Birth = no) (Live in Water = yes) Fishes
R3: (Give Birth = yes) (Blood Type = warm) Mammals
R4: (Give Birth = no) (Can Fly = no) Reptiles
R5: (Live in Water = sometimes) Amphibians
A lemur triggers rule R3, so it is classified as a mammal
A turtle triggers both R4 and R5A dogfish shark triggers none of the
rules
Name Blood Type Give Birth Can Fly Live in Water Classlemur warm yes no no ?turtle cold no no sometimes ?dogfish shark cold yes no yes ?
65
Characteristics of Rule-Based Classifier Mutually exclusive rules
Classifier contains mutually exclusive rules if the rules are independent of each other
Every record is covered by at most one rule
Exhaustive rules Classifier has exhaustive coverage if it
accounts for every possible combination of attribute values
Each record is covered by at least one rule
66
From Decision Trees To Rules
YESYESNONO
NONO
NONO
Yes No
{Married}{Single,
Divorced}
< 80K > 80K
Taxable Income
Marital Status
Refund
Classification Rules
(Refund=Yes) ==> No
(Refund=No, Marital Status={Single,Divorced},Taxable Income<80K) ==> No
(Refund=No, Marital Status={Single,Divorced},Taxable Income>80K) ==> Yes
(Refund=No, Marital Status={Married}) ==> No
Rules are mutually exclusive and exhaustive
Rule set contains as much information as the tree67
Rules Can Be Simplified
YESYESNONO
NONO
NONO
Yes No
{Married}{Single,
Divorced}
< 80K > 80K
Taxable Income
Marital Status
Refund
Tid Refund Marital Status
Taxable Income Cheat
1 Yes Single 125K No
2 No Married 100K No
3 No Single 70K No
4 Yes Married 120K No
5 No Divorced 95K Yes
6 No Married 60K No
7 Yes Divorced 220K No
8 No Single 85K Yes
9 No Married 75K No
10 No Single 90K Yes 10
Initial Rule: (Refund=No) (Status=Married) No
Simplified Rule: (Status=Married) No68
Effect of Rule Simplification Rules are no longer mutually exclusive
A record may trigger more than one rule Solution?
Ordered rule set Unordered rule set – use voting
schemes Rules are no longer exhaustive
A record may not trigger any rules Solution?
Use a default class
69
Ordered Rule Set Rules are rank ordered according to their
priority An ordered rule set is known as a
decision list When a test record is presented to the
classifier It is assigned to the class label of the
highest ranked rule it has triggered If none of the rules fired, it is assigned to
the default class
R1: (Give Birth = no) (Can Fly = yes) Birds
R2: (Give Birth = no) (Live in Water = yes) Fishes
R3: (Give Birth = yes) (Blood Type = warm) Mammals
R4: (Give Birth = no) (Can Fly = no) Reptiles
R5: (Live in Water = sometimes) Amphibians Name Blood Type Give Birth Can Fly Live in Water Class
turtle cold no no sometimes ?
70
Rule Ordering Schemes Rule-based ordering
Individual rules are ranked based on their quality
Class-based ordering Rules that belong to the same class
appear togetherRule-based Ordering
(Refund=Yes) ==> No
(Refund=No, Marital Status={Single,Divorced},Taxable Income<80K) ==> No
(Refund=No, Marital Status={Single,Divorced},Taxable Income>80K) ==> Yes
(Refund=No, Marital Status={Married}) ==> No
Class-based Ordering
(Refund=Yes) ==> No
(Refund=No, Marital Status={Single,Divorced},Taxable Income<80K) ==> No
(Refund=No, Marital Status={Married}) ==> No
(Refund=No, Marital Status={Single,Divorced},Taxable Income>80K) ==> Yes
71
Building Classification Rules Direct Method:
Extract rules directly from data e.g.: RIPPER, CN2, Holte’s 1R
Indirect Method: Extract rules from other classification models (e.g. decision trees, neural networks, etc).
e.g: C4.5rules72
Direct Method: Sequential Covering
1. Start from an empty rule2. Grow a rule using the Learn-One-
Rule function3. Remove training records covered by
the rule4. Repeat Step (2) and (3) until
stopping criterion is met
73
Example of Sequential Covering
(i) Original Data (ii) Step 1
74
Example of Sequential Covering…
(iii) Step 2
R1
(iv) Step 3
R1
R2
75
Aspects of Sequential Covering
Rule GrowingInstance EliminationRule EvaluationStopping CriterionRule Pruning
76
Rule Growing Two common
strategies
Status =Single
Status =Divorced
Status =Married
Income> 80K...
Yes: 3No: 4{ }
Yes: 0No: 3
Refund=No
Yes: 3No: 4
Yes: 2No: 1
Yes: 1No: 0
Yes: 3No: 1
(a) General-to-specific
Refund=No,Status=Single,Income=85K(Class=Yes)
Refund=No,Status=Single,Income=90K(Class=Yes)
Refund=No,Status = Single(Class = Yes)
(b) Specific-to-general
77
Rule Growing (Examples) CN2 Algorithm:
Start from an empty conjunct: {} Add conjuncts that minimizes the entropy measure:
{A}, {A,B}, … Determine the rule consequent by taking majority
class of instances covered by the rule RIPPER Algorithm:
Start from an empty rule: {} => class Add conjuncts that maximizes FOIL’s information
gain measure: R0: {} => class (initial rule) R1: {A} => class (rule after adding conjunct) Gain(R0, R1) = t [ log (p1/(p1+n1)) – log (p0/(p0
+ n0)) ] where t: number of positive instances covered
by both R0 and R1p0: number of positive instances covered by R0n0: number of negative instances covered by R0p1: number of positive instances covered by R1n1: number of negative instances covered by R1
78
Instance Elimination Why do we need to
eliminate instances? Otherwise, the next
rule is identical to previous rule
Why do we remove positive instances? Ensure that the next
rule is different Why do we remove
negative instances? Prevent
underestimating accuracy of rule
Compare rules R2 and R3 in the diagram
class = +
class = -
+
+ +
++
++
+
++
+
+
+
+
+
+
++
+
+
-
-
--
- --
--
- -
-
-
-
-
--
-
-
-
-
+
+
++
+
+
+
R1
R3 R2
+
+
79
Rule Evaluation Metrics:
Accuracy
Laplace
M-estimate
kn
nc
1
kn
kpnc
n : Number of instances covered by rule
nc : Number of instances covered by rule
k : Number of classes
p : Prior probability
n
nc
80
Stopping Criterion and Rule Pruning Stopping criterion
Compute the gain If gain is not significant, discard the
new rule
Rule Pruning Similar to post-pruning of decision
trees Reduced Error Pruning:
Remove one of the conjuncts in the rule
Compare error rate on validation set before and after pruning
If error improves, prune the conjunct
81
Summary of Direct Method Grow a single rule Remove Instances from rule Prune the rule (if necessary) Add rule to Current Rule Set Repeat
82
Direct Method: RIPPER For 2-class problem, choose one of the classes as
positive class, and the other as negative class Learn rules for positive class Negative class will be default class
For multi-class problem Order the classes according to increasing class
prevalence (fraction of instances that belong to a particular class)
Learn the rule set for smallest class first, treat the rest as negative class
Repeat with next smallest class as positive class
83
Direct Method: RIPPER Growing a rule:
Start from empty rule Add conjuncts as long as they improve FOIL’s
information gain Stop when rule no longer covers negative
examples Prune the rule immediately using incremental
reduced error pruning Measure for pruning: v = (p-n)/(p+n)
p: number of positive examples covered by the rule in the validation set
n: number of negative examples covered by the rule in the validation set
Pruning method: delete any final sequence of conditions that maximizes v
84
Direct Method: RIPPER Building a Rule Set:
Use sequential covering algorithm Finds the best rule that covers the current
set of positive examples Eliminate both positive and negative
examples covered by the rule Each time a rule is added to the rule set,
compute the new description length stop adding new rules when the new
description length is d bits longer than the smallest description length obtained so far
85
Direct Method: RIPPER Optimize the rule set:
For each rule r in the rule set R Consider 2 alternative rules:
Replacement rule (r*): grow new rule from scratch
Revised rule(r’): add conjuncts to extend the rule r
Compare the rule set for r against the rule set for r* and r’
Choose rule set that minimizes MDL principle Repeat rule generation and rule optimization for
the remaining positive examples86
Indirect Methods
Rule Set
r1: (P=No,Q=No) ==> -r2: (P=No,Q=Yes) ==> +r3: (P=Yes,R=No) ==> +r4: (P=Yes,R=Yes,Q=No) ==> -r5: (P=Yes,R=Yes,Q=Yes) ==> +
P
Q R
Q- + +
- +
No No
No
Yes Yes
Yes
No Yes
87
Indirect Method: C4.5rules Extract rules from an unpruned decision
tree For each rule, r: A y,
consider an alternative rule r’: A’ y where A’ is obtained by removing one of the conjuncts in A
Compare the pessimistic error rate for r against all r’s
Prune if one of the r’s has lower pessimistic error rate
Repeat until we can no longer improve generalization error
88
Indirect Method: C4.5rules Instead of ordering the rules, order subsets
of rules (class ordering) Each subset is a collection of rules with the
same rule consequent (class) Compute description length of each subset
Description length = L(error) + g L(model)
g is a parameter that takes into account the presence of redundant attributes in a rule set (default value = 0.5)
89
ExampleName Give Birth Lay Eggs Can Fly Live in Water Have Legs Class
human yes no no no yes mammalspython no yes no no no reptilessalmon no yes no yes no fisheswhale yes no no yes no mammalsfrog no yes no sometimes yes amphibianskomodo no yes no no yes reptilesbat yes no yes no yes mammalspigeon no yes yes no yes birdscat yes no no no yes mammalsleopard shark yes no no yes no fishesturtle no yes no sometimes yes reptilespenguin no yes no sometimes yes birdsporcupine yes no no no yes mammalseel no yes no yes no fishessalamander no yes no sometimes yes amphibiansgila monster no yes no no yes reptilesplatypus no yes no no yes mammalsowl no yes yes no yes birdsdolphin yes no no yes no mammalseagle no yes yes no yes birds
90
C4.5 versus C4.5rules versus RIPPERC4.5rules:
(Give Birth=No, Can Fly=Yes) Birds
(Give Birth=No, Live in Water=Yes) Fishes
(Give Birth=Yes) Mammals
(Give Birth=No, Can Fly=No, Live in Water=No) Reptiles
( ) Amphibians
GiveBirth?
Live InWater?
CanFly?
Mammals
Fishes Amphibians
Birds Reptiles
Yes No
Yes
Sometimes
No
Yes No
RIPPER:
(Live in Water=Yes) Fishes
(Have Legs=No) Reptiles
(Give Birth=No, Can Fly=No, Live In Water=No)
Reptiles
(Can Fly=Yes,Give Birth=No) Birds
() Mammals
91
C4.5 versus C4.5rules versus RIPPER
PREDICTED CLASS Amphibians Fishes Reptiles Birds MammalsACTUAL Amphibians 0 0 0 0 2CLASS Fishes 0 3 0 0 0
Reptiles 0 0 3 0 1Birds 0 0 1 2 1Mammals 0 2 1 0 4
PREDICTED CLASS Amphibians Fishes Reptiles Birds MammalsACTUAL Amphibians 2 0 0 0 0CLASS Fishes 0 2 0 0 1
Reptiles 1 0 3 0 0Birds 1 0 0 3 0Mammals 0 0 1 0 6
C4.5 and C4.5rules:
RIPPER:
92
Advantages of Rule-Based Classifiers As highly expressive as decision trees Easy to interpret Easy to generate Can classify new instances rapidly Performance comparable to decision trees
93
94
HW#5 What is Classification? What is decision tree? What is rule-based classification? How many step of Classification? Please explain supervised vs. unsupervised
Learning? Please explain that how to avoid overfitting
event? How many type of rule-based classification?
95
HW#5 The following table consists of training data from a buy computer database.
The data have been generalized. Let status the class label attributes. Use your algorithm to construct a decision tree from the given data. Use your algorithm to construct a rule-based classification.
96
LAB5 Use weka program to construct a
decision tree and rule-based classification from the given file.
buycomputer.csv buyhouse_c45.csv buyhouse_id3.csv