forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveBayes.swift
196 lines (140 loc) · 5.07 KB
/
NaiveBayes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// NaiveBayes.swift
// NaiveBayes
//
// Created by Philipp Gabriel on 14.04.17.
// Copyright © 2017 ph1ps. All rights reserved.
//
import Foundation
extension String: Error {}
extension Array where Element == Double {
func mean() -> Double {
return self.reduce(0, +) / Double(count)
}
func standardDeviation() -> Double {
let calculatedMean = mean()
let sum = self.reduce(0.0) { (previous, next) in
return previous + pow(next - calculatedMean, 2)
}
return sqrt(sum / Double(count - 1))
}
}
extension Array where Element == Int {
func uniques() -> Set<Element> {
return Set(self)
}
}
enum NBType {
case gaussian
case multinomial
//case bernoulli --> TODO
func calcLikelihood(variables: [Any], input: Any) -> Double? {
if case .gaussian = self {
guard let input = input as? Double else {
return nil
}
guard let mean = variables[0] as? Double else {
return nil
}
guard let stDev = variables[1] as? Double else {
return nil
}
let eulerPart = pow(M_E, -1 * pow(input - mean, 2) / (2 * pow(stDev, 2)))
let distribution = eulerPart / sqrt(2 * .pi) / stDev
return distribution
} else if case .multinomial = self {
guard let variables = variables as? [(category: Int, probability: Double)] else {
return nil
}
guard let input = input as? Int else {
return nil
}
return variables.first { variable in
return variable.category == input
}?.probability
}
return nil
}
func train(values: [Any]) -> [Any]? {
if case .gaussian = self {
guard let values = values as? [Double] else {
return nil
}
return [values.mean(), values.standardDeviation()]
} else if case .multinomial = self {
guard let values = values as? [Int] else {
return nil
}
let count = values.count
let categoryProba = values.uniques().map { value -> (Int, Double) in
return (value, Double(values.filter { $0 == value }.count) / Double(count))
}
return categoryProba
}
return nil
}
}
class NaiveBayes<T> {
var variables: [Int: [(feature: Int, variables: [Any])]]
var type: NBType
var data: [[T]]
var classes: [Int]
init(type: NBType, data: [[T]], classes: [Int]) throws {
self.type = type
self.data = data
self.classes = classes
self.variables = [Int: [(Int, [Any])]]()
if case .gaussian = type, T.self != Double.self {
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)"
}
}
func train() throws -> Self {
for `class` in classes.uniques() {
variables[`class`] = [(Int, [Any])]()
let classDependent = data.enumerated().filter { (offset, _) in
return classes[offset] == `class`
}
for feature in 0..<data[0].count {
let featureDependent = classDependent.map { $0.element[feature] }
guard let trained = type.train(values: featureDependent) else {
throw "Critical! Data could not be casted even though it was checked at init"
}
variables[`class`]?.append((feature, trained))
}
}
return self
}
func classify(with input: [T]) -> Int {
let likelihoods = classifyProba(with: input).max { (first, second) -> Bool in
return first.1 < second.1
}
guard let `class` = likelihoods?.0 else {
return -1
}
return `class`
}
func classifyProba(with input: [T]) -> [(Int, Double)] {
var probaClass = [Int: Double]()
let amount = classes.count
classes.forEach { `class` in
let individual = classes.filter { $0 == `class` }.count
probaClass[`class`] = Double(individual) / Double(amount)
}
let classesAndFeatures = variables.map { (`class`, value) -> (Int, [Double]) in
let distribution = value.map { (feature, variables) -> Double in
return type.calcLikelihood(variables: variables, input: input[feature]) ?? 0.0
}
return (`class`, distribution)
}
let likelihoods = classesAndFeatures.map { (`class`, distribution) in
return (`class`, distribution.reduce(1, *) * (probaClass[`class`] ?? 0.0))
}
let sum = likelihoods.map { $0.1 }.reduce(0, +)
let normalized = likelihoods.map { (`class`, likelihood) in
return (`class`, likelihood / sum)
}
return normalized
}
}