Beispiel #1
0
        void Build_Corr_Matrix(DataTable tempdt)
        {
            for (int i = 0; i < tempdt.Columns.Count; i++)
            {
                string type = tempdt.Columns[i].DataType.ToString();
                if ((type.Contains(typeof(string).ToString()) || type.Contains(typeof(char).ToString())))
                {
                    tempdt.Columns.Remove(tempdt.Columns[i]);
                }
                else
                {
                    foreach (DataRow dr in tempdt.Rows)
                    {
                        if (dr[i].GetType().ToString().Contains("DBNull"))
                        {
                            dr[i] = 0.0;
                        }
                    }
                }
            }

            double[,] tableMatrix = tempdt.ToMatrix();
            double[,] correlMatrix = Accord.Statistics.Tools.Correlation(tableMatrix);
            string[] names = new string[tempdt.Columns.Count];
            int h = 0;
            TableRow headRow = new TableRow();

            TableCell cornerCell = new TableCell();
            cornerCell.Text = "-";
            cornerCell.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
            cornerCell.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
            headRow.Cells.Add(cornerCell);

            foreach (DataColumn col in tempdt.Columns)
            {
                string type = col.DataType.ToString();
                if (!(type.Contains(typeof(string).ToString()) || type.Contains(typeof(char).ToString())))
                {
                    names[h] = col.ColumnName.ToString();

                    TableCell cell = new TableCell();
                    cell.Text = names[h];
                    cell.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
                    cell.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
                    headRow.Cells.Add(cell);

                    h++;
                }
            }

            CorrTable.Rows.Add(headRow);

            for (int i = 0; i < correlMatrix.Rows(); i++)
            {
                double[] row = correlMatrix.GetRow(i);
                int length = row.Length;

                TableRow tR = new TableRow();

                TableCell rowName = new TableCell();
                rowName.Text = names[i];
                rowName.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
                rowName.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
                tR.Cells.Add(rowName);

                for (int j = 0; j < length; j++)
                {
                    TableCell cell = new TableCell();

                    if (j <= i)
                    {
                        cell.Text = row[j].ToString("0.###");
                    }
                    else
                    {
                        cell.Text = "-";
                    }

                    cell.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
                    cell.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);
                    tR.Cells.Add(cell);
                }
                CorrTable.Rows.Add(tR);
            }
        }
Beispiel #2
0
        public void ToMatrixTest2()
        {
            DataTable table = new DataTable("myData");
            table.Columns.Add("Double", typeof(double));
            table.Columns.Add("Integer", typeof(int));
            table.Columns.Add("Boolean", typeof(bool));

            table.Rows.Add(4.20, 42, true);
            table.Rows.Add(-3.14, -17, false);
            table.Rows.Add(21.00, 0, false);

            double[,] expected =
            {
                {  4.20,  42, 1 },
                { -3.14, -17, 0 },
                { 21.00,   0, 0 },
            };

            double[,] actual = table.ToMatrix();

            Assert.IsTrue(expected.IsEqual(actual));


            string[] expectedNames = { "Double", "Integer", "Boolean" };
            string[] actualNames;

            table.ToMatrix(out actualNames);

            Assert.IsTrue(expectedNames.IsEqual(actualNames));
        }
        private ArrayDataView AnalysisData(DataTable dataTable)
        {
            DescriptiveAnalysis analysis;
            string[] columnNames;

            double[,] arrayData = dataTable.ToMatrix(out columnNames);

            // Analysis Data
            analysis = new DescriptiveAnalysis(arrayData, columnNames);
            analysis.Compute();

            double[,] analysisData = new double[0, arrayData.GetLength(1)];

            analysisData = analysisData.InsertRow<double>(analysis.Distinct.ToDouble(), analysisData.GetLength(0));
            analysisData = analysisData.InsertRow<double>(analysis.Means, analysisData.GetLength(0));
            analysisData = analysisData.InsertRow<double>(analysis.Medians, analysisData.GetLength(0));
            analysisData = analysisData.InsertRow<double>(analysis.Modes, analysisData.GetLength(0));
            analysisData = analysisData.InsertRow<double>(analysis.StandardDeviations, analysisData.GetLength(0));
            analysisData = analysisData.InsertRow<double>(analysis.Variances, analysisData.GetLength(0));
            analysisData = analysisData.InsertRow<double>(analysis.Sums, analysisData.GetLength(0));

            string[] rowNames = { "Distinct", "Means", "Medians", "Modes", "StandardDeviations", "Variances", "Sums" };

            return new ArrayDataView(analysisData, columnNames, rowNames);
        }