Linearalgebra - DLL Zedgraph - DLL

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 46

LinearAlgebra.dll Zedgraph.

dll

LinearAlgebra
TJU.AC


public Matrix(int row, int col) public Matrix(int row, int col, double value) public Matrix(double[,] array2D) public Matrix(Matrix matrix) public int RowCount // public int ColCount // public int Size //


public double this[int row, int col] public Matrix Translate() public Vector GetRow(int i) public Vector GetCol(int j) public void SetRow(int i, Vector rowVector) public void SetCol(int j, Vector colVector)


public static implicit operator Matrix(double[,] array2D)
Matrix m1 = array2D

public static explicit operator double[,](Matrix matrix)


double[,] array2D = (double[,])matrix;

public static Matrix operator +(Matrix matrix, double c) public static Matrix operator +(Matrix lhs, Matrix rhs) public static Vector operator *(Matrix matrix, Vector colVector) public static Vector operator *(Vector rowVector, Matrix matrix)


public static Matrix operator ~(Matrix matrix) public Matrix Inverse() // public static Matrix operator !(Matrix matrix) // public static Matrix LoadData(string path)// public static Matrix LoadExcel(string path) public static int Nipals(Matrix X, out Matrix T, out Matrix P, out Matrix Resiual, int PCs) public static void SVD(Matrix X, out Matrix U, out Matrix S, out Matrix V) public Matrix Pinv(double epsilon) //


public double averageError() public Matrix getSubMatrix(int segment,int nFold) public Matrix getSubMatrixPredict (int segment,int nFold) public double PRESS(Matrix other)

Vector
public Vector(int length) public Vector(double[] array) public Vector(Vector sourceVector) public Vector(int length, double c) public int Length public double this[int index] public double Model()

Vector
public void Normalize() public void Rand() public void Rand(int max) public void Rand(int min, int max) public static implicit operator Vector(double[] array) public static explicit operator double[](Vector vector) public static Vector operator -(Vector vector) public static Vector operator +(Vector vector, double c) public static Vector operator +(double c, Vector vector)

Vector
public static Vector operator +(Vector lhs, Vector rhs) public static Vector operator -(Vector vector, double c) public static Vector operator -(double c, Vector vector) public static Vector operator -(Vector lhs, Vector rhs) public static Vector operator *(Vector vector, double c) public double InnerProduct(Vector colVector) public Matrix OuterProduct(Vector rowVector) public double CalculateCosine(Vector vector2)

PCA

PCA
Principle Component Analysis


Xn*mX nmk X31020 X 3(10)

PCA
Xn*m ,

Xn*m = Tn*k * Pk*m


T P kn m

PCA
PCREFA HELP,FSMWFA


3 CO 10 9 *10 PCATT 2
t2

t1


1020 10X XPCA 1

t2

t1

t1 x1, x2 , x3, x4 Xi*p1i k1Xi*p1i k2Xi*p1i k3Xi*p1i t2 Xi*p2i k1Xi*p2i k2Xi*p2i k3 Xi*p2i

X=TP
T=XP

k1x1, k1x2, k1x3, k1x4 K2x1,k2x2, k2x3, k2x4

k3x1,k3x2,k3x3, k3x4
.

PCA
X=TP TP Xn*m k=min(n,m) T n*k P m*k

PCA
X k = 0, sigmaLamda = 0; Do while () tX Do w = t * X w = w /|w| tnew = X * w temp = tnew-t t=tnew While (temp>1e-6) // tTkwPk X= X-t*w thisLamda = t siamaLamda = sigmaLamda + thisLamda if (thisLamda / sigmaLamda <0.005) k1 enddo

PCA
public class PCA { Matrix X,T,P; Vec lamda; public PCA(Matrix XX) { this.X = new Matrix (XX.rowCount(),XX.colCount()); for (int i = 0; i < X.rowCount(); i++) { X.setRow(i,XX.getRow(i)); } int min = XX.rowCount()>XX.colCount()? XX.colCount():XX.rowCount(); this.P = new Matrix (XX.colCount(),min); this.T = new Matrix (XX.rowCount(),min); this.lamda = new Vec (min); } }

PCA
public int decompose(int PCs) // public Matrix getT() // public Matrix getP() // public Vec getLamda() //

PCA
public int decompose(int PCs) // 0 { int nPCs = 0; double sigmaLamda=0.0; int maxPCs = X.colCount()<X.rowCount()? X.colCount():X.rowCount(); if (PCs != 0 && PCs<maxPCs) maxPCs = PCs; int selectedCol = 0; Vec tmpVec = X.getRow(0); double tmpValue = tmpVec.getElement(0); for(int i = 1; i < tmpVec.length(); i++) { if (tmpValue < tmpVec.getElement(i)) { tmpValue = tmpVec.getElement(i); selectedCol = i; } }
for (; nPCs <maxPCs; nPCs++) { Vec t = X.getCol(selectedCol); Vec w; Vec temp; do{ w = X.multiBy(t); w.normalize(); Vec Tnew = X.multiTo(w); temp = Tnew.minusBy(t); t = Tnew; } while (temp.model()>1e-6); T.setCol(nPCs,t); P.setCol(nPCs,w); double temp1 = t.model(); sigmaLamda += temp1; lamda.setElement(nPCs,temp1*temp1); X = X.minusBy(t.outMulti(w));

PCA
if (PCs == 0) { if ((temp1/sigmaLamda)<0.005) { break; } } }

PCA
Matrix tempT,tempP; tempT = ModelManager.createMatrix(X.rowCount(),nPCs); tempP =ModelManager.createMatrix(X.colCount(),nPCs); for (int i=0; i<nPCs; i++) { Vec v = T.getCol(i); tempT.setCol(i,v); v = P.getCol(i); tempP.setCol(i,v); }

T = tempT; P = tempP; return nPCs;


}

2003excel
// try { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt files (*.xls)|*.xls|All files (*.*)|*.*"; openFileDialog.ShowDialog(); if (openFileDialog.FileName != "") { String str = openFileDialog.FileName; Matrix m = Matrix.LoadExcel(str); } } catch { MessageBox.Show("Data Entry Error\nTry again Please!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }

MatrixListview
private void matrixToListVew(Matrix m, ListView lv) { if (m == null) return; lv.View = View.Details; // lv.MultiSelect = false; // lv.HeaderStyle = ColumnHeaderStyle.Nonclickable; lv.Visible = true; lv.GridLines = true; lv.FullRowSelect = true; // for (int i = 0; i < m.colCount(); i++) { lv.Columns.Add("" + (i+1) + "", 60, HorizontalAlignment.Center); } for (int i = 0; i < m.rowCount(); i++) { ListViewItem li = new ListViewItem(); li.SubItems.Clear(); li.SubItems[0].Text = "" + m.getElement(i,0); for (int j = 1; j < m.colCount(); j++) { li.SubItems.Add("" + m.getElement(i,j)); } lv.Items.Add(li); } }

PCA

ZedGraph
ZedGraph is a class library Supports both .NET 2.0 and .NET 1.1
For .NET 2.0, use ZedGraph version 5.0+ For .NET 1.1, use ZedGraph version 4.5+

Both windows form and web page Support chart type: line, bar, pie (does not handle 2.5D or 3D surfaces or charts) ZedGraph .NET C#D

ZedGraph5.1.4 Framework2.0 5.0.0Framework1.1 4.3.4.17595 ZedGraphwindowsweb winform


ZedGraph.dll 1..NET Framework ZedGraph.dll 2.ZedGraphControl

3. ZedGraphControl, ,zgc

ZedGraph.dll

using ZedGraph


GraphPane myPane = zgc.GraphPane;

myPane.Title = ""; myPane.XAxis.Title= "PC1"; myPane.YAxis.Title = "PC2"; myPane.CurveList.Clear(); myPane.GraphItemList.Clear();

PointPairList list = new PointPairList(); list.Add( x, y );

PCA

PCAForm

ScorePlotForm

PCAForm
private void button2_Click(object sender, EventArgs e) { ScorePlotForm sp = new ScorePlotForm(T); sp.ShowDialog(); sp.Close(); }

ScorePlotForm
public ScorePlotForm(Matrix score) { InitializeComponent(); this.score = score; // }


2, 2, , 0, 1

2
private void prepareData(int col1, int col2, out int[] axis,out Matrix drawMat) { // score axis = new int[2]; axis[0] = col1; axis[1] = col2; drawMat = new Matrix(score.rowCount(), 2); drawMat.setCol(0, score.getCol( col1 )); drawMat.setCol(1, score.getCol( col2 )); }

FormLoad
private void ScorePlotForm_Load(object sender, EventArgs e) { classMark = new ArrayList(); classMark.Add(Y.getElement(0, 0)); for (int i = 1; i < Y.rowCount(); i++) { if (!classMark.Contains(Y.getElement(i, 0))) { classMark.Add(Y.getElement(i, 0)); } } label5.Text = " " + score.colCount() + " ! 0 " + (score.colCount()-1); Matrix drawMat; int[] axis; prepareData(0, 1, out axis, out drawMat); this.CreateGraph(zgc, drawMat, axis); zgc.IsShowPointValues = true; }

CreateGraph
private void CreateGraph(ZedGraphControl zgc, Matrix drawMat, int[] axis) { try { GraphPane myPane = zgc.GraphPane; // Set the titles and axis labels myPane.Title = ""; myPane.XAxis.Title = "PC" + axis[0]; myPane.YAxis.Title = "PC" + axis[1]; myPane.CurveList.Clear(); myPane.GraphItemList.Clear(); PointPairList list = new PointPairList(); for (int i = 0; i < drawMat.rowCount(); i++) { list.Add(drawMat.getElement(i,0), drawMat.getElement(i,1)); TextItem text = new TextItem((i + 1).ToString(), (float)drawMat.getElement(i,0), (float)drawMat.getElement(i,1));

if (Y.getElement(i,0) == (double)classMark[0]) text.FontSpec.Border.Color = Color.Green; else text.FontSpec.Border.Color = Color.Red; myPane.GraphItemList.Add(text); //


} LineItem myCurve1 = myPane.AddCurve("", list, Color.Blue, SymbolType.Circle); // , // myCurve1.Line.IsVisible = false; // zgc.AxisChange(); zgc.Refresh(); } catch (Exception err) { MessageBox.Show("Error:" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }


private void button1_Click(object sender, EventArgs e) { Matrix drawMat; int[] axis; prepareData(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text), out axis,out drawMat); this.CreateGraph(zgc, drawMat, axis); zgc.IsShowPointValues = true; zgc.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler); }

private string MyPointValueHandler(ZedGraphControl control, GraphPane pane, CurveItem curve, int iPt) { PointPair pt = curve[iPt]; return curve.Label + "(" + pt.X.ToString() + " "

+ pt.Y.ToString() + ")";

You might also like