Text hashingvectorizer load - CyrilB1531/lodestar GitHub Wiki
Development build. This page describes
main, not a released package. The latest published Lodestar.Text is 0.6.0 — read its documentation.
Home › Text › Vectorization
HashingVectorizer.Load
Read the options back.
public static HashingVectorizer Load(Stream source, ArtifactLoadOptions options = null)
public static HashingVectorizer Load(string path, ArtifactLoadOptions options = null)
Parameters — source is a readable stream, left open; path is a file to read. options
bounds what will be accepted.
Returns — HashingVectorizer, ready to
Transform immediately.
Exceptions — ArgumentNullException for a null source. InvalidDataException when the content
is not a saved vectorizer, holds options the vectorizer refuses — a token pattern no regex parses —
or exceeds a bound in options.
Example — restored, and hashing identically to the original.
using Lodestar.Abstractions;
using Lodestar.Text.Vectorization;
var original = new HashingVectorizer(new HashingVectorizerOptions { NumFeatures = 16 });
using var buffer = new MemoryStream();
original.Save(buffer);
buffer.Position = 0;
HashingVectorizer restored = HashingVectorizer.Load(buffer);
CsrMatrix before = original.Transform(["the cat eats"]);
CsrMatrix after = restored.Transform(["the cat eats"]);
bool sameShape = before.NonZeroCount == after.NonZeroCount; // => True
bool sameLength = before.RowL2Norm(0) == after.RowL2Norm(0); // => True
Remarks — a restored vectorizer produces the same columns as the original, because hashing depends only on the term and the settings. That is the property that makes this type useful across machines: shipping the options is enough, where the other two must ship a vocabulary.
Applies to — net10.0, netstandard2.0.
See also — HashingVectorizer.Save,
ArtifactLoadOptions.