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是一个数字类型的数据框。

  • 相关阅读:
    eclipse环境:把jdk1.6 改 jdk1.7或jdk1.8(改回也可以)(图文详解)
    SVN 将文件还原到之前的指定版本
    用起来很方便的枚举扩展类
    VS2015新功能
    EasyUi 动态列
    基于EasyUi的快速开发框架
    深圳某保险公司招聘职位列表
    考勤系统之计算工作小时数
    考勤系统之状态管理
    第一次裁员
  • 原文地址:https://www.cnblogs.com/hunttown/p/5526786.html
Copyright © 2011-2022 走看看