Skip to content

Update NaiveBayes.swift #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion Naive Bayes Classifier/NaiveBayes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum NBType {

case gaussian
case multinomial
//case bernoulli --> TODO
case bernoulli

func calcLikelihood(variables: [Any], input: Any) -> Double? {

Expand Down Expand Up @@ -76,6 +76,22 @@ enum NBType {
return variable.category == input
}?.probability

} else if case .bernoulli = self {

guard let variables = variables as? [(category: Int, probability: Double)] else {
return nil
}

guard let input = input as? Bool else {
return nil
}

let probability = variables.first { variable in
return variable.category == (input ? 1 : 0)
}?.probability ?? 0.0

return probability

}

return nil
Expand All @@ -102,6 +118,17 @@ enum NBType {
return (value, Double(values.filter { $0 == value }.count) / Double(count))
}
return categoryProba

} else if case .bernoulli = self {

guard let values = values as? [Bool] else {
return nil
}

let count = values.count
let categoryProba = [(0, 1.0 - Double(values.filter { $0 }.count) / Double(count)),
(1, Double(values.filter { $0 }.count) / Double(count))]
return categoryProba
}

return nil
Expand All @@ -126,6 +153,8 @@ class NaiveBayes<T> {
throw "When using Gaussian NB you have to have continuous features (Double)"
} else if case .multinomial = type, T.self != Int.self {
throw "When using Multinomial NB you have to have categorical features (Int)"
} else if case .bernoulli = type, T.self != Double.self {
throw "When using Bernoulli NB you have to have continuous features (Double)"
}
}

Expand Down