Abstractions csrmatrix rowl1norm - CyrilB1531/lodestar GitHub Wiki
Home › Abstractions › The shared sparse primitive
CsrMatrix.RowL1Norm
The sum of one row's absolute values — its total mass.
public double RowL1Norm(int row)
Parameters — row is the zero-based row index.
Returns — double, the sum of |value| over 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 — on raw counts, the L1 norm is how many terms the document holds.
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.RowL1Norm(0); // => 3
double third = counts.RowL1Norm(2); // => 5
Remarks — absolute values, which matters only where a matrix can hold negatives:
HashingVectorizer with AlternateSign on produces them by design, and
there the L1 norm is the total weight rather than a net one.
Dividing a row by this norm turns it into a distribution over terms, which is what
NormalizeRows(SparseNorm.L1) does.
Applies to — net10.0, netstandard2.0.
See also — CsrMatrix.RowL2Norm,
CsrMatrix.NormalizeRows, SparseNorm.