Example #1
0
        public DataLR getLastNRows(int nbRows)
        {
            DataLR part = new DataLR();

            if (DepVar.Length > 0)
            {
                int nbRowsTake = Math.Min(nbRows, DepVar.Length);
                int nbVars     = IndepVar[0].Length;
                int iStart     = DepVar.Length - nbRowsTake;

                part.IndepVar = MatrixCreate(nbRowsTake, nbVars);
                part.DepVar   = VectorCreate(nbRowsTake);

                int row     = 0;
                int rowLoad = 0;
                foreach (double[] vars in IndepVar)
                {
                    //out of section
                    if (row >= iStart)
                    {
                        for (int i = 0; i < vars.Length; i++)
                        {
                            part.IndepVar[rowLoad][i] = vars[i];
                        }
                        part.DepVar[rowLoad] = DepVar[row];
                        ++rowLoad;
                    }
                    ++row;
                }
            }

            return(part);
        }
Example #2
0
        //split in two sub samples
        public void split(int pcentStartExtract, int pcentEndExtract, out DataLR partOut, out DataLR partIn)
        {
            partOut = new DataLR();
            partIn  = new DataLR();

            if (DepVar.Length == 0)
            {
                return;
            }

            int nbLignes = DepVar.Length;
            int nbVars   = IndepVar[0].Length;

            int iStart    = (nbLignes * pcentStartExtract) / 100;
            int iEnd      = (nbLignes * pcentEndExtract) / 100;
            int nbRowsIn  = iEnd - iStart;
            int nbRowsOut = DepVar.Length - nbRowsIn;

            partIn.IndepVar = MatrixCreate(nbRowsIn, nbVars);
            partIn.DepVar   = new double[nbRowsIn];

            partOut.IndepVar = MatrixCreate(nbRowsOut, nbVars);
            partOut.DepVar   = new double[nbRowsOut];

            int row    = 0;
            int rowIn  = 0;
            int rowOut = 0;

            foreach (double[] vars in IndepVar)
            {
                //out of section
                if (row < iStart || row >= iEnd)
                {
                    for (int i = 0; i < vars.Length; i++)
                    {
                        partOut.IndepVar[rowOut][i] = vars[i];
                    }
                    partOut.DepVar[rowOut] = DepVar[row];
                    ++rowOut;
                }

                //in section
                if (row >= iStart && row < iEnd)
                {
                    for (int i = 0; i < vars.Length; i++)
                    {
                        partIn.IndepVar[rowIn][i] = vars[i];
                    }
                    partIn.DepVar[rowIn] = DepVar[row];
                    ++rowIn;
                }

                ++row;
            }

            //Console.WriteLine("Partion split: partIn=" + rowIn + " partOut:" + rowOut);
        }
Example #3
0
    public static double TestModel(ModelLR model, DataLR testData)
    {
        double acc = 0;

        try
        {
            acc = (double)PredictiveAccuracy(testData.IndepVar, testData.DepVar, model.Betas) / 100.0; // percent of data cases correctly predicted in the test data set.
        }
        catch (Exception ex)
        {
            Console.WriteLine("Fatal in TestModel: " + ex.Message);
        }

        return(acc);
    }
Example #4
0
    //Le fichier doit contenir pour chaque lignes les valeurs des indépendants suivie de la dépendante
    public static ModelLR ComputeModel(DataLR datas)
    {
        ModelLR model = new ModelLR();

        try
        {
            int    maxIterations = 25;
            double epsilon       = 0.01;                                                                     // stop if all new beta values change less than epsilon (algorithm has converged?)
            double jumpFactor    = 1000.0;                                                                   // stop if any new beta jumps too much (algorithm spinning out of control?)

            model.Betas = ComputeBestBeta(datas.IndepVar, datas.DepVar, maxIterations, epsilon, jumpFactor); // computing the beta parameters is synonymous with 'training'
        }
        catch (Exception ex)
        {
            Console.WriteLine("Fatal in ComputeBestBeta: " + ex.Message);
        }

        return(model);
    }
Example #5
0
        //shuffle
        public DataLR shuffle()
        {
            DataLR part = new DataLR();

            if (DepVar.Length == 0)
            {
                return(part);
            }

            int nbRows = DepVar.Length;
            int nbVars = IndepVar[0].Length;

            part.IndepVar = new double[nbRows][];
            part.DepVar   = VectorCreate(nbRows);

            Random r   = new Random();
            int    row = 0;

            foreach (double[] vars in IndepVar)
            {
                //On tire une ligne a remplir au hasard
                int rowRand = r.Next() % nbRows;
                //sens dans lequel on cherche une case vide
                int sens = r.NextDouble() > 0.5 ? -1 : 1;

                //On cherche une case vide
                int nextRow = -1;
                if (sens > 0)
                {
                    for (int i = 0; i < nbRows; i++)
                    {
                        int rowTest = (rowRand + i) % nbRows;
                        if (part.IndepVar[rowTest] == null)
                        {
                            nextRow = rowTest;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < nbRows; i++)
                    {
                        int rowTest = (rowRand - i);
                        if (rowTest < 0)
                        {
                            rowTest += nbRows;
                        }
                        if (part.IndepVar[rowTest] == null)
                        {
                            nextRow = rowTest;
                        }
                    }
                }

                if (nextRow < 0)
                {
                    throw new Exception("Pas trouvé de case vide, algo de shuffle marche pas");
                }

                part.IndepVar[nextRow] = new double[nbVars];

                for (int i = 0; i < vars.Length; i++)
                {
                    part.IndepVar[nextRow][i] = vars[i];
                }
                part.DepVar[nextRow] = DepVar[row];

                ++row;
            }

            return(part);
        }