public void start(Analysis parent, HttpResponse response, System.Web.SessionState.HttpSessionState session) { ParameterStream stream = ParameterStream.getStream(session); String[] features = (String[])stream.get("selectedFeatures"); int PCs = (int)stream.get("numberOfPCs"); Registry.Registry registry = Registry.Registry.getRegistry(session); System.Data.DataSet ds = (System.Data.DataSet)registry.GetDataset((string)stream.get("dataSetName")); //retrieve dataset table (assume one for now) System.Data.DataTable dt = ds.Tables[0]; //raw data double[,] rawData = new double[dt.Rows.Count, features.Count()]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < features.Count(); j++) { rawData[i, j] = (double)dt.Rows[i].ItemArray.ElementAt(dt.Columns[features[j]].Ordinal); } } //Create matrix to hold data for PCA Matrix X = new Matrix(rawData); //Remove mean Vector columnVector; for (int i = 0; i < X.ColumnCount; i++) { columnVector = X.GetColumnVector(i); X.SetColumnVector(columnVector.Subtract(columnVector.Average()), i); } Matrix PCmatrix = new Matrix(X.ColumnCount, PCs, 0); Vector Weights = new Vector(PCs); System.Diagnostics.Stopwatch watch = new Stopwatch(); //Run algorithm and time it watch.Start(); SingularValueDecomposition svd = SVD(X); watch.Stop(); stream.set("algRunTime", watch.ElapsedMilliseconds); /* * response.Buffer = true; * response.Write(PCmatrix.ToString() + "\n"); * response.Write(Weights.ToString() + "\n"); * response.Flush(); */ Debug.WriteLine("Done with PCA"); stream.set("PCmatrix", svd.RightSingularVectors); stream.set("Weights", svd.SingularValues); parent.next(response, session); }
protected void FeatureList_Init(object sender, EventArgs e) { String dataSetParameterName = "dataSetName"; ParameterStream stream = ParameterStream.getStream(Session); if (stream.contains(dataSetParameterName)) { Registry.Registry appRegistry = Registry.Registry.getRegistry(Session); DataSet ds = appRegistry.GetDataset((String)stream.get(dataSetParameterName)); foreach (DataColumn dc in ds.Tables[0].Columns) { FeatureList.Items.Add(dc.ColumnName); } } }
protected void Page_Load(object sender, EventArgs e) { Matrix PCmatrix = (Matrix)stream.get("PCmatrix"); String[] features = (String[])stream.get("selectedFeatures"); System.Data.DataSet ds = (System.Data.DataSet)registry.GetDataset((string)stream.get("dataSetName")); //retrieve dataset table (assume one for now) System.Data.DataTable dt = ds.Tables[0]; //raw data double[,] rawData = new double[dt.Rows.Count, features.Count()]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < features.Count(); j++) { rawData[i, j] = (double)dt.Rows[i].ItemArray.ElementAt(dt.Columns[features[j]].Ordinal); } } //Create matrix to hold data for PCA Matrix X = new Matrix(rawData); //Remove mean Vector columnVector; for (int i = 0; i < X.ColumnCount; i++) { columnVector = X.GetColumnVector(i); X.SetColumnVector(columnVector.Subtract(columnVector.Average()), i); } //get first two PCs Matrix xy = new Matrix(PCmatrix.RowCount, 2); xy.SetColumnVector(PCmatrix.GetColumnVector(0), 0); xy.SetColumnVector(PCmatrix.GetColumnVector(1), 1); //project Matrix projected = X.Multiply(xy); DataPoint point; Projection.Series.Clear(); Projection.Legends.Clear(); //if a label column is selected String LabelColumnName = LabelColumn.Text; if (!LabelColumnName.Equals("")) { //get labels int labelColumnIndex = dt.Columns[LabelColumnName].Ordinal; List <String> labels = new List <String>(); String item; for (int i = 0; i < dt.Rows.Count; i++) { item = (String)dt.Rows[i].ItemArray.ElementAt(labelColumnIndex); if (!labels.Contains(item)) { labels.Add(item); } } Projection.Legends.Add(LabelColumnName); System.Drawing.Font font = Projection.Legends[LabelColumnName].Font = new System.Drawing.Font(Projection.Legends[LabelColumnName].Font.Name, 14); //Configure series foreach (String label in labels) { Projection.Series.Add(label); Projection.Series[label].LegendText = label; Projection.Series[label].IsXValueIndexed = false; Projection.Series[label].ChartType = SeriesChartType.Point; Projection.Series[label].MarkerSize = 8; } //Add points for (int i = 0; i < projected.RowCount; i++) { point = new DataPoint(projected[i, 0], projected[i, 1]); String label = dt.Rows[i].ItemArray[labelColumnIndex].ToString(); Projection.Series[label].Points.Add(point); } } else { //Single plot graph Projection.Series.Add("series1"); Projection.Series[0].IsXValueIndexed = false; Projection.Series[0].ChartType = SeriesChartType.Point; Projection.Series[0].MarkerSize = 8; for (int i = 0; i < projected.RowCount; i++) { point = new DataPoint(projected[i, 0], projected[i, 1]); Projection.Series[0].Points.Add(point); } } }