- Machine Learning with Swift
- Alexander Sosnovshchenko
- 291字
- 2021-06-24 18:55:01
Evaluating performance of the model on iOS
I'm not describing here a .csv parsing in Swift; if you are interested in the details, please see the supplementary materials. Assuming that you've successfully loaded the test data in the form of two arrays, [Double] for features and [String] for labels, have a go at the following code:
let (xMat, yVec) = loadCSVData()
To create a decision tree and evaluate it, try this:
let sklDecisionTree = DecisionTree() let xSKLDecisionTree = xMat.map { (x: [Double]) -> DecisionTreeInput in return DecisionTreeInput(length: x[0], fluffy: x[1], color_light_black: x[2], color_pink_gold: x[3], color_purple_polka_dot: x[4], color_space_gray: x[5]) } let predictionsSKLTree = try! xSKLDecisionTree .map(sklDecisionTree.prediction) .map{ prediction in return prediction.label == "rabbosaurus" ? 0 : 1 } let groundTruth = yVec.map{ $0 == "rabbosaurus" ? 0 : 1 } let metricsSKLDecisionTree = evaluateAccuracy(yVecTest: groundTruth, predictions: predictionsSKLTree) print(metricsSKLDecisionTree)
To create a random forest and evaluate it, trying using the following code:
let sklRandomForest = RandomForest() let xSKLRandomForest = xMat.map { (x: [Double]) -> RandomForestInput in return RandomForestInput(length: x[0], fluffy: x[1], color_light_black: x[2], color_pink_gold: x[3], color_purple_polka_dot: x[4], color_space_gray: x[5]) } let predictionsSKLRandomForest = try! xSKLRandomForest.map(sklRandomForest.prediction).map{$0.label == "rabbosaurus" ? 0 : 1} let metricsSKLRandomForest = evaluateAccuracy(yVecTest: groundTruth, predictions: predictionsSKLRandomForest) print(metricsSKLRandomForest)
This is an example of how you can evaluate your model's prediction quality in the Swift application. The structure, that contains the results of evaluation is as follows:
struct Metrics: CustomStringConvertible { let confusionMatrix: [[Int]] let normalizedConfusionMatrix: [[Double]] let accuracy: Double let precision: Double let recall: Double let f1Score: Double var description: String { return """ Confusion Matrix: (confusionMatrix) Normalized Confusion Matrix: (normalizedConfusionMatrix) Accuracy: (accuracy) Precision: (precision) Recall: (recall) F1-score: (f1Score) """ } }
For the function for quality assessment, here's the code:
func evaluateAccuracy(yVecTest: [Int], predictions: [Int]) -> Metrics {
推薦閱讀
- 基于Proteus和Keil的C51程序設(shè)計(jì)項(xiàng)目教程(第2版):理論、仿真、實(shí)踐相融合
- 電腦常見(jiàn)問(wèn)題與故障排除
- 數(shù)字道路技術(shù)架構(gòu)與建設(shè)指南
- Effective STL中文版:50條有效使用STL的經(jīng)驗(yàn)(雙色)
- 嵌入式技術(shù)基礎(chǔ)與實(shí)踐(第5版)
- 精選單片機(jī)設(shè)計(jì)與制作30例(第2版)
- 計(jì)算機(jī)維修與維護(hù)技術(shù)速成
- Learning Game Physics with Bullet Physics and OpenGL
- 計(jì)算機(jī)組裝與維修技術(shù)
- CC2530單片機(jī)技術(shù)與應(yīng)用
- STM32嵌入式技術(shù)應(yīng)用開(kāi)發(fā)全案例實(shí)踐
- VMware Workstation:No Experience Necessary
- 數(shù)字媒體專(zhuān)業(yè)英語(yǔ)(第2版)
- 基于PROTEUS的電路設(shè)計(jì)、仿真與制板
- WebGL Hotshot