Skip to content

Instantly share code, notes, and snippets.

@dansondergaard
Created October 19, 2013 23:43
Show Gist options
  • Select an option

  • Save dansondergaard/7062952 to your computer and use it in GitHub Desktop.

Select an option

Save dansondergaard/7062952 to your computer and use it in GitHub Desktop.
R implementation of the Naive Bayes classifier example from [Wikipedia](http://en.wikipedia.org/wiki/Naive_Bayes_classifier#Examples).
# naivebayes.txt
#
# sex height weight footsize
# male 6 180 12
# male 5.92 190 11
# male 5.58 170 12
# male 5.92 165 10
# female 5 100 6
# female 5.5 150 8
# female 5.42 130 7
# female 5.75 150 9
library("data.table")
train <- function (dt) {
return(dt[,list(meanHeight=mean(height), varHeight=sd(height),
meanWeight=mean(weight), varWeight=sd(weight),
meanFootsize=mean(footsize), varFootsize=sd(footsize)), by=sex])
}
classify <- function (classifier, sample) {
posterior <- function (sample, class_prior, class) {
p_height <- dnorm(sample$height, class$meanHeight, class$varHeight)
p_weight <- dnorm(sample$weight, class$meanWeight, class$varWeight)
p_footsize <- dnorm(sample$footsize, class$meanFootsize, class$varFootsize)
return(class_prior * p_height * p_weight * p_footsize)
}
class_male <- classifier[which(classifier$sex == 'male'),]
class_female <- classifier[which(classifier$sex == 'female'),]
prior_male <- 0.5
prior_female <- 0.5
return(list(male=posterior(sample, prior_male, class_male),
female=posterior(sample, prior_female, class_female)))
}
training_set <- data.table(read.table('naivebayes.txt', header=TRUE))
sample = data.table(height=6, weight=130, footsize=8)
classifier <- train(training_set)
result = classify(classifier, sample)
cat('posterior(male) =', result$male)
cat('posterior(female) =', result$female)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment