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

  • 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 { 
主站蜘蛛池模板: 乌海市| 鹤壁市| 兰西县| 襄城县| 舟山市| 庆阳市| 清涧县| 金川县| 大安市| 陆川县| 嘉定区| 同心县| 砚山县| 宜黄县| 安顺市| 汕头市| 凌云县| 博乐市| 资溪县| 北安市| 凯里市| 佛教| 长阳| 大同县| 靖安县| 华阴市| 博兴县| 托克托县| 鲁山县| 雷州市| 措勤县| 大荔县| 沈丘县| 寿宁县| 海城市| 沁源县| 盱眙县| 商都县| 固原市| 桂阳县| 南通市|