예제 #1
0
        public MatrisBase <object> Mean(MatrisBase <object> df,
                                        int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int nc = df.Col;
            int nr = df.Row;

            List <object> means = new List <object>();
            int           pop;

            for (int c = 0; c < nc; c++)
            {
                pop = nr - (numberOnly == 1 ? df.AmountOfNanInColumn(c) : 0);
                object res = ArrayMethods.ArraySum(df.ColList(c, 0), 0, nr, numberOnly) ?? float.NaN;
                if (pop == 0)
                {
                    means.Add(float.NaN);
                }
                else
                {
                    means.Add(float.IsNaN((float)res) ? res : (float)res / pop);
                }
            }

            return(df is Dataframe data
                ? new Dataframe(new List <List <object> >()
            {
                means
            },
                                data.Delimiter,
                                data.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                null,
                                data.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                means
            }));
        }
예제 #2
0
        public MatrisBase <object> Median(MatrisBase <object> df,
                                          int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int           pop;
            int           nr   = df.Row;
            int           nc   = df.Col;
            List <object> meds = new List <object>();

            if (df is Dataframe dataframe)
            {
                for (int j = 0; j < nc; j++)
                {
                    if (!dataframe.IsAllNumberColumn(j, 0))
                    {
                        if (numberOnly == 1)
                        {
                            List <object> col = new List <object>();
                            foreach (object o in dataframe.ColList(j, 0))
                            {
                                if (o is None ||
                                    o is null ||
                                    ((!float.TryParse(o.ToString(), out float res) ||
                                      float.IsNaN(res))))
                                {
                                    continue;
                                }
                                col.Add(o);
                            }

                            if (col.Count == 0)
                            {
                                meds.Add(float.NaN);
                                continue;
                            }

                            pop = nr - df.AmountOfNanInColumn(j);
                            col.Sort();
                            meds.Add((pop % 2 == 1)
                                ? (float)col[((pop + 1) / 2) - 1]
                                : ((float)col[(pop / 2) - 1] + (float)col[(int)Math.Round((decimal)(pop + 1) / 2, 0) - 1]) / 2);
                        }
                        else
                        {
                            meds.Add(float.NaN);
                        }
                    }
                    else
                    {
                        List <object> col = dataframe.ColList(j, 0);
                        col.Sort();
                        meds.Add((nr % 2 == 1)
                            ? (float)col[((nr + 1) / 2) - 1]
                            : ((float)col[(nr / 2) - 1] + (float)col[(int)Math.Round((decimal)(nr + 1) / 2, 0) - 1]) / 2);
                    }
                }

                return(new Dataframe(new List <List <object> >()
                {
                    meds
                },
                                     dataframe.Delimiter,
                                     dataframe.NewLine,
                                     null,
                                     Dataframe.GetCopyOfLabels(dataframe.GetColLabels()),
                                     null,
                                     dataframe.GetColSettings().Copy()));
            }
            else
            {
                for (int j = 0; j < nc; j++)
                {
                    if (numberOnly == 1)
                    {
                        List <object> col = new List <object>();
                        foreach (object o in df.ColList(j, 0))
                        {
                            if (o is None ||
                                o is null ||
                                ((!float.TryParse(o.ToString(), out float res) ||
                                  float.IsNaN(res))))
                            {
                                continue;
                            }
                            col.Add(o);
                        }

                        if (col.Count == 0)
                        {
                            meds.Add(float.NaN);
                            continue;
                        }

                        pop = nr - df.AmountOfNanInColumn(j);
                        col.Sort();
                        meds.Add((pop % 2 == 1)
                            ? (float)col[((pop + 1) / 2) - 1]
                            : ((float)col[(pop / 2) - 1] + (float)col[(int)Math.Round((decimal)(pop + 1) / 2, 0) - 1]) / 2);
                    }
                    else
                    {
                        List <object> col = df.ColList(j, 0);
                        col.Sort();
                        meds.Add((nr % 2 == 1)
                            ? (float)col[((nr + 1) / 2) - 1]
                            : (float)col[(nr / 2) - 1] + (float)col[(int)Math.Round((decimal)(nr + 1) / 2, 0) - 1]);
                    }
                }

                return(new MatrisBase <object>(new List <List <object> >()
                {
                    meds
                }));
            }
        }
예제 #3
0
        public MatrisBase <object> SDev(MatrisBase <object> df,
                                        int usePopulation = 0,
                                        int numberOnly    = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            if (usePopulation != 0 && usePopulation != 1)
            {
                throw new Exception(CompilerMessage.ARG_INVALID_VALUE("usePopulation", "Örneklem için 0, popülasyon için 1 olmalı!"));
            }

            int nr = df.Row;
            int nc = df.Col;

            if (nr == 1 && usePopulation == 1)
            {
                usePopulation = 0;
            }

            List <object>         means = Mean(df).RowList(1);
            List <object>         sdevs = new List <object>();
            List <List <object> > vals  = df.GetValues();
            int pop;

            for (int j = 0; j < nc; j++)
            {
                float sdev = 0;
                float mean = (float)means[j];
                if (float.IsNaN(mean))
                {
                    if (numberOnly == 1)
                    {
                        sdevs.Add(0);
                    }
                    else
                    {
                        sdevs.Add(float.NaN);
                    }
                    continue;
                }
                for (int i = 0; i < nr; i++)
                {
                    if (float.TryParse(vals[i][j].ToString(), out float res))
                    {
                        if (numberOnly == 1 && float.IsNaN(res))
                        {
                            continue;
                        }
                        sdev += (float)Math.Pow(res - mean, 2);
                    }
                    else
                    {
                        if (numberOnly == 1)
                        {
                            continue;
                        }
                        else
                        {
                            sdev = float.NaN;
                            break;
                        }
                    }
                }
                pop = nr - usePopulation - (numberOnly == 1 ? df.AmountOfNanInColumn(j) : 0);
                if (pop == 0)
                {
                    sdevs.Add(0);
                }
                else
                {
                    sdevs.Add((float)Math.Pow(sdev * (1.0 / pop), 0.5));
                }
            }

            return(df is Dataframe data
                ? new Dataframe(new List <List <object> >()
            {
                sdevs
            },
                                data.Delimiter,
                                data.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                null,
                                data.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                sdevs
            }));
        }