利用给定的数据预测某天("Sunny","Cool","High","Strong")是否打球……
数据:
NO Outlook Temperature Humidity Wind Play 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
代码:
data=read.table("C:\code\R\playTennis.txt",header=T) pre=c("Sunny","Cool","High","Strong","xx") sum_Yes=length(which(data$Play=="Yes")) sum_No=length(which(data$Play=="No")) sum=sum_Yes+sum_No #计算yes的概率 p_O_y=length(which(data$Outlook==pre[1]&data$Play=="Yes"))/sum_Yes p_T_y=length(which(data$Temperature==pre[2]&data$Play=="Yes"))/sum_Yes p_H_y=length(which(data$Humidity==pre[3]&data$Play=="Yes"))/sum_Yes p_W_y=length(which(data$Wind==pre[4]&data$Play=="Yes"))/sum_Yes p_y=(sum_Yes/sum)*p_O_y*p_T_y*p_H_y*p_W_y #计算No的概率 p_O_n=length(which(data$Outlook==pre[1]&data$Play=="No"))/sum_No p_T_n=length(which(data$Temperature==pre[2]&data$Play=="No"))/sum_No p_H_n=length(which(data$Humidity==pre[3]&data$Play=="No"))/sum_No p_W_n=length(which(data$Wind==pre[4]&data$Play=="No"))/sum_No p_n=(sum_No/sum)*p_O_n*p_T_n*p_H_n*p_W_n #结果 print(p_y) print(p_n)
结果:
[1] 0.005291005 [1] 0.02057143