Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            DataTable table = new Accord.IO.CsvReader("C:\\Users\\michael\\Downloads\\JulyToOct2015Test.csv", true).ToTable();

            // Convert the DataTable to input and output vectors
            double[][] inputs  = table.ToJagged <double>("BookToPrice", "DividendYield", "DebtToEquity", "MarketBeta", "SectorID");
            int[]      outputs = table.Columns["MonthlyReturn"].ToArray <int>();


            //SecurityID BookToPrice DividendYield EarningsYield   SalesGrowth AssetsToEquity  MarketCap MarketBeta  DebtToEquity    1YrVol  5YrVol  3YrVol ExposureToCurrencyGain  SectorID countryID

            DecisionTree tree = new DecisionTree(
                inputs: new List <DecisionVariable>
            {
                DecisionVariable.Continuous("BookToPrice"),
                DecisionVariable.Continuous("DividendYield"),
                DecisionVariable.Continuous("DebtToEquity"),
                DecisionVariable.Continuous("MarketBeta"),
                DecisionVariable.Discrete("SectorID", 11)
            },
                classes: 2);

            C45Learning teacher = new C45Learning(tree);

            teacher.Learn(inputs, outputs);
            int[] answers = tree.Decide(inputs);


            // Plot the results
            // ScatterplotBox.Show("Expected results", inputs, outputs);
            //ScatterplotBox.Show("Ans", inputs, answers)
            //    .Hold();
        }
        public void LoadData(String filePath)
        {
            if (!"".Equals(filePath) && filePath != null)
            {
                _data = null;

                try
                {
                    // open selected file
                    using (TextReader stream = new StreamReader(filePath))
                    using (CsvReader reader = new CsvReader(stream, true))
                    {
                        // data format:
                        // Date || X(SMA, EMA, Prices,...)
                        var table = reader.ToTable();
                        table.Columns.RemoveAt(0);
                        _data = table.ToMatrix(CultureInfo.InvariantCulture).GetColumn(0).Reverse().ToArray(); // get 'X' values

                        attributeName = table.Columns[0].ToString();
                    }

                }
                catch (Exception e)
                {
                    Debug.WriteLine("Failed reading the file");
                    Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                    return;
                }
            }
        }
Ejemplo n.º 3
0
        public void SettleCSVwords(string filePath)
        {
            System.Data.DataTable table = new Accord.IO.CsvReader(filePath, true).ToTable();

            string[] texts = table.Columns[1].ToArray <string>();

            string concatText = "";

            for (int i = 0; i < texts.Length; i++)
            {
                concatText += texts[i];
                concatText += " ";
            }

            concatText = concatText
                         .Replace(",", "")
                         .Replace(".", "")
                         .Replace(":", "")
                         .Replace(";", "")
                         .Replace("– ", "")
                         .Replace("- ", "")
                         .Replace("— ", "")
                         .Replace("-", "")
                         .Replace("«", "")
                         .Replace("»", "")
                         .Replace("\"", "")
                         .Replace("(", "")
                         .Replace(")", "")
                         .Replace("?", "")
                         .Replace("!", "")
                         .Replace("#", "")
                         .Replace("/", " ")
                         .ToLower();

            string[] words = concatText.Split(' ');

            for (int i = 0; i < words.Length; i++)
            {
                if (wordsDictionaryClass1.ContainsKey(words[i]))
                {
                    wordsDictionaryClass1[words[i]]++;
                }
                else
                {
                    wordsDictionaryClass1.Add(words[i], 1);
                }
            }
        }
        public void ZeroLambdaTest()
        {
            double[,] data = null;

            // open selected file
            using (TextReader stream = new StringReader(Properties.Resources.ZeroLambda))
            using (CsvReader reader = new CsvReader(stream, false))
            {
                data = reader.ToTable().ToMatrix();
            }

            // number of learning samples
            int samples = data.GetLength(0);

            var ranges = data.Range(dimension: 0);

            Assert.AreEqual(2, ranges.Length);

            var rangeX = ranges[0];
            var rangeY = ranges[1];

            // data transformation factor
            double yFactor = 1.7 / rangeY.Length;
            double yMin = rangeY.Min;
            double xFactor = 2.0 / rangeX.Length;
            double xMin = rangeX.Min;

            // prepare learning data
            double[][] input = new double[samples][];
            double[][] output = new double[samples][];

            for (int i = 0; i < samples; i++)
            {
                input[i] = new double[1];
                output[i] = new double[1];

                input[i][0] = (data[i, 0] - xMin) * xFactor - 1.0; // set input
                output[i][0] = (data[i, 1] - yMin) * yFactor - 0.85; // set output
            }

            // create multi-layer neural network
            ActivationNetwork network = new ActivationNetwork(
                new BipolarSigmoidFunction(5),
                1, 12, 1);

            // create teacher
            LevenbergMarquardtLearning teacher = new LevenbergMarquardtLearning(network, true);

            teacher.LearningRate = 1;

            // iterations
            int iteration = 1;
            int iterations = 2000;

            // solution array
            double[,] solution = new double[samples, 2];
            double[] networkInput = new double[1];

            bool needToStop = false;

            double learningError = 0;

            // loop
            while (!needToStop)
            {
                Assert.AreNotEqual(0, teacher.LearningRate);

                // run epoch of learning procedure
                double error = teacher.RunEpoch(input, output) / samples;

                // calculate solution
                for (int j = 0; j < samples; j++)
                {
                    networkInput[0] = (solution[j, 0] - xMin) * xFactor - 1.0;
                    solution[j, 1] = (network.Compute(networkInput)[0] + 0.85) / yFactor + yMin;
                }


                // calculate error
                learningError = 0.0;
                for (int j = 0; j < samples; j++)
                {
                    networkInput[0] = input[j][0];
                    learningError += Math.Abs(data[j, 1] - ((network.Compute(networkInput)[0] + 0.85) / yFactor + yMin));
                }

                // increase current iteration
                iteration++;

                // check if we need to stop
                if ((iterations != 0) && (iteration > iterations))
                    break;
            }

            Assert.IsTrue(learningError < 0.13);
        }
Ejemplo n.º 5
0
        // Load data
        private void loadDataButton_Click(object sender, System.EventArgs e)
        {
            // show file selection dialog
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                data = null;

                try
                {
                    // open selected file
                    using (TextReader stream = new StreamReader(openFileDialog.FileName))
                    using (CsvReader reader = new CsvReader(stream, false))
                    {
                        data = reader.ToTable().ToMatrix(CultureInfo.InvariantCulture);
                    }

                }
                catch (Exception)
                {
                    MessageBox.Show("Failed reading the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                DoubleRange[] ranges = data.Range(dimension: 0);

                xRange = ranges[0];
                yRange = ranges[1];

                // update list and chart
                UpdateDataListView();
                chart.RangeX = new Range((float)ranges[0].Min, (float)ranges[0].Max);
                chart.UpdateDataSeries("data", data);
                chart.UpdateDataSeries("solution", null);

                // enable "Start" button
                startButton.Enabled = true;
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 ///   Reads the specified file or stream into a table.
 /// </summary>
 /// 
 public DataTable Read(Stream stream)
 {
     var reader = new CsvReader(new StreamReader(stream), true);
     return reader.ToTable();
 }
Ejemplo n.º 7
0
        // Load data
        private void loadDataButton_Click(object sender, System.EventArgs e)
        {
            // show file selection dialog
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                data = null;

                try
                {
                    // open selected file
                    using (TextReader stream = new StreamReader(openFileDialog.FileName))
                    using (CsvReader reader = new CsvReader(stream, false))
                    {
                        data = reader.ToTable().ToMatrix(CultureInfo.InvariantCulture).GetColumn(0);
                    }

                }
                catch (Exception)
                {
                    MessageBox.Show("Failed reading the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                dataToShow = Matrix.Stack(Vector.Range(0, data.Length).ToDouble(), data).Transpose();

                // update list and chart
                UpdateDataListView();
                chart.RangeX = new Range(0, data.Length - 1);
                chart.UpdateDataSeries("data", dataToShow);
                chart.UpdateDataSeries("solution", null);

                // set delimiters
                UpdateDelimiters();

                // enable "Start" button
                startButton.Enabled = true;
            }
        }
        /// <summary>
        ///  Action for the File->Paste command.
        /// </summary>
        ///
        public void Paste_Executed(object target)
        {
            var text = Clipboard.GetText();

            var reader = new CsvReader(new StringReader(text), false, '\t');
            var lines = reader.ReadToEnd();

            lines.Reverse();

            int startIndex = Values.IndexOf(CurrentCell.Item as SampleViewModel);
            if (startIndex == -1)
                startIndex = Values.Count;

            foreach (var line in lines)
            {
                var sample = new SampleViewModel();

                double value;
                if (!Double.TryParse(line[0], out value))
                    continue;

                sample.Value = value;

                if (line.Length > 1)
                {
                    double weight = 1;
                    if (Double.TryParse(line[1], out weight))
                        sample.Weight = weight;
                }

                Values.Insert(startIndex, sample);
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     _reader  = null;
     _current = null;
 }
Ejemplo n.º 10
0
        public static DataTable Load(Stream stream, TableFormat format)
        {
            switch (format)
            {
                case TableFormat.SerializedXml:
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
                        return (DataTable)serializer.Deserialize(stream);
                    }

                case TableFormat.SerializedBin:
                    {
                        BinaryFormatter serializer = new BinaryFormatter();
                        return (DataTable)serializer.Deserialize(stream);
                    }

                case TableFormat.OctaveMatFile:
                    {
                        MatReader reader = new MatReader(stream);
                        return reader.Fields.First().Value.GetValue<double[,]>().ToTable();
                    }

                case TableFormat.OpenDocument:
                    {
                        ExcelReader reader = new ExcelReader(stream, true);
                        string ws = reader.GetWorksheetList().First();
                        return reader.GetWorksheet(ws);
                    }

                case TableFormat.OlderExcel:
                    {
                        ExcelReader reader = new ExcelReader(stream, false);
                        string ws = reader.GetWorksheetList().First();
                        return reader.GetWorksheet(ws);
                    }

                case TableFormat.Csv:
                    {
                        CsvReader reader = new CsvReader(stream, true);
                        return reader.ToTable();
                    }

                case TableFormat.Tsv:
                    {
                        CsvReader reader = new CsvReader(stream, true, '\t');
                        return reader.ToTable();
                    }

                case TableFormat.LibSVM:
                    {
                        SparseReader reader = new SparseReader(stream);
                        return reader.ToTable();
                    }

                case TableFormat.Idx:
                    {
                        IdxReader reader = new IdxReader(stream);
                        return reader.ReadToEndAsVectors().ToTable();
                    }

                case TableFormat.CSharp:
                    throw new NotSupportedException();
            }
        }