题目:
大致结果手绘图:
详细计算过程:
一级筛选:
一级筛选 1.天气 from math import* sum = -(9/14) * log(9/14,2) - (5/14) * log(5/14,2) a = 5/14*(-(2/5) * log(2/5,2) - (3/5) * log(3/5,2))#晴 b = 4/14*(-1 * log(1,2))#阴 c = 5/14*(-(3/5) * log(3/5,2) - (2/5) * log(2/5,2))#雨 sum1 = a + b + c print(sum - sum1) 结果 #0.2467498197744391 2.温度 from math import* sum = -(9/14) * log(9/14,2) - (5/14) * log(5/14,2) a = 4/14*(-(1/2) * log(1/2,2) - (1/2) * log(1/2,2))#热 b = 6/14*(-(2/3) * log(2/3,2) - (2/3) * log(2/3,2))#温 c = 4/14*(-(3/4) * log(3/4,2) - (1/4) * log(1/4,2))#凉爽 sum1 = a + b + c print(sum - sum1) 结果 #0.08851363698450365 3.湿度 from math import* sum = -(9/14) * log(9/14,2) - (5/14) * log(5/14,2) a = 7/14*(-(3/7) * log(3/7,2) - (4/7) * log(4/7,2))#高 b = 7/14*(-(6/7) * log(6/7,2) - (1/7) * log(1/7,2))#中 sum1 = a + b print(sum - sum1) 结果 #0.15183550136234136 4.是否有风 from math import* sum = -(9/14) * log(9/14,2) - (5/14) * log(5/14,2) a = 6/14*(-(3/6) * log(3/6,2) - (3/6) * log(3/6,2))#是 b = 8/14*(-(6/8) * log(6/8,2) - (2/8) * log(2/8,2))#否 sum1 = a + b print(sum - sum1) 结果 #0.04812703040826927 所以一级筛选选择天气
二级筛选:
2.1晴 2.1.1温度 from math import* sum = -(2/5) * log(2/5,2) - (3/5) * log(3/5,2) a = 2/5*(-(1/2) * log(1/2,2) - (1/2) * log(1/2,2))#温 #热和凉爽都为0 sum1 = a print(sum - sum1) 结果 #0.5709505944546686 2.1.2湿度 由于中和高都为0,所以信息增益最大 2.1.3是否有风 from math import* sum = -(2/5) * log(2/5,2) - (3/5) * log(3/5,2) a = 2/5*(-(1/2) * log(1/2,2) - (1/2) * log(1/2,2))#是 b = 3/5*(-(1/3) * log(1/3,2) - (2/3) * log(2/3,2))#否 sum1 = a + b print(sum - sum1) 结果 #0.01997309402197489 所以在二级筛选中晴天时选择湿度 2.2阴————全部适合 2.3雨 2.3.1温度 from math import* sum = -(2/5) * log(2/5,2) - (3/5) * log(3/5,2) a = 2/5*(-(1/2) * log(1/2,2) - (1/2) * log(1/2,2))#凉爽 b = 3/5*(-(1/3) * log(1/3,2) - (2/3) * log(2/3,2))#温 #热为0 sum1 = a + b print(sum - sum1) 结果 #0.01997309402197489 2.3.2湿度 from math import* sum = -(2/5) * log(2/5,2) - (3/5) * log(3/5,2) a = 2/5*(-(1/2) * log(1/2,2) - (1/2) * log(1/2,2))#高 b = 3/5*(-(1/3) * log(1/3,2) - (2/3) * log(2/3,2))#中 sum1 = a + b print(sum - sum1) 结果 #0.01997309402197489 2.3.3是否有风 由于有风和无风都为0,所以信息增益最大 所以在二级筛选中晴天时选择是否有风
关于手算有不懂的的地方可以问我~
(争取早点弄出程序,手算费脑子。。。)