void Test_DoWork(object sender, DoWorkEventArgs e) { double counter = 0, accurate = 0; for (int i = 6; i < numberofimages; i += 7) { counter++; var img = BitmapFactory.New(1, 1).FromStream(info[i].OpenRead()); if (img.PixelWidth != 100 || img.PixelHeight != 150) { img = img.Resize(100, 150, WriteableBitmapExtensions.Interpolation.Bilinear); } var mat = helper.imgtomat(img.ToByteArray()); if (decide == 0) { List <projectedTrainingMatrix> projectedTrainingSet = pca.getProjectedTrainingSet(); double[][] testCase = pca.getW().Transpose().Multiply(mat.Subtract(pca.getMeanMatrix()).ToArray()); String result = KNN.assignLabel(projectedTrainingSet.ToArray(), testCase, 3, ED); if (int.Parse(result.Substring(1)) == ((i + 1) / 7)) { accurate++; } else { } // MessageBox.Show(result); } else { List <projectedTrainingMatrix> projectedTrainingSet = lda.getProjectedTrainingSet(); double[][] testCase = lda.getW().Transpose().Multiply(mat.Subtract(lda.getMeanMatrix()).ToArray()); String result = KNN.assignLabel(projectedTrainingSet.ToArray(), testCase, 3, ED); if (int.Parse(result.Substring(1)) == ((i + 1) / 7)) { accurate++; } else { } //MessageBox.Show(result); // } } } MessageBox.Show("tested : " + counter + "\n accurate :" + accurate.ToString()); // }
public LDA(double[][] trainingSet, List<string> labels,int numOfComponents) { int n = trainingSet.Length; // sample size HashSet<String> tempSet = new HashSet<String>(labels); int c = tempSet.Count; // class size // process in PCA PCA pca = new PCA(trainingSet, labels, n - c); // classify double[][] meanTotal = new double[n - c][]; for (int i = 0; i < n - c; i++) { meanTotal[i] = new double[1]; } Dictionary<String, List<double[]>> dict = new Dictionary<String, List<double[]>>(); List<projectedTrainingMatrix> pcaTrain = pca.getProjectedTrainingSet(); for (int i = 0; i < pcaTrain.Count; i++) { String key = pcaTrain[i].label; meanTotal= meanTotal.Add(pcaTrain[i].matrix); if (!dict.ContainsKey(key)) { List<double[]> temp = new List<double[]>(); temp.Add(pcaTrain[i].matrix.Transpose()[0]); dict.Add(key, temp); } else { List<double[]> temp = dict[key]; temp.Add(pcaTrain[i].matrix.Transpose()[0]); dict[key]= temp; } } meanTotal.ToMatrix().Multiply((double) 1 / n); // calculate Sw, Sb double[][] Sw = new double[n - c][]; double[][] Sb = new double[n - c][]; for (int i = 0; i < n - c; i++) { Sw[i] = new double[n-c]; Sb[i] = new double[n-c]; } List<String> labelSet = dict.Keys.ToList(); foreach(string label in labelSet) { List<double[]> tempMatrix = dict[label]; double[][] matrixWithinThatClass = tempMatrix.ToArray(); double[] meanOfCurrentClass = Accord.Statistics.Tools.Mean(matrixWithinThatClass); for (int i = 0; i < matrixWithinThatClass.Length; i++) { double[][] temp1 = matrixWithinThatClass[i].ToArray().Subtract(meanOfCurrentClass.ToArray()); temp1 = temp1.Multiply(temp1.Transpose()); Sw =Sw.Add(temp1); } double[][] temp = meanOfCurrentClass.ToArray().Subtract(meanTotal); temp = temp.Multiply(temp.Transpose()).ToMatrix().Multiply((double)matrixWithinThatClass.Length).ToArray(); Sb=Sb.Add(temp); } // calculate the eigenvalues and vectors of Sw^-1 * Sb double [][] targetForEigen = Sw.Inverse().Multiply(Sb); var feature = new EigenvalueDecomposition(targetForEigen.ToMatrix()); double[] d = feature.RealEigenvalues; int[] indexes; d.StableSort(out indexes); indexes = indexes.Reverse().ToArray(); indexes = indexes.Submatrix(0, c - 1); //int[] indexes = getIndexesOfKEigenvalues(d, c - 1); double[][] eigenVectors = feature.Eigenvectors.ToArray(); double[][] selectedEigenVectors = eigenVectors.Submatrix(0, eigenVectors.Length - 1, indexes); this.W = pca.getW().Multiply(selectedEigenVectors); // Construct projectedTrainingMatrix this.projectedTrainingSet = new List<projectedTrainingMatrix>(); for (int i = 0; i < trainingSet.Length; i++) { projectedTrainingMatrix ptm = new projectedTrainingMatrix(this.W.Transpose().Multiply(trainingSet[i].Subtract(pca.meanMatrix).ToArray()), labels[i]); this.projectedTrainingSet.Add(ptm); } this.meanMatrix= pca.meanMatrix; GC.Collect(); }
public LDA(double[][] trainingSet, List <string> labels, int numOfComponents) { int n = trainingSet.Length; // sample size HashSet <String> tempSet = new HashSet <String>(labels); int c = tempSet.Count; // class size // process in PCA PCA pca = new PCA(trainingSet, labels, n - c); // classify double[][] meanTotal = new double[n - c][]; for (int i = 0; i < n - c; i++) { meanTotal[i] = new double[1]; } Dictionary <String, List <double[]> > dict = new Dictionary <String, List <double[]> >(); List <projectedTrainingMatrix> pcaTrain = pca.getProjectedTrainingSet(); for (int i = 0; i < pcaTrain.Count; i++) { String key = pcaTrain[i].label; meanTotal = meanTotal.Add(pcaTrain[i].matrix); if (!dict.ContainsKey(key)) { List <double[]> temp = new List <double[]>(); temp.Add(pcaTrain[i].matrix.Transpose()[0]); dict.Add(key, temp); } else { List <double[]> temp = dict[key]; temp.Add(pcaTrain[i].matrix.Transpose()[0]); dict[key] = temp; } } meanTotal.ToMatrix().Multiply((double)1 / n); // calculate Sw, Sb double[][] Sw = new double[n - c][]; double[][] Sb = new double[n - c][]; for (int i = 0; i < n - c; i++) { Sw[i] = new double[n - c]; Sb[i] = new double[n - c]; } List <String> labelSet = dict.Keys.ToList(); foreach (string label in labelSet) { List <double[]> tempMatrix = dict[label]; double[][] matrixWithinThatClass = tempMatrix.ToArray(); double[] meanOfCurrentClass = Accord.Statistics.Tools.Mean(matrixWithinThatClass); for (int i = 0; i < matrixWithinThatClass.Length; i++) { double[][] temp1 = matrixWithinThatClass[i].ToArray().Subtract(meanOfCurrentClass.ToArray()); temp1 = temp1.Multiply(temp1.Transpose()); Sw = Sw.Add(temp1); } double[][] temp = meanOfCurrentClass.ToArray().Subtract(meanTotal); temp = temp.Multiply(temp.Transpose()).ToMatrix().Multiply((double)matrixWithinThatClass.Length).ToArray(); Sb = Sb.Add(temp); } // calculate the eigenvalues and vectors of Sw^-1 * Sb double [][] targetForEigen = Sw.Inverse().Multiply(Sb); var feature = new EigenvalueDecomposition(targetForEigen.ToMatrix()); double[] d = feature.RealEigenvalues; int[] indexes; d.StableSort(out indexes); indexes = indexes.Reverse().ToArray(); indexes = indexes.Submatrix(0, c - 1); //int[] indexes = getIndexesOfKEigenvalues(d, c - 1); double[][] eigenVectors = feature.Eigenvectors.ToArray(); double[][] selectedEigenVectors = eigenVectors.Submatrix(0, eigenVectors.Length - 1, indexes); this.W = pca.getW().Multiply(selectedEigenVectors); // Construct projectedTrainingMatrix this.projectedTrainingSet = new List <projectedTrainingMatrix>(); for (int i = 0; i < trainingSet.Length; i++) { projectedTrainingMatrix ptm = new projectedTrainingMatrix(this.W.Transpose().Multiply(trainingSet[i].Subtract(pca.meanMatrix).ToArray()), labels[i]); this.projectedTrainingSet.Add(ptm); } this.meanMatrix = pca.meanMatrix; GC.Collect(); }