Abstractions csrmatrix rowl2norm - CyrilB1531/lodestar GitHub Wiki
Home › Abstractions › The shared sparse primitive
CsrMatrix.RowL2Norm
The Euclidean length of one row.
public double RowL2Norm(int row)
Parameters — row is the zero-based row index.
Returns — double, the square root of the sum of the squares of that row's stored cells.
Zero for an empty row.
Exceptions — IndexOutOfRangeException when row is negative or not below
RowCount. The index reaches the backing array directly, so the array's own exception
is what surfaces rather than a re-wrapped one.
Example — three ones give √3; the third row's repeated the makes it longer.
using Lodestar.Abstractions;
using Lodestar.Text.Vectorization;
string[] docs = ["the cat eats", "the dog eats", "the cat and the dog"];
CsrMatrix counts = new CountVectorizer().FitTransform(docs);
double first = counts.RowL2Norm(0); // => 1.7320508075688772
double third = counts.RowL2Norm(2); // => 2.6457513110645907
Remarks — this is the norm that matters for similarity. Two rows divided by their L2 norms
have a dot product equal to their cosine similarity, which is why
TfidfOptions normalizes by it and why
NormalizeRows(SparseNorm.L2) is the usual call before comparing
documents.
A matrix straight out of TfidfVectorizer is already L2-normalized, so
every row's norm is 1 and calling this on one is a way to confirm that rather than to learn
something new.
Applies to — net10.0, netstandard2.0.
See also — CsrMatrix.RowL1Norm,
CsrMatrix.NormalizeRows, SparseNorm.