Contents

1 企?
2
3
4 code
5 谿瑚襭


1 企? #

企襦 覿襴 伎 蟆壱 襯 襦 襴(independence)企朱 螳 蠍磯覓語企. れ 覩語渚蟆襷 螳覲 蟇伎 襯 るジ 襯 レ 覩語 讌襷, 覓伎螻 蠏碁 蟆 蟇伎 襦 襴曙企手 螳螻(讀, 覓企 レ 譯殊 り 螳螻) 蟇伎 蟆壱 襯 蟲 蠏碁 襯螳 螻煙朱襷 伎 蟆壱 襯(joint probability)襯 蟲 蟆企. --http://abipictures.tistory.com/716

2 #

れ螻 螳 一危郁 .
Admit Gender Dept Freq
1  Admitted   Male    A  512
2  Rejected   Male    A  313
3  Admitted Female    A   89
4  Rejected Female    A   19

譟郁唄覿 襯 螻壱 覲企..
  • P(Admitted | Male, A) = 512 / (512 + 313) = 0.6206061
  • P(Admitted | Female, A) = 89 / (89 + 19) = 0.8240741
  • ...

library(e1071)
classifier <- naiveBayes(Admit ~ Gender + Dept, data=UCBAdmissions)
classifier

3 #

install.packages("klaR")
library("klaR")
data(iris)
model<- NaiveBayes(Species ~ ., data = iris)
plot(model)

install.packages("e1071")
library("e1071")

install.packages("mlbench") #HouseVotes84, 谿瑚襭襯 谿瑚.
library("mlbench")

data(HouseVotes84)
head(HouseVotes84)

model <- naiveBayes(Class ~ ., data = HouseVotes84)
predict(model, HouseVotes84[1:10,-1])
predict(model, HouseVotes84[1:10,-1], type = "raw")

pred <- predict(model, HouseVotes84[,-1])
table(pred, HouseVotes84$Class)

## Example of using a contingency table:
data(Titanic)
m <- naiveBayes(Survived ~ ., data = Titanic)
m
predict(m, as.data.frame(Titanic)[,1:3])

## Example with metric predictors:
data(iris)
m <- naiveBayes(Species ~ ., data = iris)
## alternatively:
m <- naiveBayes(iris[,-5], iris[,5])
m
table(predict(m, iris[,-5]), iris[,5])

4 code #

https://github.com/baotong/code-recipes/blob/master/R/naiveBayes.R
naiveBayes <- function(x, ...)
    UseMethod("naiveBayes")

naiveBayes.default <- function(x, y, laplace = 0, ...) {
    call <- match.call()
    Yname <- deparse(substitute(y))
    x <- as.data.frame(x)

    ## estimation-function
    est <- function(var)
        if (is.numeric(var)) {
            cbind(tapply(var, y, mean, na.rm = TRUE),
                  tapply(var, y, sd, na.rm = TRUE))
        } else {
            tab <- table(y, var)
            (tab + laplace) / (rowSums(tab) + laplace * nlevels(var))
        }

    ## create tables
    apriori <- table(y)
    tables <- lapply(x, est)

    ## fix dimname names
    for (i in 1:length(tables))
        names(dimnames(tables[[i]])) <- c(Yname, colnames(x)[i])
    names(dimnames(apriori)) <- Yname

    structure(list(apriori = apriori,
                   tables = tables,
                   levels = levels(y),
                   call   = call
                   ),

              class = "naiveBayes"
              )
}

naiveBayes.formula <- function(formula, data, laplace = 0, ...,
                               subset, na.action = na.pass) {
    call <- match.call()
    Yname <- as.character(formula[[2]])

    if (is.data.frame(data)) {
        ## handle formula
        m <- match.call(expand.dots = FALSE)
        m$... <- NULL
        m$laplace = NULL
        m$na.action <- na.action
        m[[1]] <- as.name("model.frame")
        m <- eval(m, parent.frame())
        Terms <- attr(m, "terms")
        if (any(attr(Terms, "order") > 1))
            stop("naiveBayes cannot handle interaction terms")
        Y <- model.extract(m, "response")
        X <- m[,-attr(Terms, "response"), drop = FALSE]

        return(naiveBayes(X, Y, laplace = laplace, ...))
    } else if (is.array(data)) {
        nam <- names(dimnames(data))
        ## Find Class dimension
        Yind <- which(nam == Yname)

        ## Create Variable index
        deps <- strsplit(as.character(formula)[3], ".[+].")[[1]]
        if (length(deps) == 1 && deps == ".")
            deps <- nam[-Yind]
        Vind <- which(nam %in% deps)

        ## create tables
        apriori <- margin.table(data, Yind)
        tables <- lapply(Vind,
                         function(i) (margin.table(data, c(Yind, i)) + laplace) /
                         (as.numeric(apriori) + laplace * dim(data)[i]))
        names(tables) <- nam[Vind]

        structure(list(apriori = apriori,
                       tables = tables,
                       levels = names(apriori),
                       call   = call
                       ),

                  class = "naiveBayes"
                  )
    } else stop("naiveBayes formula interface handles data frames or arrays only")

}


print.naiveBayes <- function(x, ...) {
    cat("\nNaive Bayes Classifier for Discrete Predictors\n\n")
    cat("Call:\n")
    print(x$call)
    cat("\nA-priori probabilities:\n")
    print(x$apriori / sum(x$apriori))

    cat("\nConditional probabilities:\n")
    for (i in x$tables) {print(i); cat("\n")}

}

predict.naiveBayes <- function(object,
                               newdata,
                               type = c("class", "raw"),
                               threshold = 0.001,
                               ...) {
    type <- match.arg(type)
    newdata <- as.data.frame(newdata)
    attribs <- which(names(object$tables) %in% names(newdata))
    isnumeric <- sapply(newdata, is.numeric)
    newdata <- data.matrix(newdata)
    L <- sapply(1:nrow(newdata), function(i) {
        ndata <- newdata[i,]
        L <- log(object$apriori) +
            apply(log(sapply(attribs, function(v) {
                nd <- ndata[v]
                if(is.na(nd))
                    rep(1, length(object$apriori))
                else {
                    prob <- if (isnumeric[v]) {
                        msd <- object$tables[[v]]
                        msd[,2][msd[,2]==0] <- threshold
                        dnorm(nd, msd[,1], msd[,2])
                    } else
                        object$tables[[v]][,nd]
                    prob[prob == 0] <- threshold
                    prob
                }
            })), 1, sum)
        if (type == "class")
            L
        else {
            L <- exp(L)
            L / sum(L)
        }
    })
    if (type == "class")
        factor(object$levels[apply(L, 2, which.max)], levels = object$levels)
    else
        t(L)
}

5 谿瑚襭 #