February 2018
Intermediate to advanced
378 pages
10h 14m
English
We'll use a straightforward approach here to calculate the confusion matrix; however, this would not work for multiclass classification. Here, p stands for predicted value, and t is for ground truth:
let pairs: [(Int, Int)] = zip(predictions, yVecTest).map{ ($0.0, $0.1) }
var confusionMatrix = [[0,0], [0,0]]
for (p, t) in pairs {
switch (p, t) {
case (0, 0):
confusionMatrix[0][0] += 1
case (0, _):
confusionMatrix[1][0] += 1
case (_, 0):
confusionMatrix[0][1] += 1
case (_, _):
confusionMatrix[1][1] += 1
}
}
let totalCount = Double(yVecTest.count)
Normalize the matrix by total count:
let normalizedConfusionMatrix = confusionMatrix.map{$0.map{Double($0)/totalCount}}
As we already know, accuracy is a number ...
Read now
Unlock full access