public void large_transform_few_components() { int n = 100; double[][] data = Jagged.Random(n, n); int[] labels = Vector.Random(n, 0, 10); var kda = new KernelDiscriminantAnalysis(); var target = kda.Learn(data, labels); var expected = kda.Transform(data, 2); Assert.AreEqual(n, expected.Rows()); Assert.AreEqual(2, expected.Columns()); kda.NumberOfOutputs = 2; target = kda.Learn(data, labels); var actual = target.First.Transform(data); Assert.AreEqual(n, actual.Rows()); Assert.AreEqual(2, actual.Columns()); Assert.IsTrue(actual.IsEqual(expected)); }
/// <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) { if (weights != null) { throw new ArgumentException(Accord.Properties.Resources.NotSupportedWeights, "weights"); } // Calculate common measures to speedup other calculations this.columnMeans = Measures.Mean(x, dimension: 0); this.columnStdDev = Measures.StandardDeviation(x, columnMeans); NumberOfInputs = x.Columns(); if (NumberOfOutputs == 0) { NumberOfOutputs = NumberOfInputs; } // First, the data should be centered by subtracting // the mean of each column in the source data matrix. var matrix = Adjust(x, overwriteSourceMatrix); // Pre-process the centered data matrix to have unit variance var whiten = Statistics.Tools.Whitening(matrix, out whiteningMatrix); // Generate a new unitary initial guess for the de-mixing matrix var initial = Jagged.Random(NumberOfOutputs, matrix.Columns()); // Compute the demixing matrix using the selected algorithm if (algorithm == IndependentComponentAlgorithm.Deflation) { revertMatrix = deflation(whiten, NumberOfOutputs, initial); } else // if (algorithm == IndependentComponentAlgorithm.Parallel) { revertMatrix = parallel(whiten, NumberOfOutputs, initial); } // Combine the rotation and demixing matrices revertMatrix = whiteningMatrix.DotWithTransposed(revertMatrix); revertMatrix.Divide(revertMatrix.Sum(), result: revertMatrix); // Compute the original source mixing matrix mixingMatrix = Matrix.PseudoInverse(revertMatrix); mixingMatrix.Divide(mixingMatrix.Sum(), result: mixingMatrix); this.demix = createRegression(revertMatrix, columnMeans, columnStdDev, analysisMethod); this.mix = demix.Inverse(); // Creates the object-oriented structure to hold the principal components var array = new IndependentComponent[NumberOfOutputs]; for (int i = 0; i < array.Length; i++) { array[i] = new IndependentComponent(this, i); } this.componentCollection = new IndependentComponentCollection(array); return(demix); }
public void DotWithTransposeTest_Jagged() { double[][] a = Jagged.Random(5, 3); double[][] b = Jagged.Random(2, 3); double[][] expected = Matrix.Dot(a, b.Transpose()); double[][] actual = Matrix.DotWithTransposed(a, b); Assert.IsTrue(expected.IsEqual(actual)); }
public void Compute(int components) { NumberOfOutputs = components; // First, the data should be centered by subtracting // the mean of each column in the source data matrix. double[][] matrix = Adjust(sourceMatrix.ToJagged(), overwriteSourceMatrix); // Pre-process the centered data matrix to have unit variance double[][] whiten = Statistics.Tools.Whitening(matrix, out whiteningMatrix); // Generate a new unitary initial guess for the de-mixing matrix double[][] initial = Jagged.Random(components, matrix.Columns()); // Compute the demixing matrix using the selected algorithm if (algorithm == IndependentComponentAlgorithm.Deflation) { revertMatrix = deflation(whiten, components, initial); } else // if (algorithm == IndependentComponentAlgorithm.Parallel) { revertMatrix = parallel(whiten, components, initial); } // Combine the rotation and demixing matrices revertMatrix = whiteningMatrix.DotWithTransposed(revertMatrix); revertMatrix.Divide(revertMatrix.Sum(), result: revertMatrix); // Compute the original source mixing matrix mixingMatrix = Matrix.PseudoInverse(revertMatrix); mixingMatrix.Divide(mixingMatrix.Sum(), result: mixingMatrix); // Demix the data into independent components resultMatrix = Matrix.Dot(matrix, revertMatrix).ToMatrix(); this.demix = createRegression(revertMatrix, columnMeans, columnStdDev, analysisMethod); this.mix = demix.Inverse(); // Creates the object-oriented structure to hold the principal components var array = new IndependentComponent[components]; for (int i = 0; i < array.Length; i++) { array[i] = new IndependentComponent(this, i); } this.componentCollection = new IndependentComponentCollection(array); }
/// <summary> /// Constructs a new Hidden Markov Model. /// </summary> /// /// <param name="topology"> /// A <see cref="Topology"/> object specifying the initial values of the matrix of transition /// probabilities <c>A</c> and initial state probabilities <c>pi</c> to be used by this model. /// </param> /// <param name="symbols">The number of output symbols used for this model.</param> /// <param name="random">Whether to initialize emissions with random probabilities /// or uniformly with <c>1 / number of symbols</c>. Default is false (default is /// to use <c>1/symbols</c>).</param> /// public HiddenMarkovModel(ITopology topology, int symbols, bool random) : base(topology) { this.symbols = symbols; double[][] b; if (random) { b = Jagged.Random(States, symbols); } else { b = Jagged.Ones(States, symbols); } base.Emissions = GeneralDiscreteDistribution.FromMatrix(b.Log(), true); }
public void DotWithTransposeBatchTest_Jagged2() { double Tolerance = 1e-12; for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { for (int k = 1; k < 10; k++) { double[,] a = Matrix.Random(i, k); double[][] b = Jagged.Random(j, k); double[][] actual = Jagged.Random(i, j); double[][] expected = a.Dot(b.Transpose()); a.DotWithTransposed(b, actual); Assert.IsTrue(expected.IsEqual(actual, Tolerance)); } } } }
public void learn_test() { Accord.Math.Random.Generator.Seed = 0; #region doc_learn // Let's create a random dataset containing // 5000 samples of two dimensional samples. // double[][] source = Jagged.Random(5000, 2); // Now, we will mix the samples the dimensions of the samples. // A small amount of the second column will be applied to the // first, and vice-versa. // double[][] mix = { new double[] { 0.25, 0.25 }, new double[] { -0.25, 0.75 }, }; // mix the source data double[][] input = source.Dot(mix); // Now, we can use ICA to identify any linear mixing between the variables, such // as the matrix multiplication we did above. After it has identified it, we will // be able to revert the process, retrieving our original samples again // Create a new Independent Component Analysis var ica = new IndependentComponentAnalysis() { Algorithm = IndependentComponentAlgorithm.Parallel, Contrast = new Logcosh() }; // Learn the demixing transformation from the data MultivariateLinearRegression demix = ica.Learn(input); // Now, we can retrieve the mixing and demixing matrices that were // used to alter the data. Note that the analysis was able to detect // this information automatically: double[][] mixingMatrix = ica.MixingMatrix; // same as the 'mix' matrix double[][] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix // We can use the regression to recover the separate sources double[][] result = demix.Transform(input); #endregion // Verify mixing matrix mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum()); Assert.IsTrue(mix.IsEqual(mixingMatrix, atol: 0.008)); Assert.IsTrue(revertMatrix.IsEqual(demix.Weights, atol: 0.008)); var dm = demix.Inverse().Weights; dm = dm.Divide(dm.Sum()); Assert.IsTrue(mixingMatrix.IsEqual(dm, atol: 0.008)); // Verify demixing matrix double[,] expected = { { 0.75, -0.25 }, { 0.25, 0.25 }, }; Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm); Assert.AreEqual(ica.Contrast.GetType(), typeof(Logcosh)); revertMatrix = revertMatrix.Divide(revertMatrix.Sum()); Assert.IsTrue(expected.IsEqual(revertMatrix, atol: 0.008)); }