Ejemplo n.º 1
0
        public MatrisBase <object> Mul(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> muls = new List <object>();

            for (int c = 0; c < nc; c++)
            {
                muls.Add(ArrayMethods.ArrayMul(df.ColList(c, 0), 0, nr, numberOnly) ?? float.NaN);
            }

            return(df is Dataframe data
                ? new Dataframe(new List <List <object> >()
            {
                muls
            },
                                data.Delimiter,
                                data.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(data.GetColLabels()),
                                null,
                                data.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                muls
            }));
        }
Ejemplo n.º 2
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
            }));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the matrix multiplication of <paramref name="A"/> and <paramref name="B"/> in the given order
        /// </summary>
        /// <param name="A">Matrix on the left</param>
        /// <param name="B">Matrix on the right</param>
        /// <returns>Resulting matrix from the matrix multiplication of <paramref name="A"/> and <paramref name="B"/></returns>
        public static MatrisBase <T> MatrisMul <T>(MatrisBase <T> A, MatrisBase <T> B)
        {
            if (A.Col != B.Row)
            {
                throw new Exception(CompilerMessage.MAT_MUL_BAD_SIZE);
            }

            List <List <T> > result = new List <List <T> >();

            for (int i = 0; i < A.Row; i++)
            {
                result.Add(new List <T>());
                for (int j = 0; j < B.Col; j++)
                {
                    result[i].Add(DotProduct(A.GetValues()[i], B.ColList(j, 0)));
                }
            }

            return(new MatrisBase <T>(result));
        }
Ejemplo n.º 4
0
        public MatrisBase <object> Transpose(MatrisBase <object> A)
        {
            if (!A.IsValid())
            {
                throw new Exception(CompilerMessage.MAT_INVALID_SIZE);
            }

            List <List <object> > result = new List <List <object> >();

            for (int j = 0; j < A.Col; j++)
            {
                result.Add(A.ColList(j, 0));
            }

            return(A is Dataframe df
                ? new Dataframe(result,
                                df.Delimiter,
                                df.NewLine,
                                Dataframe.GetCopyOfLabels(df.GetColLabels()),
                                Dataframe.GetCopyOfLabels(df.GetRowLabels()),
                                df.GetRowSettings().Copy(),
                                df.GetColSettings().Copy())
                : new MatrisBase <object>(result));
        }
Ejemplo n.º 5
0
        public MatrisBase <object> Mode(MatrisBase <object> df,
                                        int numberOnly = 1)
        {
            if (!df.IsValid())
            {
                throw new Exception(CompilerMessage.DF_INVALID_SIZE);
            }

            int           nc    = df.Col;
            List <object> modes = new List <object>();

            for (int j = 0; j < nc; j++)
            {
                List <object>            col     = df.ColList(j, 0);
                Dictionary <object, int> tracker = new Dictionary <object, int>();
                foreach (object val in col)
                {
                    if (tracker.ContainsKey(val))
                    {
                        tracker[val]++;
                    }
                    else
                    {
                        tracker.Add(val, 1);
                    }
                }

                object mode        = null;
                object placeholder = new None();
                placeholder = (df is Dataframe ? placeholder : float.NaN);

                int currentmax = 0;
                foreach (KeyValuePair <object, int> pair in tracker)
                {
                    if (numberOnly == 1 && (pair.Key is None ||
                                            pair.Key is null ||
                                            ((!float.TryParse(pair.Value.ToString(), out float res) ||
                                              float.IsNaN(res)))))
                    {
                        continue;
                    }

                    if (pair.Value > currentmax)
                    {
                        currentmax = pair.Value;
                        mode       = pair.Key;
                    }
                }
                modes.Add(mode ?? placeholder);
            }

            return(df is Dataframe dataframe
                ? new Dataframe(new List <List <object> >()
            {
                modes
            },
                                dataframe.Delimiter,
                                dataframe.NewLine,
                                null,
                                Dataframe.GetCopyOfLabels(dataframe.GetColLabels()),
                                null,
                                dataframe.GetColSettings().Copy())
                : new MatrisBase <object>(new List <List <object> >()
            {
                modes
            }));
        }
Ejemplo n.º 6
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
                }));
            }
        }