簡體   English   中英

一個簡單的余弦相似度

[英]A simple Cosine similarity

  • Vectors 包含一列,m 行:每行包含 n 個浮點數的數組;
  • 我想要 amxn 矩陣,轉置它並在那里做產品
  • 我正在使用 C#

另外,我從官方文檔https://numerics.mathdotnet.com/Matrix.html中讀到了這個

“例如,如果您有一組向量,請考慮將它們存儲在向量列表或數組中,而不是矩陣中(當然,除非您需要矩陣運算)。”

那么請用 Math.net.numerics 實現這一目標的優化方法是什么? 提前致謝

#更新

我收到此錯誤

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

使用此代碼:

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

解決了

var Matrix = M.SparseMatrix.OfColumnArrays(Vectors);

細節:

public Matrix<T> SparseOfColumnArrays(IEnumerable<T[]> columns);

期望具有 T=float 的相同類型的向量,所以它起作用了。 我建議您閱讀 IDE 中的庫方法。 2-由於矩陣不密集,我切換到稀疏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM