Ejemplo n.º 1
0
        private async Task <StringBuilder> Predict(List <List <string> > trainData,
                                                   List <List <string> > rowNames, List <List <string> > testData)
        {
            StringBuilder sb = null;

            try
            {
                //ml algo rule: iterations can also set rowcount and -1 for mlinstructs removed
                int iRowCount = (Shared.GetRowCount(_iterations, trainData.Count) - 1);
                //columns of data used and returned in DataResults
                _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);
                // error allowance
                double dbPlusorMinus
                    = CalculatorHelpers.ConvertStringToDouble(actualMLInstructs[0]);
                //converts rows to columns with normalized data
                List <List <double> > trainDB = Shared.GetNormalizedDData(trainData,
                                                                          this.IndicatorQT, _colNames, _depColNames, normTypes, "F2");
                List <List <double> > testDB = Shared.GetNormalizedDData(testData,
                                                                         this.IndicatorQT, _colNames, _depColNames, normTypes, "F2");
                //make a new list with same matrix, to be replaced with results
                int iColCount = testDB.Count;
                if (_subalgorithm == MATHML_SUBTYPES.subalgorithm_03.ToString().ToString())
                {
                    //subalgo02 needs qtm and percent probability of accuracy, qtm, low ci, high ci
                    iColCount = testDB.Count + 5;
                    //normtypes need full columns before insertion
                    normTypes = Shared.FixNormTypes(normTypes, iColCount);
                }
                //row count comes from original testdata to account for the instructions row
                DataResults    = CalculatorHelpers.GetList(testData.Count, iColCount);
                DataResults[0] = normTypes;
                //dep var output count
                int numOutput = 1;
                //less col[0]
                int numInput  = trainDB.Count - 1;
                int numHidden = 12;
                //can truncate the data to iRowCount
                double[][] trainInputs = Shared.MakeInputDData(trainDB, iRowCount, this.IndicatorQT,
                                                               numInput);
                //build a neural network
                NeuralNetwork2 nn2       = new NeuralNetwork2(numInput, numHidden, numOutput);
                int            maxEpochs = iRowCount;
                double         learnRate = 0.001;
                //train nn2
                double[] wts = nn2.Train(trainInputs, maxEpochs, learnRate, sb);
                //mean squared error
                double trainErr = nn2.Error(trainInputs);
                //final model accuracy
                double trainAcc = nn2.Accuracy(trainInputs, dbPlusorMinus);
                //add classified test data to DataResults
                bool bHasNewClassifs = await AddNewClassifications(nn2, testDB,
                                                                   trainAcc, trainErr, iRowCount, dbPlusorMinus, _ciLevel);
            }
            catch (Exception ex)
            {
                IndicatorQT.ErrorMessage = ex.Message;
            }
            return(sb);
        }