Beispiel #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);
        }
Beispiel #2
0
        private async Task <bool> AddNewClassifications(NeuralNetwork2 nn2, List <List <double> > testData,
                                                        double trainAccuracy, double mse, int rowCount, double hiLow, int ciPercent)
        {
            bool bHasNewClassifs = false;

            if (ciPercent > 5)
            {
                hiLow = CalculatorHelpers.GetConfidenceIntervalFromMSE(
                    ciPercent, rowCount, mse);
            }
            int           iRowLength = 0;
            string        sResult    = string.Empty;
            List <double> predictors = new List <double>();
            double        dbInput    = 0;
            double        dbTarget   = 0;

            //test data stores cols in testdata[0] first index
            for (int r = 0; r < DataResults.Count - 1; r++)
            {
                if (r == 0)
                {
                    iRowLength = DataResults[r + 1].Count;
                }
                predictors = new List <double>();
                for (int c = 0; c < testData.Count; c++)
                {
                    //prepare mathresults
                    dbInput = testData[c][r];
                    DataResults[r + 1][c] = dbInput.ToString("F4");
                    if (c > 0)
                    {
                        predictors.Add(dbInput);
                    }
                }
                //predict
                double[] forecast        = nn2.ComputeOutputs(predictors.ToArray());
                double   dbMostLikelyQTM = forecast[0];
                if (r == 0)
                {
                    DataResults[r][iRowLength - 5] = mse.ToString("F4");
                    DataResults[r][iRowLength - 4] = trainAccuracy.ToString("F4");
                }
                dbTarget = testData[0][r];
                double dbError = Math.Abs(dbTarget - dbMostLikelyQTM);
                this.DataResults[r + 1][iRowLength - 5] = dbError.ToString("F4");
                if (dbError < hiLow)
                {
                    DataResults[r + 1][iRowLength - 4] = "true";
                }
                else
                {
                    DataResults[r + 1][iRowLength - 4] = "false";
                }
                DataResults[r + 1][iRowLength - 3] = dbMostLikelyQTM.ToString("F4");
                if (r == DataResults.Count - 2)
                {
                    this.IndicatorQT.QTM = dbMostLikelyQTM;
                    this.IndicatorQT.QTL = dbMostLikelyQTM - hiLow;
                    this.IndicatorQT.QTU = dbMostLikelyQTM + hiLow;
                }
                //low estimation
                DataResults[r + 1][iRowLength - 2] = (dbMostLikelyQTM - hiLow).ToString("F4");
                //high estimation
                DataResults[r + 1][iRowLength - 1] = (dbMostLikelyQTM + hiLow).ToString("F4");
            }
            return(bHasNewClassifs);
        }