zoukankan      html  css  js  c++  java
  • 数据分析与挖掘

    案例三比较简单,不需要自己写公式算法,使用了R自带的naiveBayes函数。

    代码如下:

    > library(e1071)
    > classifier<-naiveBayes(iris[,1:4], iris[,5]) #或写成下面形式,都可以。 > classifier<- naiveBayes(Species ~ ., data = iris) #其中Species是类别变量 #预测 > predict(classifier, iris[1, -5])

    预测结果为:

    [1] setosa
    Levels: setosa versicolor virginica

    和原数据一样!

    *********************************这里是分割线**************************************

    我们再拿这个方法来预测一下案例一中的样本。

    #样本数据集:
    mydata <- matrix(c("sunny","hot","high","weak","no",  
                     "sunny","hot","high","strong","no",  
                     "overcast","hot","high","weak","yes",  
                     "rain","mild","high","weak","yes",  
                     "rain","cool","normal","weak","yes",  
                     "rain","cool","normal","strong","no",  
                     "overcast","cool","normal","strong","yes",  
                     "sunny","mild","high","weak","no",  
                     "sunny","cool","normal","weak","yes",  
                     "rain","mild","normal","weak","yes",  
                     "sunny","mild","normal","strong","yes",  
                     "overcast","mild","high","strong","yes",  
                     "overcast","hot","normal","weak","yes",  
                     "rain","mild","high","strong","no"), byrow = TRUE, nrow=14, ncol=5)
    
    #添加列名:
    colnames(mydata) <-  c("outlook","temperature","humidity","wind","playtennis")
    
    #贝叶斯算法:
    m<-naiveBayes(mydata[,1:4], mydata[,5]) 
    #或使用下面的方法
    m<- naiveBayes(playtennis ~ ., data = mydata)    
    #报错:Error in sum(x) : invalid 'type' (character) of argument 无效的类型,只能是数字? #创建预测数据集: new_data = data.frame(outlook="rain", temperature="cool", humidity="normal", wind="strong", playtennis="so") #预测: predict(m, new_data)

    在使用naiveBayes函数时报错:Error in sum(x) : invalid 'type' (character) of argument

    我们看一下官方文档,对data有这样一句描述:

    data  Either a data frame of predictors (categorical and/or numeric) or a contingency table.

    data是一个数字类型的数据框。

  • 相关阅读:
    获取父窗口的xxx节点的方法
    enumeration
    关于Java正则表达式的一些理解
    简单JNI的使用在Java中调用C库函数
    cursor管理
    [转]Vim配置与高级技巧
    [转]Vim+Taglist+Ctags
    在Windows下面使用cygwin将含有JNI的C文件编译成DLL文件
    vim转换大小写
    JMeter学习资料集锦
  • 原文地址:https://www.cnblogs.com/hunttown/p/5526786.html
Copyright © 2011-2022 走看看