public static void Train(double[][] dataMatrix) { Gaussian kernel = new Gaussian(10.0); var kpca = new KernelPrincipalComponentAnalysis(kernel); model = kpca.Learn(dataMatrix); }
public void transform_more_columns_than_samples_new_interface() { // Lindsay's tutorial data double[,] datat = data.Transpose(); var target = new KernelPrincipalComponentAnalysis(new Linear()); // Compute target.Learn(datat); // Transform double[,] actual = target.Transform(datat); // Assert the scores equals the transformation of the input double[,] result = target.Result; double[,] expected = new double[, ] { { 0.50497524691810358 }, { -0.504975246918104 } }.Multiply(-1); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.01)); Assert.IsTrue(Matrix.IsEqual(result, actual, 0.01)); }
// GET: DataAnalysis/PCA_Analysis/id public async Task <IActionResult> PcaAnalysis(int?id) { if (id == null) { return(NotFound()); } var dataAnalysis = await _context.FilesInformation.FindAsync(id); if (dataAnalysis == null) { return(NotFound()); } string path = await _context.FilesInformation.Where(m => m.Id == id).Select(d => d.Path).FirstOrDefaultAsync(); CsvReader reader = new CsvReader(_appEnvironment.WebRootPath + path, hasHeaders: true); double[][] actual = reader.ToJagged(); var pcaTool = new KernelPrincipalComponentAnalysis(); pcaTool.Learn(actual); pcaTool.NumberOfOutputs = 3; var outputMatrix = pcaTool.Transform(actual); ViewBag.columns = pcaTool.NumberOfOutputs; ViewBag.rows = outputMatrix.Length; List <double> PC1 = new List <double>(); List <double> PC2 = new List <double>(); List <double> PC3 = new List <double>(); for (int i = 0; i < outputMatrix.Length; i++) { for (int j = 0; j < pcaTool.NumberOfOutputs; j += 3) { outputMatrix[i][j] = Math.Round(outputMatrix[i][j], 3); outputMatrix[i][j + 1] = Math.Round(outputMatrix[i][j + 1], 3); outputMatrix[i][j + 2] = Math.Round(outputMatrix[i][j + 2], 3); PC1.Add(outputMatrix[i][j]); PC2.Add(outputMatrix[i][j + 1]); PC3.Add(outputMatrix[i][j + 2]); } } ViewBag.data = outputMatrix; ViewBag.PC1 = PC1; ViewBag.PC2 = PC2; ViewBag.PC3 = PC3; return(View()); }
public void learn_whiten_success() { double[,] data = { { 2.5, 2.4 }, { 0.5, 0.7 }, { 2.2, 2.9 }, { 1.9, 2.2 }, { 3.1, 3.0 }, { 2.3, 2.7 }, { 2.0, 1.6 }, { 1.0, 1.1 }, { 1.5, 1.6 }, { 1.1, 0.9 } }; var method = PrincipalComponentMethod.Center; // PrincipalComponentMethod.Standardize var pca = new KernelPrincipalComponentAnalysis(new Linear(), method, whiten: true); pca.Learn(data); double[] eigenvalues = { 1.28402771, 0.0490833989 }; double[] proportion = eigenvalues.Divide(eigenvalues.Sum()); double[,] eigenvectors = { { 0.19940687993951403, -1.1061252858739095 }, { 0.21626410214440508, 1.0199057073792104 } }; // Everything is alright (up to the 9 decimal places shown in the tutorial) // Assert.IsTrue(eigenvectors.IsEqual(pca.ComponentMatrix, rtol: 1e-9)); Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9)); Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues.Divide(data.GetLength(0) - 1), rtol: 1e-5)); double[,] actual = pca.Transform(data); double[][] expected = new double[][] { new double[] { 0.243560157209023, -0.263472650637184 }, new double[] { -0.522902576315494, 0.214938218565977 }, new double[] { 0.291870144299372, 0.578317788814594 }, new double[] { 0.0806632088164338, 0.19622137941132 }, new double[] { 0.492962746459375, -0.315204397734004 }, new double[] { 0.268558011864442, 0.263724118751361 }, new double[] { -0.0291545644762578, -0.526334573603598 }, new double[] { -0.336693495487974, 0.0698378585807067 }, new double[] { -0.128858004446015, 0.0267280693333571 }, new double[] { -0.360005627922904, -0.244755811482527 } }.Multiply(-1); // Everything is correct (up to 8 decimal places) Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8)); }
public void test_kernlab_new_method() { // Tested against R's kernlab // test <- c(16, 117, 94, 132, 13, 73, 68, 129, 91, 50, 56, 12, 145, 105, 35, 53, 38, 51, 85, 116) int[] test = { 15, 116, 93, 131, 12, 72, 67, 128, 90, 49, 55, 11, 144, 104, 34, 52, 37, 50, 84, 115 }; // data(iris) double[,] iris = create_iris(); // kpc <- kpca(~.,data=iris[-test,-5],kernel="rbfdot",kpar=list(sigma=0.2),features=2) var data = iris.Remove(test, new[] { 4 }); var kernel = new Gaussian() { Gamma = 1 }; var kpc = new KernelPrincipalComponentAnalysis(kernel); kpc.NumberOfOutputs = 2; var transform = kpc.Learn(data); var rotated = kpc.Result; var pcv = kpc.ComponentMatrix; var eig = kpc.Eigenvalues; double[] expected_eig = { 28.542404060412132, 15.235596653653861 }; double[,] expected_pcv = expected_r(); Assert.IsTrue(Matrix.IsEqual(expected_eig, eig, 1e-10)); Assert.IsTrue(Matrix.IsEqual(expected_pcv, pcv, 1e-10)); double[,] irisSubset = iris_sub(); var testing = iris.Submatrix(test, new[] { 0, 1, 2, 3 }); Assert.IsTrue(Matrix.IsEqual(irisSubset, testing)); double[,] expectedProjection = expected_p(); double[,] proj = kpc.Transform(testing); Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6)); double[][] proj2 = kpc.Transform(testing.ToJagged()); Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6)); double[][] proj3 = transform.Transform(testing.ToJagged()); Assert.IsTrue(expectedProjection.IsEqual(proj, 1e-6)); }
public void SerializeTest() { double[][] actual, expected = new double[][] { new double[] { -0.57497881446526, 0.0385634996866008 }, new double[] { 0.615576484818799, 0.463702189462175 }, new double[] { -0.593829949403244, 0.225530142279202 }, new double[] { -0.317225395167446, -0.470473936541537 }, new double[] { -0.372910594880684, 0.490761790918429 }, new double[] { -0.635435167456034, 0.154118097007375 }, new double[] { 0.0591892009931548, -0.659772491267272 }, new double[] { 0.739020019118434, 0.133927036952283 }, new double[] { 0.344183901764118, -0.559832164574368 }, new double[] { 0.736410314678163, 0.183475836077113 } }; var target = new KernelPrincipalComponentAnalysis() { Kernel = new Gaussian(0.7) }; target.Learn(data.ToJagged()); actual = target.Transform(data.ToJagged()); string str = actual.ToCSharp(); Assert.IsTrue(Matrix.IsEqual(actual, expected, 1e-10)); var copy = Serializer.DeepClone(target); actual = copy.Transform(data.ToJagged()); Assert.IsTrue(Matrix.IsEqual(actual, expected, 1e-10)); Assert.IsTrue(target.Kernel.Equals(copy.Kernel)); Assert.IsTrue(target.ComponentProportions.IsEqual(copy.ComponentProportions)); Assert.IsTrue(target.ComponentVectors.IsEqual(copy.ComponentVectors)); Assert.IsTrue(target.CumulativeProportions.IsEqual(copy.CumulativeProportions)); Assert.IsTrue(target.Eigenvalues.IsEqual(copy.Eigenvalues)); Assert.IsTrue(target.MaximumNumberOfOutputs.IsEqual(copy.MaximumNumberOfOutputs)); Assert.IsTrue(target.Method.Equals(copy.Method)); Assert.IsTrue(target.NumberOfInputs.IsEqual(copy.NumberOfInputs)); Assert.IsTrue(target.NumberOfOutputs.IsEqual(copy.NumberOfOutputs)); Assert.IsTrue(target.Overwrite.Equals(copy.Overwrite)); Assert.IsTrue(target.Whiten.Equals(copy.Whiten)); }
public void kernel_matrix_success() { var actual = new KernelPrincipalComponentAnalysis(new Linear(), PrincipalComponentMethod.KernelMatrix); var expected = new KernelPrincipalComponentAnalysis(data, new Linear(), AnalysisMethod.Standardize); Linear kernel = new Linear(); double[][] K = kernel.ToJagged(data.ZScores().ToJagged()); // Compute actual.Learn(K); expected.Compute(); // Transform double[][] actualTransform = actual.Transform(K); double[][] expectedTransform1 = expected.Transform(data).ToJagged(); double[][] expectedTransform2 = expected.Transform(data.ToJagged()); // Verify both are equal with 0.01 tolerance value Assert.IsTrue(Matrix.IsEqual(actualTransform, expectedTransform1, 0.01)); Assert.IsTrue(Matrix.IsEqual(actualTransform, expectedTransform2, 0.01)); }
public void RevertTest2_new_method() { string path = @"Resources\examples.xls"; // Create a new reader, opening a given path ExcelReader reader = new ExcelReader(path); // Afterwards, we can query the file for all // worksheets within the specified workbook: string[] sheets = reader.GetWorksheetList(); // Finally, we can request an specific sheet: DataTable table = reader.GetWorksheet("Wikipedia"); // Now, we have loaded the Excel file into a DataTable. We // can go further and transform it into a matrix to start // running other algorithms on it: double[][] matrix = table.ToArray(); IKernel kernel = new Gaussian(5); // Create analysis var target = new KernelPrincipalComponentAnalysis(kernel) { Method = PrincipalComponentMethod.Center, Center = true // Center in feature space }; var regression = target.Learn(matrix); double[][] forward = regression.Transform(matrix); double[][] reversion = target.Revert(forward); Assert.IsTrue(!reversion.HasNaN()); }
/// <summary> /// Launched when the user clicks the "Run analysis" button. /// </summary> /// private void btnCompute_Click(object sender, EventArgs e) { // Save any pending changes dgvAnalysisSource.EndEdit(); if (dgvAnalysisSource.DataSource == null) { MessageBox.Show("Please load some data using File > Open!"); return; } // Create a matrix from the source data table double[][] sourceMatrix = (dgvAnalysisSource.DataSource as DataTable).ToJagged(out columnNames); // Create and compute a new Simple Descriptive Analysis sda = new DescriptiveAnalysis() { ColumnNames = columnNames }; sda.Learn(sourceMatrix); // Show the descriptive analysis on the screen dgvDistributionMeasures.DataSource = sda.Measures; // Create the kernel function IKernel kernel = createKernel(); // Get the input values (the two first columns) this.inputs = sourceMatrix.GetColumns(0, 1); // Get only the associated labels (last column) this.outputs = sourceMatrix.GetColumn(2).ToMulticlass(); var method = (PrincipalComponentMethod)cbMethod.SelectedValue; // Creates the Kernel Principal Component Analysis of the given source kpca = new KernelPrincipalComponentAnalysis() { Kernel = kernel, Method = method }; // Whether to center in space kpca.Center = cbCenter.Checked; var classifier = kpca.Learn(inputs); // Finally, compute the analysis! double[][] result = kpca.Transform(inputs); double[][] reduced; if (kpca.Components.Count >= 2) { // Perform the transformation of the data using two components kpca.NumberOfOutputs = 2; reduced = kpca.Transform(inputs); } else { kpca.NumberOfOutputs = 1; reduced = kpca.Transform(inputs); reduced = reduced.InsertColumn(Vector.Zeros(reduced.GetLength(0))); } // Create a new plot with the original Z column double[][] points = reduced.InsertColumn(sourceMatrix.GetColumn(2)); // Create output scatter plot outputScatterplot.DataSource = points; CreateScatterplot(graphMapFeature, points); // Create output table dgvProjectionResult.DataSource = new ArrayDataView(points, columnNames); dgvReversionSource.DataSource = new ArrayDataView(result); // Populates components overview with analysis data dgvFeatureVectors.DataSource = new ArrayDataView(kpca.ComponentVectors); dgvPrincipalComponents.DataSource = kpca.Components; cumulativeView.DataSource = kpca.Components; distributionView.DataSource = kpca.Components; numNeighbor.Maximum = result.Rows(); numNeighbor.Value = System.Math.Min(10, numNeighbor.Maximum); lbStatus.Text = "Good! Feel free to browse the other tabs to see what has been found."; }
public void learn_success() { #region doc_learn_1 // Reproducing Lindsay Smith's "Tutorial on Principal Component Analysis" // using the framework's default method. The tutorial can be found online // at http://www.sccg.sk/~haladova/principal_components.pdf // Step 1. Get some data // --------------------- double[][] data = { new double[] { 2.5, 2.4 }, new double[] { 0.5, 0.7 }, new double[] { 2.2, 2.9 }, new double[] { 1.9, 2.2 }, new double[] { 3.1, 3.0 }, new double[] { 2.3, 2.7 }, new double[] { 2.0, 1.6 }, new double[] { 1.0, 1.1 }, new double[] { 1.5, 1.6 }, new double[] { 1.1, 0.9 } }; // Step 2. Subtract the mean // ------------------------- // Note: The framework does this automatically. By default, the framework // uses the "Center" method, which only subtracts the mean. However, it is // also possible to remove the mean *and* divide by the standard deviation // (thus performing the correlation method) by specifying "Standardize" // instead of "Center" as the AnalysisMethod. var method = PrincipalComponentMethod.Center; // PrincipalComponentMethod.Standardize // Step 3. Compute the covariance matrix // ------------------------------------- // Note: Accord.NET does not need to compute the covariance // matrix in order to compute PCA. The framework uses the SVD // method which is more numerically stable, but may require // more processing or memory. In order to replicate the tutorial // using covariance matrices, please see the next unit test. // Create the analysis using the selected method var pca = new KernelPrincipalComponentAnalysis(new Linear(), method); // Compute it pca.Learn(data); // Step 4. Compute the eigenvectors and eigenvalues of the covariance matrix // ------------------------------------------------------------------------- // Note: Since Accord.NET uses the SVD method rather than the Eigendecomposition // method, the Eigenvalues are computed from the singular values. However, it is // not the Eigenvalues themselves which are important, but rather their proportion: // Those are the expected eigenvalues, in descending order: double[] eigenvalues = { 1.28402771, 0.0490833989 }; // And this will be their proportion: double[] proportion = eigenvalues.Divide(eigenvalues.Sum()); Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9)); Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues.Divide(data.GetLength(0) - 1), rtol: 1e-5)); // Step 5. Deriving the new data set // --------------------------------- double[][] actual = pca.Transform(data); // transformedData shown in pg. 18 double[,] expected = new double[, ] { { 0.827970186, -0.175115307 }, { -1.77758033, 0.142857227 }, { 0.992197494, 0.384374989 }, { 0.274210416, 0.130417207 }, { 1.67580142, -0.209498461 }, { 0.912949103, 0.175282444 }, { -0.099109437, -0.349824698 }, { -1.14457216, 0.046417258 }, { -0.438046137, 0.017764629 }, { -1.22382056, -0.162675287 }, }.Multiply(-1); // Everything is correct (up to 8 decimal places) Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8)); // Finally, we can project all the data double[][] output1 = pca.Transform(data); // Or just its first components by setting // NumberOfOutputs to the desired components: pca.NumberOfOutputs = 1; // And then calling transform again: double[][] output2 = pca.Transform(data); // We can also limit to 80% of explained variance: pca.ExplainedVariance = 0.8; // And then call transform again: double[][] output3 = pca.Transform(data); #endregion actual = pca.Transform(data); // transformedData shown in pg. 18 expected = new double[, ] { { 0.827970186 }, { -1.77758033, }, { 0.992197494 }, { 0.274210416 }, { 1.67580142, }, { 0.912949103 }, { -0.099109437 }, { -1.14457216, }, { -0.438046137 }, { -1.22382056, }, }.Multiply(-1); // Everything is correct (up to 8 decimal places) Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8)); // Create the analysis using the selected method pca = new KernelPrincipalComponentAnalysis() { Kernel = new Linear(), Method = method, NumberOfOutputs = 1 }; // Compute it pca.Learn(data); actual = pca.Transform(data); // transformedData shown in pg. 18 expected = new double[, ] { { 0.827970186 }, { -1.77758033, }, { 0.992197494 }, { 0.274210416 }, { 1.67580142, }, { 0.912949103 }, { -0.099109437 }, { -1.14457216, }, { -0.438046137 }, { -1.22382056, }, }.Multiply(-1); // Everything is correct (up to 8 decimal places) Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8)); }