官术网_书友最值得收藏!

Calculating the confusion matrix

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 of true predictions divided by the total number of cases.

To calculate accuracy, try using the following code:

let truePredictionsCount = pairs.filter{ $0.0 == $0.1 }.count 
let accuracy = Double(truePredictionsCount) / totalCoun  

To calculate true positive, false positive, and false negative counts, you can use the numbers from the confusion matrix, but let's do it the proper way:

 
let truePositive = Double(pairs.filter{ $0.0 == $0.1 && $0.0 == 0 }.count) 
let falsePositive = Double(pairs.filter{ $0.0 != $0.1 && $0.0 == 0 }.count) 
let falseNegative = Double(pairs.filter{ $0.0 != $0.1 && $0.0 == 1 }.count) 

To calculate precision:

let precision = truePositive / (truePositive + falsePositive) 

To calculate recall:

let recall = truePositive / (truePositive + falseNegative) 

To calculate F1-score:

let f1Score = 2 * precision * recall / (precision + recall) 
     
return Metrics(confusionMatrix: confusionMatrix, normalizedConfusionMatrix: normalizedConfusionMatrix, accuracy: accuracy, precision: precision, recall: recall, f1Score: f1Score) 
} 

Here is my result for the decision tree on iOS:

Confusion Matrix: 
[[135, 17],  
[20, 128]] 
 
Normalized Confusion Matrix: 
[[0.45000000000000001, 0.056666666666666664],  
[0.066666666666666666, 0.42666666666666669]] 
 
Accuracy: 0.876666666666667 
Precision: 0.870967741935484 
Recall: 0.888157894736842 
F1-score: 0.879478827361563 
 

And for the random forest:

Confusion Matrix: 
[[138, 14],  
[18, 130]] 
 
Normalized Confusion Matrix: 
[[0.46000000000000002, 0.046666666666666669],  
[0.059999999999999998, 0.43333333333333335]] 
 
Accuracy: 0.893333333333333 
Precision: 0.884615384615385 
Recall: 0.907894736842105 
F1-score: 0.896103896103896 

Congratulations! We've trained two machine learning algorithms, deployed them to the iOS, and evaluated their accuracy. Interesting that while decision tree metrics match perfectly, the random forest performance is slightly worse on Core ML. Don't forget to always validate your model after any type of conversion.

主站蜘蛛池模板: 沿河| 河南省| 乌拉特后旗| 新巴尔虎左旗| 佳木斯市| 鄂伦春自治旗| 平昌县| 化德县| 平南县| 康平县| 衡东县| 轮台县| 衡山县| 台江县| 墨脱县| 义马市| 高要市| 安宁市| 万全县| 逊克县| 湘潭市| 龙山县| 鹤庆县| 吴忠市| 丰台区| 丰县| 商河县| 五莲县| 神农架林区| 鞍山市| 鄂伦春自治旗| 讷河市| 杭锦后旗| 北票市| 呼伦贝尔市| 濮阳县| 龙江县| 商洛市| 罗江县| 乳山市| 策勒县|