Wednesday, June 7, 2017

Naive Bayes - Simple

Naive Bayes - Toy

Problem: Classify whether a given person is a male or a female based on the measured features. The features include height, weight, and foot size.

Data Source : https://en.wikipedia.org/wiki/Naive_Bayes_classifier

Step 1: read human data

human <- read.csv("human.csv")

Step 2 : Prepare data and explore data

human
##      sex height weight foot.size
## 1   male   6.00    180        12
## 2   male   5.92    190        11
## 3   male   5.58    170        12
## 4   male   5.92    165        10
## 5 female   5.00    100         6
## 6 female   5.50    150         8
## 7 female   5.42    130         7
## 8 female   5.75    150         9
summary(human)
##      sex        height          weight        foot.size     
##  female:4   Min.   :5.000   Min.   :100.0   Min.   : 6.000  
##  male  :4   1st Qu.:5.480   1st Qu.:145.0   1st Qu.: 7.750  
##             Median :5.665   Median :157.5   Median : 9.500  
##             Mean   :5.636   Mean   :154.4   Mean   : 9.375  
##             3rd Qu.:5.920   3rd Qu.:172.5   3rd Qu.:11.250  
##             Max.   :6.000   Max.   :190.0   Max.   :12.000

Step 3 : Test Data set

sex = c("sample")
height = c(6)
weight = c(130)
foot.size = c(8)
# sample is female
human_test <- data.frame(sex,height,weight,foot.size)

Step 4 : Training a naive bayes Model on training dataset

library(e1071)
model <- naiveBayes(sex ~ ., data = human)

Step 5 : Eveluate the performace of the model

pred_sex <- predict(model, newdata = human_test)
pred_sex
## [1] female
## Levels: female male

No comments:

Post a Comment

Naive Bayes - Simple

Naive Bayes - Toy Problem: Classify whether a given person is a male or a female based...