Skip to content

Text csrmatrix rowl2norm

github-actions[bot] edited this page Aug 26, 2026 · 24 revisions

Development build. This page describes main, not a released package. The latest published Lodestar.Text is 0.4.0 — read its documentation.

CsrMatrix.RowL2Norm

The Euclidean length of one row.

public double RowL2Norm(int row)

Parametersrow is the zero-based row index.

Returnsdouble, the square root of the sum of the squares of that row's stored cells. Zero for an empty row.

ExceptionsIndexOutOfRangeException 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.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 alsoCsrMatrix.RowL1Norm, CsrMatrix.NormalizeRows, SparseNorm.

Lodestar

Project

Clone this wiki locally