/// <summary> /// Расчет классификации по тестовому обучающему на одном классификаторе /// </summary> /// <param name="batch"></param> /// <returns>Количество деревьев, проголосовавших за каждый класс</returns> private Dictionary <string, double> GetTrainClassificationCounts(DecisionBatch batch) { var probDict = new Dictionary <string, double>(); int lines = _trainLoader.TotalDataLines; int vars = _trainLoader.NVars; // пробегаем по всем клиентски данным и сохраняем результат for (int i = 0; i < lines; i++) { var cdata = new double[vars]; for (int j = 0; j < vars; j++) { cdata[j] = _trainLoader.LearnRows[i, j]; } //var y = batch.PredictCounts(cdata); var y = batch.PredictProba(cdata); string id = i.ToString(); if (!probDict.ContainsKey(id)) { probDict.Add(id, y[1]); } } return(probDict); }
/// <summary> /// Расчет классификации по тестовому множеству на одном классификаторе /// ипользуется в GetTestSetMetrics /// </summary> /// <param name="batch"></param> /// <returns>Количество деревьев, проголосовавших за каждый класс</returns> private Dictionary <string, double> GetTestClassificationCounts(DecisionBatch batch) { var probDict = new Dictionary <string, double>(); // пробегаем по всем клиентски данным и сохраняем результат foreach (string id in _testDataDict.Keys) { //var y = batch.PredictCounts(_testDataDict[id]); var y = batch.PredictProba(_testDataDict[id]); if (!probDict.ContainsKey(id)) { probDict.Add(id, y[1]); } } return(probDict); }
private static void CalcOutOfTheBagMetrics(List <DecisionTree> treeList, double[,] xy, DecisionBatch batch, bool useLogit) { var rdict = new Dictionary <int, int>(); foreach (var tree in treeList) { foreach (int id in tree.RowIndexes) { if (!rdict.ContainsKey(id)) { rdict.Add(id, 0); } rdict[id]++; } } int npoints = xy.GetLength(0); int nvars = xy.GetLength(1) - 1; int tnpoints = npoints - rdict.Count; var rlist = new RocItem[tnpoints]; // массив для оценки результата var ntrain = new double[tnpoints, batch.CountTreesInBatch + 1]; // массив для оценки результата double accCoeff = rdict.Count / (double)npoints; for (int i = 0, k = 0; i < npoints; i++) { if (rdict.ContainsKey(i)) { continue; } var tobj = new double[nvars]; for (int j = 0; j < nvars; j++) { tobj[j] = xy[i, j]; } rlist[k] = new RocItem(); double[] tprob = batch.PredictProba(tobj); rlist[k].Prob = tprob[1]; rlist[k].Predicted = tprob[1] > 0.5?1:0; rlist[k].Target = Convert.ToInt32(xy[i, nvars]); double[] bytree = batch.PredictByTree(tobj); for (int j = 0; j < batch.CountTreesInBatch; j++) { ntrain[k, j] = bytree[j]; } ntrain[k, batch.CountTreesInBatch] = xy[i, nvars]; k++; } Array.Sort(rlist, (o1, o2) => (1 - o1.Prob).CompareTo(1 - o2.Prob)); batch.OutBagEstimations = ResultCalc.GetResult(rlist, 0.05); if (useLogit) { int info; alglib.mnlreport rep; alglib.mnltrainh(ntrain, tnpoints, batch.CountTreesInBatch, 2, out info, out batch._logitModel, out rep); for (int i = 0, k = 0; i < npoints; i++) { if (rdict.ContainsKey(i)) { continue; } var tobj = new double[nvars]; for (int j = 0; j < nvars; j++) { tobj[j] = xy[i, j]; } rlist[k] = new RocItem(); double[] tprob = batch.PredictProba(tobj); rlist[k].Prob = tprob[1]; rlist[k].Predicted = tprob[1] > 0.5 ? 1 : 0; rlist[k].Target = Convert.ToInt32(xy[i, nvars]); k++; } Array.Sort(rlist, (o1, o2) => (1 - o1.Prob).CompareTo(1 - o2.Prob)); batch.OutBagEstimationsLogit = ResultCalc.GetResult(rlist, 0.05); } Logger.Log("train used: " + accCoeff + "; out_of_bag:" + batch.OutBagEstimations.AUC + "; out_of_bag_logit:" + batch.OutBagEstimationsLogit?.AUC); }
/// <summary> /// Расчет классификации по тестовому обучающему на одном классификаторе /// </summary> /// <param name="batch"></param> /// <returns>Количество деревьев, проголосовавших за каждый класс</returns> private Dictionary<string, double> GetTrainClassificationCounts(DecisionBatch batch) { var probDict = new Dictionary<string, double>(); int lines = _trainLoader.TotalDataLines; int vars = _trainLoader.NVars; // пробегаем по всем клиентски данным и сохраняем результат for (int i = 0; i < lines; i++) { var cdata = new double[vars]; for (int j = 0; j < vars; j++) cdata[j] = _trainLoader.LearnRows[i, j]; //var y = batch.PredictCounts(cdata); var y = batch.PredictProba(cdata); string id = i.ToString(); if (!probDict.ContainsKey(id)) probDict.Add(id, y[1]); } return probDict; }
/// <summary> /// Расчет классификации по тестовому множеству на одном классификаторе /// ипользуется в GetTestSetMetrics /// </summary> /// <param name="batch"></param> /// <returns>Количество деревьев, проголосовавших за каждый класс</returns> private Dictionary<string, double> GetTestClassificationCounts(DecisionBatch batch) { var probDict = new Dictionary<string, double>(); // пробегаем по всем клиентски данным и сохраняем результат foreach (string id in _testDataDict.Keys) { //var y = batch.PredictCounts(_testDataDict[id]); var y = batch.PredictProba(_testDataDict[id]); if (!probDict.ContainsKey(id)) probDict.Add(id, y[1]); } return probDict; }
private static void CalcOutOfTheBagMetrics(List<DecisionTree> treeList, double[,] xy, DecisionBatch batch, bool useLogit) { var rdict = new Dictionary<int, int>(); foreach (var tree in treeList) { foreach (int id in tree.RowIndexes) { if (!rdict.ContainsKey(id)) rdict.Add(id, 0); rdict[id]++; } } int npoints = xy.GetLength(0); int nvars = xy.GetLength(1) - 1; int tnpoints = npoints - rdict.Count; var rlist = new RocItem[tnpoints]; // массив для оценки результата var ntrain = new double[tnpoints, batch.CountTreesInBatch+1]; // массив для оценки результата double accCoeff = rdict.Count / (double)npoints; for (int i=0, k=0; i < npoints; i++) { if (rdict.ContainsKey(i)) continue; var tobj = new double[nvars]; for (int j = 0; j < nvars; j++) tobj[j] = xy[i, j]; rlist[k] = new RocItem(); double[] tprob = batch.PredictProba(tobj); rlist[k].Prob = tprob[1]; rlist[k].Predicted = tprob[1]>0.5?1:0; rlist[k].Target = Convert.ToInt32(xy[i, nvars]); double[] bytree = batch.PredictByTree(tobj); for (int j = 0; j < batch.CountTreesInBatch; j++) ntrain[k, j] = bytree[j]; ntrain[k, batch.CountTreesInBatch] = xy[i, nvars]; k++; } Array.Sort(rlist, (o1, o2) => (1 - o1.Prob).CompareTo(1 - o2.Prob)); batch.OutBagEstimations = ResultCalc.GetResult(rlist, 0.05); if (useLogit) { int info; alglib.mnlreport rep; alglib.mnltrainh(ntrain, tnpoints, batch.CountTreesInBatch, 2, out info, out batch._logitModel, out rep); for (int i = 0, k = 0; i < npoints; i++) { if (rdict.ContainsKey(i)) continue; var tobj = new double[nvars]; for (int j = 0; j < nvars; j++) tobj[j] = xy[i, j]; rlist[k] = new RocItem(); double[] tprob = batch.PredictProba(tobj); rlist[k].Prob = tprob[1]; rlist[k].Predicted = tprob[1] > 0.5 ? 1 : 0; rlist[k].Target = Convert.ToInt32(xy[i, nvars]); k++; } Array.Sort(rlist, (o1, o2) => (1 - o1.Prob).CompareTo(1 - o2.Prob)); batch.OutBagEstimationsLogit = ResultCalc.GetResult(rlist, 0.05); } Logger.Log("accCoeff: " + accCoeff + "; bag:" + batch.OutBagEstimations.AUC + "; logit:" + batch.OutBagEstimationsLogit?.AUC); }