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

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 { 
主站蜘蛛池模板: 甘洛县| 若尔盖县| 临邑县| 新宾| 平陆县| 长兴县| 凌源市| 固阳县| 克拉玛依市| 海兴县| 响水县| 高平市| 林周县| 隆回县| 望奎县| 马尔康县| 泰州市| 周至县| 樟树市| 武安市| 格尔木市| 龙州县| 邳州市| 瓦房店市| 竹山县| 苗栗县| 大同市| 桑植县| 波密县| 永康市| 崇明县| 双峰县| 丹凤县| 郎溪县| 利津县| 嵩明县| 合作市| 汽车| 沙坪坝区| 古浪县| 防城港市|