• 数据分析与挖掘


    案例三比较简单,不需要自己写公式算法,使用了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是一个数字类型的数据框。

  • 相关阅读:
    用ps 查看线程状态
    机器学习资料收集
    [转]漫谈数据中心CLOS网络架构
    MBR中“起始磁头/扇区/柱面“同"逻辑区块地址(LBA)"的区别
    [转]硬盘分区表知识——详解硬盘MBR
    [转]什么是总线?什么是前端总线?
    语言的编译-汇编-链接
    计算机进行小数运算会出错

    计算机底层是如何访问显卡的?
  • 原文地址:https://www.cnblogs.com/hunttown/p/5526786.html
Copyright © 2020-2023  润新知