Пример #1
0
        private async Task <StringBuilder> Classify(List <List <string> > trainData,
                                                    List <List <string> > rowNames, List <List <string> > testData)
        {
            StringBuilder sb = null;

            try
            {
                //columns of data used and returned in DataResults
                int iRowCount = (Shared.GetRowCount(_iterations, trainData.Count) - 1);
                _actualColNames = Shared.GetActualColNames(_colNames, _depColNames).ToArray();
                //ml instructions associated with actual colNames
                List <string> normTypes = Shared.GetNormTypes(trainData[0], _colNames, _depColNames);
                //instructions in both row names and datasets
                List <string> actualMLInstructs = Shared.GetAlgoInstructs(rowNames);
                actualMLInstructs.AddRange(normTypes);
                // prevent joint counts with 0
                bool withLaplacian
                    = actualMLInstructs[0].ToLower().Contains("true") ? true : false;
                //converts rows to columns with normalized data
                List <List <string> > trainDB = Shared.GetNormalizedSData(trainData,
                                                                          this.IndicatorQT, _colNames, _depColNames, normTypes, "F0");
                List <List <string> > testDB = Shared.GetNormalizedSData(testData,
                                                                         this.IndicatorQT, _colNames, _depColNames, normTypes, "F0");
                int iColCount = testDB.Count;
                if (_subalgorithm == MATHML_SUBTYPES.subalgorithm_01.ToString().ToString())
                {
                    //subalgo02 needs qtm and percent probability of accuracy, mse, qtm, low ci, high ci
                    iColCount = testDB.Count + 2;
                }
                //row count comes from original testdata to account for the instructions row
                DataResults    = CalculatorHelpers.GetList(testData.Count, iColCount);
                DataResults[0] = normTypes;
                // trainData columns define number of rows (depcolumns.Length + 1)
                string[][] attributeValues = new string[trainDB.Count][];
                //for each column of trainDB, fill in the unique attribute names (i.e. gender = 2 unique atts)
                for (int i = 0; i < trainDB.Count; i++)
                {
                    attributeValues[i] = Shared.GetAttributeGroups(i, trainDB, this.IndicatorQT);
                }
                int[][][] jointCounts     = MakeJointCounts(trainDB, attributeValues);
                int[]     dependentCounts = MakeDependentCounts(jointCounts, attributeValues[0].Length);
                //classify everything in test dataset and add result to new columns in test dataset
                List <string> predictors = new List <string>();
                int           d          = 0;
                int           iRowLength = DataResults[1].Count;
                string        sAttribute = string.Empty;
                for (int r = 0; r < DataResults.Count - 1; r++)
                {
                    predictors = new List <string>();
                    //cols have separate set of predictors
                    for (int j = 0; j < testDB.Count; j++)
                    {
                        //prepare mathresults
                        DataResults[r + 1][j] = testDB[j][r];
                        if (j > 0)
                        {
                            //going down the rows (j) in the column (r)
                            predictors.Add(testDB[j][r]);
                        }
                    }
                    d = await Classify(r + 1, attributeValues, predictors.ToArray(),
                                       jointCounts, dependentCounts, withLaplacian, attributeValues.Length - 1);

                    for (int l = 0; l < attributeValues[0].Length; l++)
                    {
                        if (d == l)
                        {
                            sAttribute = Shared.ConvertAttributeToLabel(attributeValues[0][l],
                                                                        this.IndicatorQT);
                            DataResults[r + 1][iRowLength - 2] = sAttribute;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IndicatorQT.ErrorMessage = ex.Message;
            }
            return(sb);
        }