Skip to content

Commit 8ac88ae

Browse files
author
ph1ps
committed
Change macOS playground to iOS
1 parent 44be0ab commit 8ac88ae

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

Naive Bayes Classifier/NaiveBayes.playground/Contents.swift

+48-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77

88
### Gaussian Naive Bayes
99
- Note:
10-
When using Gaussian NB you have to have continuous features (Double).
10+
When using Gaussian NB you have to have continuous features (Double).
1111

1212
For this example we are going to use a famous dataset with different types of wine. The labels of the features can be viewed [here](https://gist.github.com/tijptjik/9408623)
1313
*/
@@ -52,9 +52,54 @@ let data = wineData.map { row in
5252
*/
5353
let wineBayes = try! NaiveBayes(type: .gaussian, data: data, classes: classes).train()
5454
let result = wineBayes.classifyProba(with: [12.85, 1.6, 2.52, 17.8, 95, 2.48, 2.37, 0.26, 1.46, 3.93, 1.09, 3.63, 1015])
55-
print(result)
5655
/*:
57-
I can assure you that this is the correct result and as you can see the classifier thinks that its ***99.99%*** correct too.
56+
I can assure you that ***class 1*** is the correct result and as you can see the classifier thinks that its ***99.99%*** likely too.
5857

5958
### Multinomial Naive Bayes
59+
60+
- Note:
61+
When using Multinomial NB you have to have categorical features (Int).
62+
63+
Now this dataset is commonly used to describe the classification problem and it is categorical which means you don't have real values you just have categorical data as stated before. The structure of this dataset is as follows.
64+
65+
Outlook,Temperature,Humidity,Windy
66+
67+
***Outlook***: 0 = rainy, 1 = overcast, 2 = sunny
68+
69+
***Temperature***: 0 = hot, 1 = mild, 2 = cool
70+
71+
***Humidity***: 0 = high, 1 = normal
72+
73+
***Windy***: 0 = false, 1 = true
74+
75+
The classes are either he will play golf or not depending on the weather conditions. (0 = won't play, 1 = will play)
76+
*/
77+
78+
let golfData = [
79+
[0, 0, 0, 0],
80+
[0, 0, 0, 1],
81+
[1, 0, 0, 0],
82+
[2, 1, 0, 0],
83+
[2, 2, 1, 0],
84+
[2, 2, 1, 1],
85+
[1, 2, 1, 1],
86+
[0, 1, 0, 0],
87+
[0, 2, 1, 0],
88+
[2, 1, 1, 0],
89+
[0, 1, 1, 1],
90+
[1, 1, 0, 1],
91+
[1, 0, 1, 0],
92+
[2, 1, 0, 1]
93+
]
94+
let golfClasses = [0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0]
95+
96+
let golfNaive = try! NaiveBayes(type: .multinomial, data: golfData, classes: golfClasses).train()
97+
98+
/*:
99+
The weather conditions is as follows now: Outlook=rainy, Temperature=cool, Humidity=high, Windy=true
100+
*/
101+
let golfResult = golfNaive.classifyProba(with: [0, 2, 0, 1])
102+
103+
/*:
104+
Naive Bayes tells us that the golf player will ***not*** play with a likelihood of almost ***80%***. Which is true of course.
60105
*/
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='macos' display-mode='raw'>
2+
<playground version='5.0' target-platform='ios' display-mode='raw'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "#CharacterRangeLen=1&amp;CharacterRangeLoc=2051&amp;EndingColumnNumber=14&amp;EndingLineNumber=53&amp;StartingColumnNumber=1&amp;StartingLineNumber=53&amp;Timestamp=514147811.827527"
7+
selectedRepresentationIndex = "0"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
<LoggerValueHistoryTimelineItem
11+
documentLocation = "#CharacterRangeLen=6&amp;CharacterRangeLoc=1936&amp;EndingColumnNumber=11&amp;EndingLineNumber=53&amp;StartingColumnNumber=5&amp;StartingLineNumber=53&amp;Timestamp=514147811.848778"
12+
selectedRepresentationIndex = "0"
13+
shouldTrackSuperviewWidth = "NO">
14+
</LoggerValueHistoryTimelineItem>
15+
<LoggerValueHistoryTimelineItem
16+
documentLocation = "#CharacterRangeLen=10&amp;CharacterRangeLoc=3424&amp;EndingColumnNumber=15&amp;EndingLineNumber=100&amp;StartingColumnNumber=5&amp;StartingLineNumber=100&amp;Timestamp=514149150.852782"
17+
selectedRepresentationIndex = "0"
18+
shouldTrackSuperviewWidth = "NO">
19+
</LoggerValueHistoryTimelineItem>
20+
</TimelineItems>
21+
</Timeline>

0 commit comments

Comments
 (0)