# naive bayes alogrithm
# Xindong Wu, top ten algorithms for data mining, ch9, exercise 1
# author: nullspace(jxhchina at gmail.com)
# last updated: 10:34 2013/11/2
library(MASS)
source('readkey.R')
n <- 100
sigma1 <- matrix(c(1, 0, 0, 1), 2, 2)
sigma1
mu1 <- c(0, 0)
d1 <- mvrnorm(n, mu1, sigma1)
sigma2 <- matrix(c(1, 0, 0, 2), 2, 2)
sigma2
mu2 <- c(2, 2)
d2 <- mvrnorm(n, mu2, sigma2)
d <- rbind(cbind(d1, 1), cbind(d2, 2))
dim(d)
minxy <- apply(d[,1:2], 2, min)
maxxy <- apply(d[,1:2], 2, max)
minmaxxy <- rbind(minxy, maxxy)
plot.new()
plot(d1, col='red', xlim=minmaxxy[,1], ylim=minmaxxy[,2]) # new=FALSE|TRUE not work
points(d2, col='blue')
df <- data.frame(d)
df[,3] <- factor(df[,3])
is.factor(df[,3])
# ratio of prior of class 1 and 2
r <- (sum(df[,3] == 1) / sum(df[,3] == 2))
# margin distribution of x1, x2
# suppose both of them belong to
Naive Bayes 算法,R 实现,二元正态样本 demo
最新推荐文章于 2021-07-17 09:50:55 发布