/// <summary> /// Centers the given kernel matrix K. /// </summary> /// /// <param name="kernelMatrix">The kernel matrix to be centered.</param> /// <param name="rowMean">The row-wise mean vector.</param> /// <param name="mean">The total mean (across all values in the matrix).</param> /// <param name="result">The array where to store results.</param> /// public static double[][] Center(double[][] kernelMatrix, double[] rowMean, double mean, double[][] result = null) { result = (result == null) ? Jagged.CreateAs(kernelMatrix) : kernelMatrix; int samples = kernelMatrix.GetLength(0); double[] rowMean1 = kernelMatrix.Mean(1); for (int i = 0; i < rowMean1.Length; i++) { for (int j = 0; j < rowMean.Length; j++) { result[i][j] = kernelMatrix[i][j] - rowMean1[i] - rowMean[j] + mean; } } return(result); }
public void create_as() { double[,] a = { { 1, 2, 3 }, { 3, 4, 5 } }; Array actual = Jagged.CreateAs(a, typeof(int)); Assert.AreEqual(new[] { 2, 3 }, actual.GetLength()); actual = Jagged.CreateAs(a.ToJagged(), typeof(int)); Assert.AreEqual(new[] { 2, 3 }, actual.GetLength()); actual = Matrix.CreateAs(a, typeof(int)); Assert.AreEqual(new[] { 2, 3 }, actual.GetLength()); actual = Matrix.CreateAs(a.ToJagged(), typeof(int)); Assert.AreEqual(new[] { 2, 3 }, actual.GetLength()); }
/// <summary> /// Obsolete /// </summary> /// protected double[][] Adjust(double[][] matrix, bool inPlace) { int rows = matrix.Length; int cols = matrix[0].Length; // Prepare the data, storing it in a new matrix if needed. double[][] result = matrix; if (!inPlace) { result = Jagged.CreateAs(matrix); } matrix.Subtract(columnMeans, dimension: 0, result: result); if (this.analysisMethod == AnalysisMethod.Standardize) { result.Divide(columnStdDev, dimension: 0, result: result); } return(result); }
/// <summary> /// Learns a model that can map the given inputs to the desired outputs. /// </summary> /// <param name="x">The model inputs.</param> /// <param name="weights">The weight of importance for each input sample.</param> /// <returns> /// A model that has learned how to produce suitable outputs /// given the input data <paramref name="x" />. /// </returns> public MultivariateKernelRegression Learn(double[][] x, double[] weights = null) { this.sourceCentered = null; this.StandardDeviations = null; this.featureMean = null; this.featureGrandMean = 0; double[][] K; if (Method == PrincipalComponentMethod.KernelMatrix) { K = x; if (centerFeatureSpace) // Center the Gram (Kernel) Matrix if requested { K = Accord.Statistics.Kernels.Kernel.Center(K, out featureMean, out featureGrandMean); // do not overwrite } } else { this.NumberOfInputs = x.Columns(); this.Means = x.Mean(dimension: 0); this.sourceCentered = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: 0, result: sourceCentered); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); sourceCentered.Divide(StandardDeviations, dimension: 0, result: sourceCentered); } // Create the Gram (Kernel) Matrix K = kernel.ToJagged(x: sourceCentered); if (centerFeatureSpace) // Center the Gram (Kernel) Matrix if requested { K = Accord.Statistics.Kernels.Kernel.Center(K, out featureMean, out featureGrandMean, result: K); // overwrite } } // Perform the Eigenvalue Decomposition (EVD) of the Kernel matrix var evd = new JaggedEigenvalueDecomposition(K, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors int numberOfSamples = x.Length; double[] evals = evd.RealEigenvalues; double[][] eigs = evd.Eigenvectors; int nonzero = evd.Rank; if (NumberOfInputs != 0) { nonzero = Math.Min(nonzero, NumberOfInputs); } if (NumberOfOutputs != 0) { nonzero = Math.Min(nonzero, NumberOfOutputs); } // Eliminate unwanted components eigs = eigs.Get(null, 0, nonzero); evals = evals.Get(0, nonzero); // Normalize eigenvectors if (centerFeatureSpace) { eigs.Divide(evals.Sqrt(), dimension: 0, result: eigs); } if (Whiten) { eigs.Divide(evals.Sqrt(), dimension: 0, result: eigs); } //this.Eigenvalues = evals.Divide(numberOfSamples - 1); this.Eigenvalues = evals; this.SingularValues = evals.Divide(numberOfSamples - 1).Sqrt(); this.ComponentVectors = eigs.Transpose(); if (allowReversion) { // Project the original data into principal component space this.result = Matrix.Dot(K, eigs).ToMatrix(); } // Computes additional information about the analysis and creates the // object-oriented structure to hold the principal components found. CreateComponents(); Accord.Diagnostics.Debug.Assert(NumberOfOutputs > 0); return(CreateRegression()); }
/// <summary> /// Learns a model that can map the given inputs to the desired outputs. /// </summary> /// /// <param name="x">The model inputs.</param> /// <param name="weights">The weight of importance for each input sample.</param> /// /// <returns> /// A model that has learned how to produce suitable outputs /// given the input data <paramref name="x" />. /// </returns> /// public MultivariateLinearRegression Learn(double[][] x, double[] weights = null) { this.NumberOfInputs = x.Columns(); if (Method == PrincipalComponentMethod.Center || Method == PrincipalComponentMethod.Standardize) { this.Means = x.Mean(dimension: 0); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: 0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); matrix.Divide(StandardDeviations, dimension: 0, result: matrix); } // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: false, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; Eigenvalues = SingularValues.Pow(2); Eigenvalues.Divide(x.Rows() - 1, result: Eigenvalues); ComponentVectors = svd.RightSingularVectors.Transpose(); } else if (Method == PrincipalComponentMethod.CovarianceMatrix || Method == PrincipalComponentMethod.CorrelationMatrix) { // We only have the covariance matrix. Compute the Eigenvalue decomposition var evd = new JaggedEigenvalueDecomposition(x, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = evd.Eigenvectors.Transpose(); } else { // The method type should have been validated before we even entered this section throw new InvalidOperationException("Invalid method, this should never happen: {0}".Format(Method)); } if (Whiten) { ComponentVectors.Divide(SingularValues, dimension: 1, result: ComponentVectors); } // Computes additional information about the analysis and creates the // object-oriented structure to hold the principal components found. CreateComponents(); return(CreateRegression()); }
public MultivariateLinearRegression Learn(double[][] x, double[] weights = null) { this.NumberOfInputs = x.Columns(); if (Method == PrincipalComponentMethod.Center || Method == PrincipalComponentMethod.Standardize) { if (weights == null) { this.Means = x.Mean(dimension: 0); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: false, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; Eigenvalues = SingularValues.Pow(2); Eigenvalues.Divide(x.Rows() - 1, result: Eigenvalues); ComponentVectors = svd.RightSingularVectors.Transpose(); } else { this.Means = x.WeightedMean(weights: weights); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.WeightedStandardDeviation(weights, Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } double[,] cov = x.WeightedCovariance(weights, Means); var evd = new EigenvalueDecomposition(cov, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = Jagged.Transpose(evd.Eigenvectors); } } else if (Method == PrincipalComponentMethod.CovarianceMatrix || Method == PrincipalComponentMethod.CorrelationMatrix) { if (weights != null) { throw new Exception(); } var evd = new JaggedEigenvalueDecomposition(x, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = evd.Eigenvectors.Transpose(); } else { throw new InvalidOperationException("Invalid method, this should never happen: {0}".Format(Method)); } if (Whiten) { ComponentVectors.Divide(SingularValues, dimension: (VectorType)1, result: ComponentVectors); } CreateComponents(); return(CreateRegression()); }
/// <summary> /// Computes class-label decisions for the given <paramref name="input" />. /// </summary> /// <param name="input">The input vectors that should be classified as /// any of the <see cref="ITransform.NumberOfOutputs" /> possible classes.</param> /// <returns> /// A set of class-labels that best describe the <paramref name="input" /> /// vectors according to this classifier. /// </returns> public int[][] Decide(TInput[][] input) { return(Decide(input, Jagged.CreateAs <TInput, int>(input))); }