//--------------------------------------------- #region Protected Methods /// <summary> /// Adjusts a data matrix, centering and standardizing its values /// using the already computed column's means and standard deviations. /// </summary> /// protected internal double[,] Adjust(double[,] matrix, bool inPlace) { // Center the data around the mean. Will have no effect if // the data is already centered (the mean will be zero). double[,] result = matrix.Center(columnMeans, inPlace); // Check if we also have to standardize our data (convert to Z Scores). if (this.analysisMethod == AnalysisMethod.Standardize) { for (int j = 0; j < columnStdDev.Length; j++) { if (columnStdDev[j] == 0) { columnStdDev[j] = 1; //manual modifed 2m0rr0w2 //throw new ArithmeticException("Standard deviation cannot be" + //" zero (cannot standardize the constant variable at column index " + j + ")."); } } // Yes. Divide by standard deviation result.Standardize(columnStdDev, true); } return(result); }
public static double[,] Covariance(this double[,] matrix) { double[,] covMatrix = new double[matrix.RowCount(), matrix.ColumnCount()]; matrix = matrix.Center(); var covarianceMatrix = Multiply(matrix.Transpose(), matrix); return(Divide(covarianceMatrix, matrix.RowCount() - 1)); }
/// <summary> /// Applies the translation operator to translate the dataset to the given coordinate /// </summary> /// /// <param name="samples">Dataset to translate</param> /// <param name="centroid">New center of the dataset</param> /// /// <returns>The translated dataset</returns> /// double[,] Translate(double[,] samples, double[] centroid) { if (centroid.Length != samples.GetLength(1)) { throw new ArgumentException("Centroid length should be the same length of the samples columns !"); } double[,] res = new double[samples.GetLength(0), samples.GetLength(1)]; // Calculate the matrix mean and translate it according to the required centroid double[] translated_avg = samples.Mean(0).Subtract(centroid); // Translate the sample data to the new centroid res = samples.Center(translated_avg); return(res); }
protected internal double[,] Adjust(double[,] matrix, bool inPlace) { if (Means == null || Means.Length == 0) { Means = matrix.Mean(dimension: 0); StandardDeviations = matrix.StandardDeviation(Means); } double[,] result = matrix.Center(Means, inPlace); if (this.Method == PrincipalComponentMethod.Standardize || this.Method == PrincipalComponentMethod.CorrelationMatrix) { result.Standardize(StandardDeviations, true); } return(result); }
/// <summary> /// Transforms the dataset to match the given reference original dataset /// </summary> /// <param name="p_reference">Dataset to match</param> /// <returns>The transformed dataset matched to the reference</returns> public double[,] Transform(ProcrustedDataset p_reference) { // Make a copy of the current Procrustes dataset double[,] tData = (double[, ])Dataset.Clone(); // Rotate the dataset to match the reference dataset rotation tData = tData.Dot(p_reference.RotationMatrix.Transpose()); // Scale the dataset to match the reference scale tData = tData.Multiply(p_reference.Scale); // Prepare a negative translation vector to... double[] refCenter = p_reference.Center.Multiply(-1); // ... move the dataset to the same center as the reference tData = tData.Center(refCenter); return(tData); }
protected internal double[,] Adjust(double[,] matrix, bool inPlace) { if (Means == null || Means.Length == 0) { Means = matrix.Mean(dimension: 0); StandardDeviations = matrix.StandardDeviation(Means); } // Center the data around the mean. Will have no effect if // the data is already centered (the mean will be zero). double[,] result = matrix.Center(Means, inPlace); // Check if we also have to standardize our data (convert to Z Scores). if (this.Method == PrincipalComponentMethod.Standardize || this.Method == PrincipalComponentMethod.CorrelationMatrix) { // Yes. Divide by standard deviation result.Standardize(StandardDeviations, true); } return(result); }