/// <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); }
/// <summary> /// Trains the model for a fixed number of epochs. /// </summary> /// <param name="train">The training dataset.</param> /// <param name="epoches">The no. of trainin epoches.</param> /// <param name="batchSize">Size of the batch for training.</param> /// <param name="validation">The validation dataset.</param> public void Train(XYFrame train, int epoches, int batchSize, XYFrame validation = null) { OnTrainingStart(); trainPredict = new DataFrameTrainPredict(modelOut, lossFunc, lossName, metricFunc, metricName, learners, featureVariable, labelVariable); TrainingResult = trainPredict.Train(train, validation, epoches, batchSize, OnEpochStart, OnEpochEnd, OnBatchStart, OnBatchEnd); OnTrainingEnd(TrainingResult); }
/// <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; }
/// <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); }
/// <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; }
/// <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); }
/// <summary> /// Initializes a new instance of the <see cref="TrainTestFrame"/> class. /// </summary> public TrainTestFrame() { Train = new XYFrame(); Test = new XYFrame(); }