本文整理汇总了C#中Matrix.GetColumnVector方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.GetColumnVector方法的具体用法?C# Matrix.GetColumnVector怎么用?C# Matrix.GetColumnVector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix.GetColumnVector方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: start
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();
NIPALS(X, PCs, PCmatrix, Weights);
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", PCmatrix);
stream.set("Weights", Weights);
parent.next(response, session);
}
示例2: IRID182_Eigenvalues
//.........这里部分代码省略.........
-0.521887360236719, 0.294726342538363, -0.204193677592214,
-0.0758472091590544, -0.401936443046788, 0.37498137218359,
-0.129585782998913, 0.517649461236531, 0.273871905878737,
-0.322714035020032, 0.536190051081469, -0.34125462486497
},
{
-0.0814483582258942, 0.0927869109175177, -0.214481531745175,
-0.0932037350339198, -0.0234089455262449, -0.129585782998913,
0.910194610272133, 0.0543719437593689, -0.59175909723612,
0.011081021028939, 0.235777494730137, -0.170324529941829
},
{
-0.939682389503636, 0.726403468926861, -0.442608559845998,
-0.165672162129375, -0.862071919853614, 0.517649461236531,
0.0543719437593689, 1.18519903822716, 0.226643546080826,
-0.742715732562644, 1.20491236837527, -0.762429062710757
},
{
-0.210173828668251, 0.0862640603521092, 0.026150314424896,
-0.00707090905640249, -0.216526083877413, 0.273871905878737,
-0.59175909723612, 0.226643546080826, 0.621099014778964,
-0.217571234379085, 0.0386828111635692, -0.0296104994618287,
},
{
0.564932147694539, -0.489794367061056, 0.227433408251588,
0.026167220128558, 0.571650829476108, -0.322714035020032,
0.011081021028939, -0.742715732562644, -0.217571234379085,
0.557123237502864, -0.694564753107317, 0.508972258047537
},
{
-1.0154353322131, 0.744944058771799, -0.513686177880137,
-0.193113416527151, -0.854054719662501, 0.536190051081469,
0.235777494730137, 1.20491236837527, 0.0386828111635692,
-0.694564753107317, 1.347356880385, -0.837009265117046
},
{
0.640685090404004, -0.508334956905994, 0.298511026285728,
0.0536084745263342, 0.563633629284995, -0.34125462486497,
-0.170324529941829, -0.762429062710757, -0.0296104994618287,
0.508972258047537, -0.837009265117046, 0.583552460453826
}
});
// Expected data evaluated with MATLAB "[V,D] = eig(A)"
ComplexVector expectedEigenValues = ComplexVector.Create(new double[] {
0d, 0d, 0d, 0d,
0.00262233860281324,
0.0210323773292213,
0.0355556528747382,
0.127624731796062,
0.179718416331653,
0.310576269081767,
1.52253860917203,
5.80033160481171
});
Matrix expectedEigenVectors = new Matrix(new double[][] {
new double[] { 0.106498497282379, 0.419691143613967, -0.0557817580954078, -0.00690851159493115, 0.371136426079307, 0.223461682210106, -0.468606464889867, 0.343527867020022, 0.0756466774996531, -0.369826279577119, -0.0284941597416966, 0.375195260395109 },
new double[] { 0.106498497282348, 0.419691143613984, -0.0557817580953903, -0.00690851159494274, 0.347453164986349, -0.567931379282469, 0.448676542846663, 0.0426012577167882, -0.00926728212925367, -0.290807685971263, -0.0241538261982648, -0.278790999189846 },
new double[] { 0.141668469389994, 0.156579178192424, -0.546459905897362, -0.320022417617439, -0.370241497490735, 0.332713332831733, 0.399336647042652, -0.104765030616846, 0.0739529635375817, -0.283780350824145, 0.138326048745256, 0.176134628893008 },
new double[] { 0.456535390675942, -0.214226432376964, 0.109822773758153, 0.133529424410943, -0.191583966179205, -0.266148198349251, -0.313325326798921, -0.506526681171299, 0.0272792573144116, -0.495783211258065, 0.0584677852966494, 0.059551430007216 },
new double[] { 0.364647620387872, 0.0977045432532739, 0.436554618830564, 0.335448611182954, 0.163995962609024, 0.328414432657394, 0.477700760063001, -0.152793609708388, 0.192188934685942, 0.137049561691855, -0.0685542301292428, 0.328126132911985 },
new double[] { -0.0118021512337526, 0.687830921400826, 0.0253622787075734, 0.0340672396524277, -0.128932474003535, 0.111659773051072, -0.253460159931006, -0.480758764565753, -0.0957818149202032, 0.338676131657749, 0.180081795108562, -0.210185274196965 },
new double[] { 0.283462964841237, 0.0345316526707189, -0.136594149073437, -0.0420356194913279, -0.0133246227195541, 0.102617378729154, -0.00943588067870301, -0.0313070656278569, -0.574003455674121, 0.0681122379738542, -0.741712896390675, -0.0446883956970386 },
new double[] { 0.244849290172628, 0.0918403159398603, -0.0949837111837109, 0.577123713866291, -0.38448721213687, 0.141537415024233, -0.0419331454701546, 0.452402089212107, 0.0853436565432261, -0.0866385554860394, 0.0434070293735178, -0.445597633077243 },
new double[] { 0.400790602926352, -0.199507773642233, -0.135930857423972, -0.0303383327890531, 0.343450721385221, 0.108021571075484, 0.0137877512860494, 0.0892884039126865, -0.512143067951694, 0.158808597386327, 0.590928375174204, -0.0752948350722554 },
new double[] { 0.221355443237417, -0.0542519363253111, -0.585993504810665, 0.258161164492889, 0.123510354803434, -0.32794171148589, -0.105403788999763, -0.0520352776917921, 0.325720237275597, 0.455414054946441, -0.0958514753059141, 0.280573659051638 },
new double[] { 0.407153336780693, -0.096005525911419, 0.0677148671312564, -0.494402873973341, 0.192742899138734, 0.18832136952349, -0.123734336631771, 0.0245713241631777, 0.481424400615441, 0.12926200568014, -0.142716239407588, -0.467562669903876 },
new double[] { 0.316238576923814, 0.181825098245019, 0.312639383751487, -0.345156725151147, -0.45371975647163, -0.374725665985126, -0.0236025978381509, 0.375795487357127, -0.0703605067966124, 0.23951349378026, 0.0902717934751914, 0.30253869587827 }
});
// Verify the eigen values
ComplexVector eigenValues = m.EigenValues;
Assert.That(eigenValues.Length, Is.EqualTo(12), "Eigenvalue Length");
Assert.That(eigenValues, NumericIs.AlmostEqualTo(expectedEigenValues, 1e-13), "Eigenvalue Values");
// verify the eigen vectors, except the first 4 (since their eigen values are 0)
Matrix eigenVectors = m.EigenVectors;
Assert.That(eigenVectors.RowCount, Is.EqualTo(12), "Eigenvector Rows");
Assert.That(eigenVectors.ColumnCount, Is.EqualTo(12), "Eigenvector Columns");
for(int i = 4; i < 12; i++)
{
Vector a = expectedEigenVectors.GetColumnVector(i);
Vector b = eigenVectors.GetColumnVector(i);
// Normalize sign
if(a[0] < 0)
{
a.NegateInplace();
}
if(b[0] < 0)
{
b.NegateInplace();
}
// Compare
Assert.That(b, NumericIs.AlmostEqualTo(a, 1e-11), "Eigenvector Values: " + i.ToString());
}
}
示例3: irisPCA
public static void irisPCA()
{
//setup to read from CSV
String CSVfilePath = "C:\\dataminingproject";
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CSVfilePath + ";Extended Properties='text;HDR=Yes;FMT=Delimited'";
//Setup connection
OleDbConnection connection = new OleDbConnection(connectionString);
//read everything
OleDbCommand cmd = new OleDbCommand("SELECT * FROM " + "iris.csv", connection);
//table to hold the data
System.Data.DataTable dt = new System.Data.DataTable();
//adapter to read the data
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
connection.Open();
da.Fill(dt);
//data should be in the table now
// create a matrix to hold the data, ignore species
// Matrix iris = new Matrix (dt.Rows.Count, dt.Columns.Count -1);
//select columns
dt.Columns.Remove(dt.Columns[4]);
double[,] dataArray = new double[dt.Rows.Count,dt.Columns.Count];
// double sample = Array.ConvertAll<System.Data.DataRow, double[]>(dt.Select(),;
for(int i = 0; i<dt.Rows.Count;i++){
Console.Out.Write ("[ ");
for(int j = 0; j<dt.Columns.Count;j++){
dataArray[i, j] = (double)dt.Rows[i].ItemArray.ElementAt(j);
Console.Out.Write (dt.Rows[i].ItemArray.ElementAt(j).ToString() + ", ");
}
Console.Out.WriteLine (" ]");
}
//dt.Rows.CopyTo(dataArray, 0);
//double[] dataArray = Array.ConvertAll(
Matrix iris = new Matrix(dataArray);
printMatrix(iris, "iris");
//remove mean
for (int i = 0; i < iris.ColumnCount; i++)
iris.SetColumnVector(iris.GetColumnVector(i).Subtract(iris.GetColumnVector(i).Average()), i);
int PCs = 2;
Matrix PCmatrix = new Matrix(iris.ColumnCount, PCs, 0);
Vector EigenValues = new Vector(PCs);
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
try
{
timer.Start();
NIPALS(iris, PCs, PCmatrix, EigenValues);
timer.Stop();
}
catch (Exception e)
{
Console.Out.WriteLine(e.ToString());
Console.In.ReadLine();
return;
}
Console.Out.WriteLine("NIPALS Time: " + timer.ElapsedMilliseconds);
printMatrix(PCmatrix, "Principal Components");
printMatrix(EigenValues.ToRowMatrix(), "Weights");
Console.Out.WriteLine("SVD:");
timer.Reset();
timer.Start();
SingularValueDecomposition svd = iris.SingularValueDecomposition;
timer.Stop();
Console.Out.WriteLine("SVD Time: " + timer.ElapsedMilliseconds);
//Console.Out.WriteLine("LSV: ");
//printMatrix(svd.LeftSingularVectors);
Console.Out.WriteLine("RSV: ");
printMatrix(svd.RightSingularVectors);
Console.Out.WriteLine("S: ");
printMatrix(svd.S);
Console.Out.WriteLine("Singular Values: ");
printMatrix(svd.SingularValues.ToRowMatrix());
Console.In.ReadLine();
// Console.Out.WriteLine(dt.Rows[0].ItemArray.Take(4).ToArray<double>());
/*
for (int i=0;i<iris.ColumnCount;i++){
iris.SetColumnVector(dt.Columns[i].Container.Components.GetEnumerator.
}
*/
}
示例4: Main
//private static MathNet.Numerics.Algorithms.LinearAlgebra.Atlas.AtlasLinearAlgebraProvider provider;
static void Main(string[] args)
{
Console.Out.WriteLine ("Hello...");
irisPCA();
//provider = new MathNet.Numerics.Algorithms.LinearAlgebra.Atlas.AtlasLinearAlgebraProvider();
/*
double[,] A = new double[2,2];
A[0, 0] = 5;
A[0, 1] = 0;
A[1, 0] = 0;
A[1, 1] = 10;
/*
double[,] A = new double[3, 2];
A[0, 0] = 5;
A[0, 1] = 0;
A[1, 0] = 0;
A[1, 1] = 10;
A[2, 0] = 5;
A[2, 1] = 15;
*/
double[,] A = new double[4, 3];
A[0, 0] = 1;
A[0, 1] = 2;
A[0, 2] = 3;
A[1, 0] = 4;
A[1, 1] = 5;
A[1, 2] = 6;
A[2, 0] = 6;
A[2, 1] = 5;
A[2, 2] = 4;
A[3, 0] = 3;
A[3, 1] = 2;
A[3, 2] = 1;
Matrix X = new Matrix(A);
//remove mean
for (int i=0;i<X.ColumnCount;i++)
X.SetColumnVector(X.GetColumnVector(i).Subtract(X.GetColumnVector(i).Average()),i);
printMatrix(X, "X");
int PCs = 2;
Matrix PCmatrix = new Matrix(X.ColumnCount, PCs, 0);
Vector EigenValues = new Vector(PCs);
try
{
NIPALS(X, PCs, PCmatrix, EigenValues);
}
catch (Exception e)
{
Console.Out.WriteLine(e.ToString());
Console.In.ReadLine();
return;
}
printMatrix(PCmatrix, "Principal Components");
double projection = PCmatrix.GetColumnVector(0).ScalarMultiply(PCmatrix.GetColumnVector(1));
Console.Out.WriteLine("projection: " + projection);
printMatrix(EigenValues.ToRowMatrix(), "Weights");
Console.Out.WriteLine("SVD:");
SingularValueDecomposition svd = X.SingularValueDecomposition;
Console.Out.WriteLine("LSV: ");
printMatrix(svd.LeftSingularVectors);
Console.Out.WriteLine("RSV: ");
printMatrix(svd.RightSingularVectors);
Console.Out.WriteLine("S: ");
printMatrix(svd.S);
Console.Out.WriteLine("Singular Values: ");
printMatrix(svd.SingularValues.ToRowMatrix());
Console.In.ReadLine();
}
示例5: Page_Load
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);
}
}
}
示例6: addcontrol
//.........这里部分代码省略.........
Projection.Width = Unit.Pixel(Convert.ToInt16(cell.Width.Substring(0, cell.Width.Length - 2)) * cell.ColSpan - 2 * (layouttable.Border + layouttable.CellPadding));
Projection.Height = Unit.Pixel(Convert.ToInt16(row.Height.Substring(0, row.Height.Length - 2)) * cell.RowSpan - 2 * (layouttable.Border + layouttable.CellPadding));
DataMiningApp.Analysis.ParameterStream stream;
Registry.Registry registry;
stream = DataMiningApp.Analysis.ParameterStream.getStream(Session);
registry = Registry.Registry.getRegistry(Session);
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 = "Species";
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);