Ví dụ kết quả cho một thuộc tính x3 như sau
Phân lớp cứng với decision tree. Decision tree với phân lớp mềm
Giải thuật xây dựng random forest
• Từ tập dữ liệu học LS có m phần tử và n biến (thuộc tính), xây dựng T cây quyết định một cách độc lập nhau
• Mô hình cây quyết định thứ t được xây dựng trên tập mẫu Bootstrap thứ t (lấy mẫu m phần tử có hoàn lại từ tập học LS)
• Tại nút trong, chọn ngẫu nhiên n’ biến (n’<<n) và tính toán phân hoạch tốt nhất dựa trên n’ biến này
• Cây được xây dựng đến độ sâu tối đa không cắt nhánh Kết thúc quá trình xây dựng T mô hình cơ sở, dùng chiến lược bình chọn số đông để phân lớp một phần tử mới đến X
Hình 2.7: Mô hình thuật toán rừng ngẫu nhiên [3]
Ví dụ:
Xây dựng cây thứ 1:
Chọn cấy thứ nhất là cây ví dụ trên mục 2. Thuật toán cây quyết định => Hình 22: Cây phân lớp cuối cùng của thuật toán DT
Xây dựng cây thứ 2:
Bảng 2.6: Tập dữ liệu phân lớp cho thuật toán RF
# Outlook Temperature Humidity Wind Decision
1 Sunny Hot High Weak No
2 Sunny Hot High Strong No
3 Overcast Hot High Weak Yes
4 Rain Mild High Weak Yes
5 Rain Cool Normal Weak Yes
6 Rain Cool Normal Strong No
7 Overcast Cool Normal Strong Yes
8 Sunny Mild High Weak No
9 Sunny Cool Normal Weak Yes
10 Rain Mild Normal Weak Yes
11 Sunny Mild Normal Strong Yes
12 Overcast Mild High Strong Yes
13 Overcast Hot Normal Weak Yes
14 Rain Mild High Strong No
Chọn ngẫu nhiên bản ghi làm tập training.
Training data cây 2:
Bảng 2.7: Dữ liệu được chọn ngẫu nhiên từ tập dữ liệu ban đầu cho cây 2
# Outlook Temperature Humidity Wind Decision
1 Sunny Hot High Weak No
4 Rain Mild High Weak Yes
5 Rain Cool Normal Weak Yes
6 Rain Cool Normal Strong No
8 Sunny Mild High Weak No
11 Sunny Mild Normal Strong Yes
12 Overcast Mild High Strong Yes
13 Overcast Hot Normal Weak Yes
13 Overcast Hot Normal Weak Yes
Test data
Bảng 2.8: Dữ liệu để kiểm tra độ chính xác thuật toán RF
# Outlook Temperature Humidity Wind Decision
2 Sunny Hot High Strong No
3 Overcast Hot High Weak Yes
7 Overcast Cool Normal Strong Yes
9 Sunny Cool Normal Weak Yes
10 Rain Mild Normal Weak Yes
14 Rain Mild High Strong No
E(Decision) = -(p(no).log2(p(no) + p(yes).log2(p(yes))) = - (3/9.log2(3/9)+6/9.log2(6/9)) = 0.918
E(Decision|Outlook) = p(Sunny).E(Sunny|Decision) + p(Overcast).E(Overcast|Decision) + p(Rain).E(Rain|Decision) = 3/9.-(2/3. log2(2/3) + 1/3. log2(1/3)) + 3/9.0 + 3/9.-(1/3.
log2(1/3)+2/3. log2(2/3)) = 0.612
Gain(Decision|Outlook) = E(Decision) – E(Decision| Outlook) = 0.918-0.612 = 0.306
E(Decision|Temperature) = p(Hot).E(Hot|Decision) + p(Mild).E(Mild|Decision) + p(Cool).E(Cool|Decision) = 3/9.-(1/3. log2(1/3) + 2/3. log2(2/3)) + 2/9.-(1/2. log2(1/2) + 1/2.
log2(1/2)) + 4/9.-(1/4. log2(1/4)+3/4. log2(3/4)) = 8/9
Gain(Decision|Temperature) = E(Decision) – E(Decision| Temperature) = 0.918-8/9=0.03 E(Decision|Humidity) = p(High).E(High|Decision) + p(Normal).E(Normal|Decision) = 5/9.- (1/5. log2(1/5) + 4/5. log2(4/5)) + 4/9.-(2/4. log2(2/4) + 2/4. log2(2/4)) = 0.8455
Gain(Decision|Humidity) = E(Decision) – E(Decision| Humidity) = 0.918-0.8455=0.0725 E(Decision|Wind) = p(Weak).E(Weak|Decision) + p(Strong).E(Strong|Decision) = 6/9.-(2/6.
log2(2/6) + 4/6. log2(4/6)) + 3/9.-(1/3. log2(1/3) + 2/3. log2(2/3)) = 0.918 Gain(Decision|Wind) = E(Decision) – E(Decision|Wind) = 0.918-0.918=0
Hình 2.8: Cây RF 2 sau vòng lặp thứ nhất
Xét nhánh Overcast: Tất cả record trả về Yes = > Dừng E(Sunny) = -(2/3. log2(2/3) + 1/3. log2(1/3)) = 0.918
Gain(Sunny|Temperature) = 0.918 – 1/3.0 – 2/3.(1/2.log2(1/2)+1/2.log2(1/2))= 0.25 Gain(Sunny|Humidity) = 0.918 – 2/3.0 – 1/3.0 = 0.918
Gain(Sunny|Wind) = 0.918 –2/3.0 – 1/3.0= 0.918
Bảng 2.9: Tất cả dữ liệu Sunny của Outlook
# Outlook Temperature Humidity Wind Decision
1 Sunny Hot High Weak No
8 Sunny Mild High Weak No
11 Sunny Mild Normal Strong Yes
E(Rain) = -1/3.log2(1/3)-2/3.log2(2/3) = 0.918
Gain(Rain|Temperature) = 0.918 - 1/3.0 – 2/3.(-1/2.log2(1/2)-1/2.log2(1/2)) = 0.25 Gain(Rain|Humidity) = 0.918 - 1/3.0 – 2/3.(-1/2.log2(1/2)-1/2.log2(1/2)) = 0.25 Gain (Rain|Wind) = 0.918 – 2/3.0 – 1/3.0 = 0.918
Bảng 2.10: Tất cả dữ liệu Rain của Outlook
# Outlook Temperature Humidity Wind Decision
4 Rain Mild High Weak Yes
5 Rain Cool Normal Weak Yes
6 Rain Cool Normal Strong No
Hình 2.9: Cây RF 2 sau vòng lặp thứ hai
Xét nhánh Sunny-> Weak : All record return No => Stop Xét nhánh Sunny-> Strong : All record return Yes => Stop Xét nhánh Rain -> Weak : All record return Yes => Stop Xét nhánh Rain -> Strong All record return No => Stop
Hình 2.10: Cây RF 2 hoàn chỉnh thứ nhất
Xây dựng cây 3:
Chọn ngẫu nhiên lấy training data cây 3
Bảng 2.11: Bảng đánh dấu dữ liệu được chọn ngẫu nhiên cho cây 3
# Outlook Temperature Humidity Wind Decision
1 Sunny Hot High Weak No
2 Sunny Hot High Strong No
3 Overcast Hot High Weak Yes
4 Rain Mild High Weak Yes
5 Rain Cool Normal Weak Yes
6 Rain Cool Normal Strong No
7 Overcast Cool Normal Strong Yes
8 Sunny Mild High Weak No
9 Sunny Cool Normal Weak Yes
10 Rain Mild Normal Weak Yes
11 Sunny Mild Normal Strong Yes
12 Overcast Mild High Strong Yes
13 Overcast Hot Normal Weak Yes
14 Rain Mild High Strong No
Training data cây 3
Bảng 2.12: Bảng dữ liệu chọn ngẫu nhiên cho cây 3
# Outlook Temperature Humidity Wind Decision
2 Sunny Hot High Strong No
3 Overcast Hot High Weak Yes
4 Rain Mild High Weak Yes
6 Rain Cool Normal Strong No
7 Overcast Cool Normal Strong Yes
10 Rain Mild Normal Weak Yes
11 Sunny Mild Normal Strong Yes
12 Overcast Mild High Strong Yes
14 Rain Mild High Strong No
E(Decision) = -(p(no).log2(p(no) + p(yes).log2(p(yes))) = - (3/9.log2(3/9)+6/9.log2(6/9)) = 0.918
E(Decision|Outlook) = p(Sunny).E(Sunny|Decision) + p(Overcast).E(Overcast|Decision) + p(Rain).E(Rain|Decision) = 2/9.-(1/2. log2(1/2) + 1/2. log2(1/2)) + 3/9.0 + 4/9.-(2/4.
log2(2/4)+2/4. log2(2/4)) = 2/3
Gain(Decision|Outlook) = E(Decision) – E(Decision| Outlook) = 0.918-2/3 = 0.25
E(Decision|Temperature) = p(Hot).E(Hot|Decision) + p(Mild).E(Mild|Decision) + p(Cool).E(Cool|Decision) = 2/9.-(1/2. log2(1/2) + 1/2. log2(1/2)) + 2/9.-(1/2. log2(1/2) + 1/2.
log2(1/2)) + 5/9.-(1/5. log2(1/5)+4/5. log2(4/5)) = 0.8455
Gain(Decision|Temperature) = E(Decision) – E(Decision| Temperature) = 0.918- 0.8455=0.0725
E(Decision|Humidity) = p(High).E(High|Decision) + p(Normal).E(Normal|Decision) = 5/9.- (1/5. log2(1/5) + 4/5. log2(4/5)) + 4/9.-(2/4. log2(2/4) + 2/4. log2(2/4)) = 0.8455
Gain(Decision|Humidity) = E(Decision) – E(Decision| Humidity) = 0.918-0.8455=0.0725 E(Decision|Wind) = p(Weak).E(Weak|Decision) + p(Strong).E(Strong|Decision) = 6/9.-(3/6.
log2(3/6) + 3/6. log2(3/6)) + 3/9.0 = 2/3
Gain(Decision|Wind) = E(Decision) – E(Decision|Wind) = 0.918=0.25
Hình 2.11: Cây RF 3 sau vòng lặp 1
Xét nhánh Weak: All record return Yes E(Strong) = -(3/6. log2(3/6) + 3/6. log2(3/6)) = 1
Gain(Strong|Temperature) = 1 – 1/6.0 – 2/6.(-1/2.log2(1/2)-1/2.log2(1/2)) – 3/6.(- 1/3log2(1/3)-2/3log2(2/3))= 0.2
Gain(Strong|Humidity) = 1 –3/6(-2/3log2(2/3)-1/3log2(1/3)) – 3/6(-2/3log2(2/3)- 1/3log2(1/3))= 0.082
Gain(Strong|Outlook) = 1 – 2/6.(-1/2log2(1/2)-1/2log2(1/2)) - 2/6.0 – 2/6.0 = 0.3 Bảng 2.13: Tất cả dữ liệu nhánh Strong của Wind
# Outlook Temperature Humidity Wind Decision
2 Sunny Hot High Strong No
6 Rain Cool Normal Strong No
7 Overcast Cool Normal Strong Yes
11 Sunny Mild Normal Strong Yes
12 Overcast Mild High Strong Yes
14 Rain Mild High Strong No
Hình 2.12: Cây RF 3 sau vòng lặp 2
Xét nhánh Overcast : All record return Yes => Stop Xét nhánh Rain-> Strong : All record return No => Stop
Bảng 2.14: Nhánh Sunny của Outlook nốt tiếp Strong của Wind
# Outlook Temperature Humidity Wind Decision
2 Sunny Hot High Strong No
11 Sunny Mild Normal Strong Yes
E(Sunny) = -1/2log2(1/2) – 1/2log2(1/2) = 1 Gain(Sunny|Temperature) = 1 – 1.0 = 1 Gain(Sunny|Humidity) = 1 – 1.0 = 1
Figure 2.13: Cây RF 3 hoàn thiện Test result
Bảng 2.15: Đánh giá kết quả thuật toán RF
# Outlook
Tempe rature
Humidity Wind Decision Tree 1
Tree 2
Tree 3
Conclusion
10 Rain Mild Normal Weak Yes Yes Yes Yes Yes 11 Sunny Mild Normal Strong Yes Yes Yes Yes Yes
12 Overcast Mild High Strong Yes Yes Yes Yes Yes 13 Overcast Hot Normal Weak Yes Yes Yes Yes Yes 14 Rain Mild High Strong No No No No No
2 Sunny Hot High Strong No No Yes No No
3 Overcast Hot High Weak Yes Yes Yes Yes Yes 7 Overcast Cool Normal Strong Yes Yes Yes Yes Yes 9 Sunny Cool Normal Weak Yes Yes No Yes Yes