public static IClassificationSolution CreateLogitClassificationSolution(IClassificationProblemData problemData, out double rmsError, out double relClassError) { var dataset = problemData.Dataset; string targetVariable = problemData.TargetVariable; var doubleVariableNames = problemData.AllowedInputVariables.Where(dataset.VariableHasType <double>); var factorVariableNames = problemData.AllowedInputVariables.Where(dataset.VariableHasType <string>); IEnumerable <int> rows = problemData.TrainingIndices; double[,] inputMatrix = dataset.ToArray(doubleVariableNames.Concat(new string[] { targetVariable }), rows); var factorVariableValues = dataset.GetFactorVariableValues(factorVariableNames, rows); var factorMatrix = dataset.ToArray(factorVariableValues, rows); inputMatrix = factorMatrix.HorzCat(inputMatrix); if (inputMatrix.Cast <double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) { throw new NotSupportedException("Multinomial logit classification does not support NaN or infinity values in the input dataset."); } alglib.logitmodel lm = new alglib.logitmodel(); alglib.mnlreport rep = new alglib.mnlreport(); int nRows = inputMatrix.GetLength(0); int nFeatures = inputMatrix.GetLength(1) - 1; double[] classValues = dataset.GetDoubleValues(targetVariable).Distinct().OrderBy(x => x).ToArray(); int nClasses = classValues.Count(); // map original class values to values [0..nClasses-1] Dictionary <double, double> classIndices = new Dictionary <double, double>(); for (int i = 0; i < nClasses; i++) { classIndices[classValues[i]] = i; } for (int row = 0; row < nRows; row++) { inputMatrix[row, nFeatures] = classIndices[inputMatrix[row, nFeatures]]; } int info; alglib.mnltrainh(inputMatrix, nRows, nFeatures, nClasses, out info, out lm, out rep); if (info != 1) { throw new ArgumentException("Error in calculation of logit classification solution"); } rmsError = alglib.mnlrmserror(lm, inputMatrix, nRows); relClassError = alglib.mnlrelclserror(lm, inputMatrix, nRows); MultinomialLogitClassificationSolution solution = new MultinomialLogitClassificationSolution(new MultinomialLogitModel(lm, targetVariable, doubleVariableNames, factorVariableValues, classValues), (IClassificationProblemData)problemData.Clone()); return(solution); }
private MultinomialLogitClassificationSolution(MultinomialLogitClassificationSolution original, Cloner cloner) : base(original, cloner) { }
private MultinomialLogitClassificationSolution(MultinomialLogitClassificationSolution original, Cloner cloner) : base(original, cloner) { }