Exemple #1
0
        public void correlation_success()
        {
            double[] mean   = Measures.Mean(data, dimension: 0);
            double[] stdDev = Measures.StandardDeviation(data);
            double[,] cov = Measures.Correlation(data);

            var actual   = PrincipalComponentAnalysis.FromCorrelationMatrix(mean, stdDev, cov);
            var expected = new PrincipalComponentAnalysis(data, AnalysisMethod.Standardize);

            // Compute
            actual.Compute();
            expected.Compute();

            // Transform
            double[,] actualTransform   = actual.Transform(data);
            double[,] expectedTransform = expected.Transform(data);


            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(actualTransform, expectedTransform, 0.01));

            // Transform
            double[,] image   = actual.Transform(data);
            double[,] reverse = actual.Revert(image);

            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(reverse, data, 0.01));
        }
Exemple #2
0
 private void buildModel()
 {
     if (varCov == null)
     {
         getCov();
     }
     pca = PrincipalComponentAnalysis.FromCorrelationMatrix(MeanVector, StdVector, CorralationMatrix);
     pca.Compute();
     egVec = pca.ComponentMatrix;
     prop  = pca.ComponentProportions;
     egVal = pca.Eigenvalues;
     //Console.WriteLine("PCA method = " + pca.Method.ToString());
 }
Exemple #3
0
        public void correlation_new_interface()
        {
            double[]   mean   = Measures.Mean(data, dimension: 0);
            double[]   stdDev = Measures.StandardDeviation(data);
            double[][] cov    = Measures.Correlation(data.ToJagged());

            var actual   = PrincipalComponentAnalysis.FromCorrelationMatrix(mean, stdDev, cov.ToMatrix());
            var expected = new PrincipalComponentAnalysis(PrincipalComponentMethod.CorrelationMatrix)
            {
                Means = mean,
                StandardDeviations = stdDev
            };

            // Compute
            actual.Compute();
            var transform = expected.Learn(cov);

            // Transform
            double[,] actualTransform   = actual.Transform(data);
            double[,] expectedTransform = expected.Transform(data);

            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(actualTransform, expectedTransform, 0.01));

            // Transform
            double[,] image   = actual.Transform(data);
            double[,] reverse = actual.Revert(image);

            // Verify both are equal with 0.01 tolerance value
            Assert.IsTrue(Matrix.IsEqual(reverse, data, 1e-6));

            // Transform
            double[][] image2   = transform.Transform(data.ToJagged());
            double[][] reverse2 = transform.Inverse().Transform(image2);
            Assert.IsTrue(Matrix.IsEqual(reverse, reverse2, 1e-6));
            Assert.IsTrue(Matrix.IsEqual(reverse2, data, 1e-6));

            // Transform
            double[][] reverse3 = actual.Revert(image2);
            Assert.IsTrue(Matrix.IsEqual(reverse, reverse3, 1e-6));
            Assert.IsTrue(Matrix.IsEqual(reverse3, data, 1e-6));

            var a = transform.Transform(data.ToJagged()).ToMatrix();

            Assert.IsTrue(Matrix.IsEqual(a, expectedTransform, 0.01));
        }
Exemple #4
0
 public void buildModel(string modelPath)
 {
     outmodelpath = modelPath;
     using (System.IO.StreamReader sr = new System.IO.StreamReader(outmodelpath))
     {
         dataPrepBase.modelTypes mType = (dataPrepBase.modelTypes)Enum.Parse(typeof(dataPrepBase.modelTypes), sr.ReadLine());
         if (mType != dataPrepBase.modelTypes.PCA)
         {
             egVec = new double[1, 1];
             System.Windows.Forms.MessageBox.Show("Not a PCA Model!!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
             return;
         }
         inpath             = sr.ReadLine();
         VariableFieldNames = sr.ReadLine().Split(new char[] { ',' });
         corr       = new double[VariableFieldNames.Length, VariableFieldNames.Length];
         egVec      = new double[VariableFieldNames.Length, VariableFieldNames.Length];
         n          = System.Convert.ToInt32(sr.ReadLine());
         meanVector = (from string s in sr.ReadLine().Split(new char[] { ',' }) select System.Convert.ToDouble(s)).ToArray();
         stdVector  = (from string s in sr.ReadLine().Split(new char[] { ',' }) select System.Convert.ToDouble(s)).ToArray();
         string[] corrLg = sr.ReadLine().Split(new char[] { ',' });
         prop  = (from string s in sr.ReadLine().Split(new char[] { ',' }) select System.Convert.ToDouble(s)).ToArray();
         egVal = (from string s in sr.ReadLine().Split(new char[] { ',' }) select System.Convert.ToDouble(s)).ToArray();
         string[] egVecLg = sr.ReadLine().Split(new char[] { ',' });
         for (int i = 0; i < VariableFieldNames.Length; i++)
         {
             for (int j = 0; j < VariableFieldNames.Length; j++)
             {
                 int indexVl = (i * VariableFieldNames.Length) + j;
                 corr[i, j]  = System.Convert.ToDouble(corrLg[indexVl]);
                 egVec[i, j] = System.Convert.ToDouble(egVecLg[indexVl]);
             }
         }
         sr.Close();
     }
     pca = PrincipalComponentAnalysis.FromCorrelationMatrix(meanVector, stdVector, corr);
     pca.Compute();
 }