Ejemplo n.º 1
0
        /// <summary>
        /// Converts the dataframe to time serires <see cref="XYFrame"/> dataframe.
        /// </summary>
        /// <param name="lookback">The lookback count.</param>
        /// <param name="useColumn">The column number to use for creating time series data.</param>
        /// <returns></returns>
        public XYFrame ConvertTimeSeries(int lookback = 1, int useColumn = 0)
        {
            XYFrame      result = new XYFrame();
            List <float> data   = Data.Select(x => (x[useColumn])).ToList();

            for (int i = 0; i < data.Count; i++)
            {
                if (i + lookback > data.Count - 1)
                {
                    continue;
                }

                List <float> values  = new List <float>();
                int          counter = i;
                while (counter < i + lookback)
                {
                    values.Add(data[counter]);
                    counter++;
                }

                result.XFrame.Add(values);
                result.YFrame.Add(new List <float>()
                {
                    data[i + lookback]
                });
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the next batch of data for training.
        /// </summary>
        /// <param name="current">The current batch number.</param>
        /// <param name="batchSize">Size of the batch.</param>
        /// <returns></returns>
        public bool ToBatch(uint current, uint batchSize)
        {
            bool next      = true;
            int  totalRows = XFrame.Data.Count;
            uint skipRows  = (current - 1) * batchSize;

            if (skipRows < totalRows)
            {
                CurrentBatch = new XYFrame
                {
                    XFrame =
                    {
                        Data    = XFrame.Data.Skip((int)skipRows).Take((int)batchSize).ToList(),
                        Columns = XFrame.Columns
                    },
                    YFrame =
                    {
                        Data    = YFrame.Data.Skip((int)skipRows).Take((int)batchSize).ToList(),
                        Columns = YFrame.Columns
                    }
                };
            }
            else
            {
                next         = false;
                CurrentBatch = null;
            }

            return(next);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Splits the dataset to X and Y set.
        /// </summary>
        /// <param name="yCol">The y columns part of this split.</param>
        /// <param name="xCols">The x columns part of this split.</param>
        /// <returns>XY frame set.</returns>
        public XYFrame SplitXY(string yCol, params string[] xCols)
        {
            XYFrame result = new XYFrame();

            result.XFrame.Frame = Frame.ToTable(false, xCols).AsDataView();
            result.YFrame.Frame = Frame.ToTable(false, yCol).AsDataView();

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Splits the dataset to X and Y set.
        /// </summary>
        /// <param name="yCol">The y columns number.</param>
        /// <param name="xCols">The x columns numbers.</param>
        /// <returns>XY frame set.</returns>
        public XYFrame SplitXY(int yCol, int[] xCols)
        {
            XYFrame result = new XYFrame();

            result.XFrame = new DataFrame();
            result.YFrame = new DataFrame();
            List <float> xFrameRow = new List <float>();
            List <float> yFrameRow = new List <float>();

            if (xCols.Length != 2)
            {
                throw new Exception("xCols length must be 2 with column range. Eg. [1, 10]");
            }

            if (xCols[0] > xCols[1])
            {
                throw new Exception("xCols range must have first value lower than second value.");
            }

            Data.ForEach((record) =>
            {
                xFrameRow = new List <float>();
                yFrameRow = new List <float>();
                for (int i = xCols[0]; i <= xCols[1]; i++)
                {
                    xFrameRow.Add(record[i]);
                }

                yFrameRow.Add(record[yCol]);

                result.XFrame.Add(xFrameRow);
                result.YFrame.Add(yFrameRow);
            });

            if (result.XFrame.Columns.Count == 0)
            {
                for (int i = xCols[0]; i <= xCols[1]; i++)
                {
                    result.XFrame.Columns.Add(Columns[i]);
                }
            }

            if (result.YFrame.Columns.Count == 0)
            {
                result.YFrame.Columns.Add(Columns[yCol]);
            }

            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Splits the dataset to X and Y set.
        /// </summary>
        /// <param name="yCol">The y columns number.</param>
        /// <param name="xCols">The x columns numbers.</param>
        /// <returns>XY frame set.</returns>
        public XYFrame SplitXY(int yCol, int[] xCols)
        {
            XYFrame       result   = new XYFrame();
            DataTable     dt       = Frame.ToTable();
            List <string> xColumns = new List <string>();
            string        yColumn  = dt.Columns[yCol].ColumnName;

            for (int i = xCols[0]; i < xCols[1]; i++)
            {
                xColumns.Add(dt.Columns[i].ColumnName);
            }


            result.XFrame.Frame = Frame.ToTable(false, xColumns.ToArray()).AsDataView();
            result.YFrame.Frame = Frame.ToTable(false, yColumn).AsDataView();

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Loads the stream.
        /// </summary>
        /// <param name="filepath">The stream filepath to load the dataframe from.</param>
        /// <returns></returns>
        public static XYFrame LoadStream(string filepath)
        {
            if (!File.Exists(filepath))
            {
                return(null);
            }

            IFormatter formatter = new BinaryFormatter();
            XYFrame    frame     = null;

            using (Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var gZipStream = new GZipStream(stream, CompressionMode.Decompress))
                {
                    frame = (XYFrame)formatter.Deserialize(gZipStream);
                }
            }

            return(frame);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the next batch of data for training.
        /// </summary>
        /// <param name="current">The current batch number.</param>
        /// <param name="batchSize">Size of the batch.</param>
        /// <returns></returns>
        public bool NextBatch(int current, int batchSize)
        {
            bool      next      = true;
            DataTable dt_x      = XFrame.Frame.ToTable();
            DataTable dt_y      = YFrame.Frame.ToTable();
            int       totalRows = dt_x.Rows.Count;
            int       skipRows  = (current - 1) * batchSize;

            if (skipRows < totalRows)
            {
                CurrentBatch = new XYFrame();
                CurrentBatch.XFrame.Frame = dt_x.Select().Skip(skipRows).Take(batchSize).CopyToDataTable().AsDataView();
                CurrentBatch.YFrame.Frame = dt_y.Select().Skip(skipRows).Take(batchSize).CopyToDataTable().AsDataView();
            }
            else
            {
                next         = false;
                CurrentBatch = null;
            }

            return(next);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Splits the dataset to X and Y set.
        /// </summary>
        /// <param name="yCol">The y columns part of this split.</param>
        /// <param name="xCols">The x columns part of this split.</param>
        /// <returns>XY frame set.</returns>
        public XYFrame SplitXY(string yCol, params string[] xCols)
        {
            XYFrame result = new XYFrame();

            result.XFrame = new DataFrame();
            result.YFrame = new DataFrame();
            List <float> xFrameRow = new List <float>();
            List <float> yFrameRow = new List <float>();

            Data.ForEach((record) =>
            {
                xFrameRow.Clear();
                foreach (var col in Columns)
                {
                    if (xCols.Contains(col))
                    {
                        xFrameRow.Add(record[Columns.IndexOf(col)]);
                    }
                }

                yFrameRow.Add(record[Columns.IndexOf(yCol)]);

                result.XFrame.Add(xFrameRow);
                result.YFrame.Add(yFrameRow);
            });

            foreach (var col in Columns)
            {
                if (xCols.Contains(col))
                {
                    result.XFrame.Columns.Add(col);
                }
            }

            result.YFrame.Columns.Add(yCol);

            return(result);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets the next batch of data for training.
        /// </summary>
        /// <param name="current">The current batch number.</param>
        /// <param name="batchSize">Size of the batch.</param>
        /// <returns></returns>
        public bool NextBatch(int current, int batchSize)
        {
            bool next      = true;
            int  totalRows = XFrame.Data.Count;
            int  skipRows  = (current - 1) * batchSize;

            if (skipRows < totalRows)
            {
                CurrentBatch                = new XYFrame();
                CurrentBatch.XFrame.Data    = XFrame.Data.Skip(skipRows).Take(batchSize).ToList();
                CurrentBatch.XFrame.Columns = XFrame.Columns;

                CurrentBatch.YFrame.Data    = YFrame.Data.Skip(skipRows).Take(batchSize).ToList();
                CurrentBatch.YFrame.Columns = YFrame.Columns;
            }
            else
            {
                next         = false;
                CurrentBatch = null;
            }

            return(next);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TrainTestFrame"/> class.
 /// </summary>
 public TrainTestFrame()
 {
     Train = new XYFrame();
     Test  = new XYFrame();
 }