Esempio n. 1
0
        public static string NumericTableToCSV(NumericTable numericTable)
        {
            int totalIndependentsRows    = numericTable.Dependants.Length;
            int totalIndependentsColumns = numericTable.IndependentHeaders.Length;

            string[] rows = new string[totalIndependentsRows + 1];
            rows[0] = string.Join(",", numericTable.IndependentHeaders) + "," + numericTable.DependantHeader;
            Parallel.For(0, totalIndependentsRows, independentRowIndex =>
            {
                double[] rowDoubles      = GetRow(numericTable.Independents, independentRowIndex);
                List <string> rowStrings = new List <string>();
                foreach (double rowDouble in rowDoubles)
                {
                    string rowString = rowDouble.ToString();
                    if (rowString == "NaN")
                    {
                        rowString = "0";
                    }
                    rowStrings.Add(rowString);
                }
                rowStrings.Add(numericTable.Dependants[independentRowIndex].ToString());
                string row = string.Join(",", rowStrings);
                rows[independentRowIndex + 1] = row;
            });
            return(string.Join(Environment.NewLine, rows));
        }
Esempio n. 2
0
        public NumericTable ToNumericTableUseProcessDefinition(string table, string columnDelimiter, int dependantColumnIndex)
        {
            string[,] rawTable = ProcessData.ParseCSV(table, columnDelimiter);
            StringTable stringTable = ProcessData.RawTableToStringTable(rawTable, dependantColumnIndex);

            stringTable = ProcessData.RemoveUnwantedColumns(stringTable, IgnoreColumnHeaders.ToArray());
            ProcessData.ConvertCategoriesToIntegers(stringTable, ConvertToIntegersColumnHeaders.ToArray());
            stringTable = ProcessData.ExpandIntegerColumnsUseProcessDefinition(stringTable, ExpandColumnHeaders.ToArray(), this);
            NumericTable numericTable = ProcessData.ToNumericTable(stringTable);

            stringTable = null;
            ProcessData.Normalize(numericTable, null);
            return(numericTable);
        }
Esempio n. 3
0
        public static NumericTable ToNumericTable(StringTable stringTable)
        {
            NumericTable numericTable = new NumericTable();

            numericTable.IndependentHeaders = stringTable.IndependentHeaders;
            numericTable.DependantHeader    = stringTable.DependantHeader;
            int totalRows    = stringTable.Independents.GetUpperBound(0) + 1;
            int totalColumns = stringTable.Independents.GetUpperBound(1) + 1;

            numericTable.Independents = new double[totalRows, totalColumns];
            Parallel.For(0, totalRows, rowIndex =>
            {
                for (int columnIndex = 0; columnIndex < totalColumns; columnIndex++)
                {
                    double result  = 0;
                    bool succeeded = double.TryParse(stringTable.Independents[rowIndex, columnIndex], out result);
                    if (succeeded & double.IsNaN(result) == false)
                    {
                        numericTable.Independents[rowIndex, columnIndex] = result;
                    }
                    else
                    {
                        numericTable.Independents[rowIndex, columnIndex] = 0;
                    }
                }
            });
            numericTable.Dependants = new double[totalRows];
            if (stringTable.Dependants != null)
            {
                for (int rowIndex = 0; rowIndex < totalRows; rowIndex++)
                {
                    double result    = 0;
                    bool   succeeded = double.TryParse(stringTable.Dependants[rowIndex], out result);
                    if (succeeded & double.IsNaN(result) == false)
                    {
                        numericTable.Dependants[rowIndex] = result;
                    }
                    else
                    {
                        numericTable.Dependants[rowIndex] = 0;
                    }
                }
            }
            return(numericTable);
        }
Esempio n. 4
0
        public static void Normalize(NumericTable numericTable, ProcessDefinition processDefinition)
        {
            int totalRows    = numericTable.Independents.GetUpperBound(0) + 1;
            int totalColumns = numericTable.Independents.GetUpperBound(1) + 1;

            //Parallel.For(0, totalColumns, columnIndex =>
            //{
            //    double[] column = GetColumn(numericTable.Independents, columnIndex);
            //    double min = column.Min();
            //    double max = column.Max();
            //    double range = max - min;
            //    for (int rowIndex = 0; rowIndex < totalRows; rowIndex++)
            //    {
            //        if (range != 0)
            //        {
            //            column[rowIndex] = (column[rowIndex] - min) / range;
            //        }
            //        else
            //        {
            //            column[rowIndex] = 0;
            //        }
            //    }
            //    PutColumn(numericTable.Independents, column, columnIndex);
            //});
            for (int columnIndex = 0; columnIndex < totalColumns; columnIndex++)
            {
                double[] column = GetColumn(numericTable.Independents, columnIndex);
                double   min    = column.Min();
                double   max    = column.Max();
                double   range  = max - min;
                for (int rowIndex = 0; rowIndex < totalRows; rowIndex++)
                {
                    if (range != 0)
                    {
                        column[rowIndex] = (column[rowIndex] - min) / range;
                    }
                    else
                    {
                        column[rowIndex] = 0;
                    }
                }
                PutColumn(numericTable.Independents, column, columnIndex);
            }
            if (processDefinition != null)
            {
                double min   = numericTable.Dependants.Min();
                double max   = numericTable.Dependants.Max();
                double range = max - min;
                for (int rowIndex = 0; rowIndex < totalRows; rowIndex++)
                {
                    if (range != 0)
                    {
                        numericTable.Dependants[rowIndex] = (numericTable.Dependants[rowIndex] - min) / range;
                    }
                    else
                    {
                        numericTable.Dependants[rowIndex] = 0;
                    }
                }
                processDefinition.Scale  = range;
                processDefinition.Offset = min;
            }
        }