public static double[] CalculateBiases_Old <L>(this Sphere <L> sphere, Vector vector, RHCLib.DistanceDelegate measure, IList <L> labels) { SortedDictionary <L, double?> dictClasses = new SortedDictionary <L, double?>(); foreach (L label in labels) { dictClasses.Add(label, null); } double fProportion = 1.0; int nCount = dictClasses.Count; bool bFirstSphere = true; RHCLib.Sphere <L> sphereIteration = sphere.Recognize(vector, measure, ParallelStrategy.SingleThreaded); while (sphereIteration != null && fProportion > 0.0 && nCount > 0) { if (bFirstSphere) { if (dictClasses.ContainsKey(sphereIteration.Label)) { #region Linear dictClasses[sphereIteration.Label] = fProportion - (measure(sphereIteration, vector) * (0.5 / sphereIteration.Radius)); #endregion fProportion -= dictClasses[sphereIteration.Label].Value; bFirstSphere = false; nCount--; } } else { if (dictClasses.ContainsKey(sphereIteration.Label) && !dictClasses[sphereIteration.Label].HasValue) { dictClasses[sphereIteration.Label] = fProportion; nCount--; } #region Linear fProportion -= fProportion * ((sphereIteration.Radius - measure(sphereIteration, vector)) / sphereIteration.Radius); #endregion } sphereIteration = sphereIteration.Parent; } double fSum = dictClasses.Values.Sum(v => v.HasValue ? v.Value : 0.0); return(dictClasses.Values.Select(v => v.HasValue ? v.Value / fSum : 0.0).ToArray()); }
public static double[] CalculateBiases <L>(this Sphere <L> sphere, Vector vector, RHCLib.DistanceDelegate measure, IList <L> labels) { SortedDictionary <L, double?> dictClasses = new SortedDictionary <L, double?>(); foreach (L label in labels) { dictClasses.Add(label, null); } double fProportion = 1.0; int nCount = dictClasses.Count; bool bFirstSphere = true; SphereEx <L> sphLDA; RHCLib.Sphere <L> sphereIteration = sphere.Recognize(vector, measure, ParallelStrategy.SingleThreaded); while (sphereIteration != null && fProportion > 0.0 && nCount > 0) { if (bFirstSphere) { if (dictClasses.ContainsKey(sphereIteration.Label)) { if ((sphLDA = sphereIteration as SphereEx <L>) != null && sphLDA.DiscriminantEx != null) { #region LDA (Uses LDAEx) // You have four cases you need to watch for... // /|\ /|\ 1.0 // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \/ | \ // |---------M---------D--------M---------| 0.5 // Equation: 1 - (h)(x_i) <-- 1 = fProportion because haven't gotten out yet Func <double, double, double> slope = (x1, x2) => { return(0.5 / Math.Abs(x2 - x1)); }; //double[][] data = LDA.MatrixFromVector(vector.Features); //double[][] wTx = LDA.MatrixProduct(sphLDA.Discriminant.Transposed, data); // Project the data double proj = Accord.Math.Matrix.Dot(sphLDA.DiscriminantEx.ProjectionVector, vector.Features); if (proj <= sphLDA.DiscriminantEx.ProjectedLeftMean) { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.DiscriminantEx.ProjectedLeftMean, sphLDA.DiscriminantEx.ProjectedSetMean - sphLDA.Radius) * (sphLDA.DiscriminantEx.ProjectedLeftMean - proj)); } else if (proj > sphLDA.DiscriminantEx.ProjectedLeftMean && proj <= sphLDA.DiscriminantEx.ProjectedSetMean) { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.DiscriminantEx.ProjectedSetMean, sphLDA.DiscriminantEx.ProjectedLeftMean) * (proj - sphLDA.DiscriminantEx.ProjectedLeftMean)); } else if (proj > sphLDA.DiscriminantEx.ProjectedSetMean && proj <= sphLDA.DiscriminantEx.ProjectedRightMean) { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.DiscriminantEx.ProjectedRightMean, sphLDA.DiscriminantEx.ProjectedSetMean) * (sphLDA.DiscriminantEx.ProjectedRightMean - proj)); } else { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.DiscriminantEx.ProjectedSetMean + sphLDA.Radius, sphLDA.DiscriminantEx.ProjectedRightMean) * (proj - sphLDA.DiscriminantEx.ProjectedRightMean)); } #endregion } else if (sphLDA != null && sphLDA.Discriminant != null) { #region Old LDA // You have four cases you need to watch for... // /|\ /|\ 1.0 // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \ / | \ // / | \/ | \ // |---------M---------D--------M---------| 0.5 // Equation: 1 - h(x_i) Func <double, double, double> slope = (x1, x2) => { return(0.5 / Math.Abs(x2 - x1)); }; double[][] data = LDA.MatrixFromVector(vector.Features); double[][] wTx = LDA.MatrixProduct(sphLDA.Discriminant.Transposed, data); // Project the data if (wTx[0][0] <= sphLDA.Discriminant.ProjectedMeanLeft) { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.Discriminant.ProjectedMeanLeft, 0.0) * (sphLDA.Discriminant.ProjectedMeanLeft - wTx[0][0])); } else if (wTx[0][0] > sphLDA.Discriminant.ProjectedMeanLeft && wTx[0][0] <= sphLDA.Discriminant.DecisionPoint) { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.Discriminant.DecisionPoint, sphLDA.Discriminant.ProjectedMeanLeft) * (wTx[0][0] - sphLDA.Discriminant.ProjectedMeanLeft)); } else if (wTx[0][0] > sphLDA.Discriminant.DecisionPoint && wTx[0][0] <= sphLDA.Discriminant.ProjectMeanRight) { dictClasses[sphLDA.Label] = fProportion - (slope(sphLDA.Discriminant.ProjectMeanRight, sphLDA.Discriminant.DecisionPoint) * (sphLDA.Discriminant.ProjectMeanRight - wTx[0][0])); } else { dictClasses[sphLDA.Label] = fProportion - (slope(2 * sphLDA.Radius, sphLDA.Discriminant.ProjectMeanRight) * (wTx[0][0] - sphLDA.Discriminant.ProjectMeanRight)); } #endregion } else { #region Linear dictClasses[sphereIteration.Label] = fProportion - (measure(sphereIteration, vector) * (0.5 / sphereIteration.Radius)); #endregion } fProportion -= dictClasses[sphereIteration.Label].Value; bFirstSphere = false; nCount--; } } else { if (dictClasses.ContainsKey(sphereIteration.Label) && !dictClasses[sphereIteration.Label].HasValue) { dictClasses[sphereIteration.Label] = fProportion; nCount--; } #region Linear // Impossible to have LDA in node that's not a leaf. fProportion -= fProportion * ((sphereIteration.Radius - measure(sphereIteration, vector)) / sphereIteration.Radius); #endregion } sphereIteration = sphereIteration.Parent; } double fSum = dictClasses.Values.Sum(v => v.HasValue ? v.Value : 0.0); return(dictClasses.Values.Select(v => v.HasValue ? v.Value / fSum : 0.0).ToArray()); }