A simple Cosine similarity

  • Vectors contains one column, m rows : each row contains an array of n floats;
  • I want to have a m x n matrix, transpose it and do there product
  • I’m using C#

Also, I read this from official documentation https://numerics.mathdotnet.com/Matrix.html

"For example, if you have a collection of vectors, consider to store them in a list or array of vectors, not in a matrix (unless you need matrix operations, of course)."

So please what’s the optimized way to achieve this with Math.net.numerics? Thanks in advance

#UPDATE

I’m getting this Error

CS1503  Argument 3: cannot convert from'System.Collections.Generic.IEnumerable<float[]>' to 'float[]' 

With this code :

IEnumerable<float[]> Vectors = predictions.GetColumn<float[]>("Features"); var x = new M.DenseMatrix(1, Vectors.Count(), Vectors); 
Add Comment
1 Answer(s)

Sample method to do the cross-product using MathNet.Numerics.LinearAlgebra.Generic.Vector of a 2 element vector.

Vector<float> v1 = new DenseVector(new float[] { 1, 2, 3 });      Vector<float> v2 = new DenseVector(new float[] { 3, 2, 1 });  Vector<float> result = v1.CrossProduct(v2); Vector<float> result = Vector.CrossProduct(v1,v2); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.