/// <summary> /// Assumes that higher (or equal) to calculated score compared to threshold will give higher trueScore /// calculatedScore and true Score, both are of the same size /// </summary> /// <param name="calculatedScore"></param> /// <param name="trueScore"></param> /// <param name="stepSize"></param> /// <returns></returns> public static OptimumBinClassRetType CalculateOptimumBinClassificationAndAccuracy(List <double> calculatedScore, List <int> trueScore, double stepSize) { OptimumBinClassRetType optimumBinClassRetType = new OptimumBinClassRetType(Double.MinValue, 0.0); double min = (from c in calculatedScore select c).Min(); double max = (from c in calculatedScore select c).Max(); int hit, maxHit = int.MinValue; for (double d = min; d <= max; d += stepSize) { hit = 0; for (int i = 0; i < calculatedScore.Count; i++)//traverse all calculatedScore elements. It might not be sorted { if (calculatedScore[i] >= d) { hit += trueScore[i] == 1 ? 1 : 0;//above partition. If truescore is 1, it a hit. } else { hit += trueScore[i] == 0 ? 1 : 0; } } //compare and update if applicable if (hit > maxHit) { maxHit = hit; optimumBinClassRetType.optimumPoint = d; } } optimumBinClassRetType.accuracyPercentage = ((double)maxHit / calculatedScore.Count); return(optimumBinClassRetType); }
private void button1_Click(object sender, EventArgs e) { double stepSize = 1.0; txtBxOutput.AppendText("\nBegin binary classification optimisation with step size " + stepSize + "\n"); double [] x = new double[] { 8, 7, 6, 5.5, 5.5, 5, 5, 4, 2, 1 }; int [] y = new int[] { 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 }; txtBxOutput.AppendText("\nThe calculated score is:\n"); txtBxOutput.AppendText(Utility.getArrayMemberAsString(x)); txtBxOutput.AppendText("\nThe trueRisk/binary result is:\n"); txtBxOutput.AppendText(Utility.getArrayMemberAsString(y)); DataMining.OptimumBinClassRetType opbc = DataMining.BasicDBMining.CalculateOptimumBinClassificationAndAccuracy(x.ToList(), y.ToList(), stepSize); txtBxOutput.AppendText("\n\nThreshold " + opbc.optimumPoint + ". Accuracy: " + opbc.accuracyPercentage); }