# 【R的机器学习】模型性能提升探索：朴素贝叶斯

``head(iris_train)    Sepal.Length Sepal.Width Petal.Length Petal.Width75           6.4         2.9          4.3         1.316           5.7         4.4          1.5         0.4120          6.0         2.2          5.0         1.591           5.5         2.6          4.4         1.293           5.8         2.6          4.0         1.2127          6.2         2.8          4.8         1.8       Species75  versicolor16      setosa120  virginica91  versicolor93  versicolor127  virginica``

``P（车少|下雨天）=0.8``

``P（下雨天）=0.5P（车少）=0.1``

``P（车少|下雨天）*P（下雨天）=P（车少）*P（下雨天|车少）``

``P（下雨天|车少）=40%``

``summary(iris)``
``Sepal.Length    Sepal.Width     Petal.Length   Min.   :4.300   Min.   :2.000   Min.   :1.000   1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   Median :5.800   Median :3.000   Median :4.350   Mean   :5.843   Mean   :3.057   Mean   :3.758   3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   Max.   :7.900   Max.   :4.400   Max.   :6.900    Petal.Width          Species   Min.   :0.100   setosa    :50   1st Qu.:0.300   versicolor:50   Median :1.300   virginica :50   Mean   :1.199                   3rd Qu.:1.800                   Max.   :2.500``

``iris1<-irisfor (j in 1:4){  for (i in 1:nrow(iris1)){    if (iris1[i,j]<=quantile(iris[,j])[2]){      iris1[i,j]<-'L'    }else if(iris1[i,j]<=quantile(iris[,j])[3]){      iris1[i,j]<-'M'    }else if(iris1[i,j]<=quantile(iris[,j])[4]){      iris1[i,j]<-'H'    }else {      iris1[i,j]<-'G'    }  }}``

``str(iris1)'data.frame':   150 obs. of  5 variables: \$ Sepal.Length: chr  "L" "L" "L" "L" ... \$ Sepal.Width : chr  "G" "M" "H" "H" ... \$ Petal.Length: chr  "L" "L" "L" "L" ... \$ Petal.Width : chr  "L" "L" "L" "L" ... \$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...``

``library(e1071)m_nb<-naiveBayes(iris1_train,iris1_train\$Species,laplace=0.01)``

``P（车少）=0.1``

``m_nbNaive Bayes Classifier for Discrete PredictorsCall:naiveBayes.default(x = iris1_train, y = iris1_train\$Species,     laplace = 0.01)A-priori probabilities:iris1_train\$Species    setosa versicolor  virginica  0.3523810  0.3238095  0.3238095 Conditional probabilities:                   Sepal.Lengthiris1_train\$Species            G            H            L         setosa     0.0002702703 0.0002702703 0.7029729730         versicolor 0.2061764706 0.3532352941 0.0885294118         virginica  0.5002941176 0.3238235294 0.0297058824                   Sepal.Lengthiris1_train\$Species            M         setosa     0.2975675676         versicolor 0.3532352941         virginica  0.1473529412                   Sepal.Widthiris1_train\$Species          G          H          L         setosa     0.62189189 0.21648649 0.02729730         versicolor 0.02970588 0.14735294 0.50029412         virginica  0.08852941 0.26500000 0.41205882                   Sepal.Widthiris1_train\$Species          M         setosa     0.13540541         versicolor 0.32382353         virginica  0.23558824                   Petal.Lengthiris1_train\$Species            G            H            L         setosa     0.0002702703 0.0002702703 0.8921621622         versicolor 0.0002941176 0.5297058824 0.0002941176         virginica  0.6179411765 0.3826470588 0.0002941176                   Petal.Lengthiris1_train\$Species            M         setosa     0.1083783784         versicolor 0.4708823529         virginica  0.0002941176                   Petal.Widthiris1_train\$Species            G            H            L         setosa     0.0002702703 0.0002702703 0.7570270270         versicolor 0.0002941176 0.4414705882 0.0002941176         virginica  0.7355882353 0.2650000000 0.0002941176                   Petal.Widthiris1_train\$Species            M         setosa     0.2435135135         versicolor 0.5591176471         virginica  0.0002941176                   Speciesiris1_train\$Species       setosa   versicolor    virginica         setosa     0.9994598974 0.0002700513 0.0002700513         versicolor 0.0002938584 0.9994122833 0.0002938584         virginica  0.0002938584 0.0002938584 0.9994122833``

``iris1_train\$Species    setosa versicolor  virginica  0.3523810  0.3238095  0.3238095 ``

``Conditional probabilities:Sepal.Lengthiris1_train\$Species            G            H            L         setosa     0.0002702703 0.0002702703 0.7029729730         versicolor 0.2061764706 0.3532352941 0.0885294118         virginica  0.5002941176 0.3238235294 0.0297058824                   Sepal.Lengthiris1_train\$Species            M         setosa     0.2975675676         versicolor 0.3532352941         virginica  0.1473529412``

``P（G|setosa）=0.0002702703P（G|versicolor）=0.2061764706P（G|virginica）=0.5002941176``

``P（virginica）*P（G|virginica）=P（G）*P（virginica|G）``

``P（virginica|G）P（G）P（G|virginica）``

``m_nbp<-predict(m_nb,iris1_test)[1] versicolor versicolor virginica  virginica  versicolor [6] versicolor setosa     setosa     virginica  versicolor[11] versicolor versicolor versicolor setosa     setosa    [16] versicolor virginica  virginica  virginica  versicolor[21] virginica  setosa     setosa     virginica  versicolor[26] setosa     versicolor versicolor virginica  setosa    [31] virginica  versicolor setosa     virginica  setosa    [36] setosa     setosa     versicolor versicolor virginica [41] virginica  virginica  setosa     virginica  virginica Levels: setosa versicolor virginica``

``table(iris1_test\$Species,m_nbp)m_nbp             setosa versicolor virginica  setosa         13          0         0  versicolor      0         16         0  virginica       0          0        16``

``m_nbp_p<-predict(m_nb,iris1_test,type='raw')m_nbp_p setosa  versicolor   virginica [1,] 0.002917515 0.994167039 0.002915446 [2,] 0.002917515 0.994167039 0.002915446 [3,] 0.002917515 0.002915446 0.994167039 [4,] 0.002917515 0.002915446 0.994167039 [5,] 0.002917515 0.994167039 0.002915446 [6,] 0.002917515 0.994167039 0.002915446 [7,] 0.994641852 0.002679074 0.002679074 [8,] 0.994641852 0.002679074 0.002679074 [9,] 0.002917515 0.002915446 0.994167039[10,] 0.002917515 0.994167039 0.002915446[11,] 0.002917515 0.994167039 0.002915446[12,] 0.002917515 0.994167039 0.002915446[13,] 0.002917515 0.994167039 0.002915446[14,] 0.994641852 0.002679074 0.002679074[15,] 0.994641852 0.002679074 0.002679074[16,] 0.002917515 0.994167039 0.002915446[17,] 0.002917515 0.002915446 0.994167039[18,] 0.002917515 0.002915446 0.994167039[19,] 0.002917515 0.002915446 0.994167039[20,] 0.002917515 0.994167039 0.002915446[21,] 0.002917515 0.002915446 0.994167039[22,] 0.994641852 0.002679074 0.002679074[23,] 0.994641852 0.002679074 0.002679074[24,] 0.002917515 0.002915446 0.994167039[25,] 0.002917515 0.994167039 0.002915446[26,] 0.994641852 0.002679074 0.002679074[27,] 0.002917515 0.994167039 0.002915446[28,] 0.002917515 0.994167039 0.002915446[29,] 0.002917515 0.002915446 0.994167039[30,] 0.994641852 0.002679074 0.002679074[31,] 0.002917515 0.002915446 0.994167039[32,] 0.002917515 0.994167039 0.002915446[33,] 0.994641852 0.002679074 0.002679074[34,] 0.002917515 0.002915446 0.994167039[35,] 0.994641852 0.002679074 0.002679074[36,] 0.994641852 0.002679074 0.002679074[37,] 0.994641852 0.002679074 0.002679074[38,] 0.002917515 0.994167039 0.002915446[39,] 0.002917515 0.994167039 0.002915446[40,] 0.002917515 0.002915446 0.994167039[41,] 0.002917515 0.002915446 0.994167039[42,] 0.002917515 0.002915446 0.994167039[43,] 0.994641852 0.002679074 0.002679074[44,] 0.002917515 0.002915446 0.994167039[45,] 0.002917515 0.002915446 0.994167039``

0 0