Text countvectorizer load - CyrilB1531/lodestar GitHub Wiki

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

Home › Text › Vectorization

CountVectorizer.Load

Read a fitted vectorizer back.

public static CountVectorizer Load(Stream source, ArtifactLoadOptions options = null)
public static CountVectorizer 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 — sizes, counts and depths — and defaults to ArtifactLoadOptions's own defaults.

Returns — CountVectorizer, fitted and ready to Transform.

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 — restoring, and counting with the vocabulary that was saved.

using Lodestar.Text.Vectorization;

var original = new CountVectorizer();
original.Fit(["the cat eats", "the dog eats"]);

using var buffer = new MemoryStream();
original.Save(buffer);
buffer.Position = 0;

CountVectorizer restored = CountVectorizer.Load(buffer);
IReadOnlyList<string> names = restored.GetFeatureNames();

string first = names[0];  // => cat

Remarks — options is the reason this is not a one-line deserialization. A saved vectorizer is a file, a file can come from anywhere, and a vocabulary declaring a hundred million entries would otherwise be allocated before anything noticed. The bounds are refused rather than truncated, so a file that exceeds one is an error rather than a quietly smaller model. An artifact past one array, about 2 GB, which a raised MaxTotalBytes lets through, is read in segments from any stream rather than refused, so whatever a save wrote, a load reads back (#1618).

The vocabulary comes back in the order it was saved, so a matrix produced after loading has the same column meanings as one produced before.

Applies to — net10.0, netstandard2.0.

See also — CountVectorizer.Save, CountVectorizer.LoadAsync, ArtifactLoadOptions.

⚠️ **GitHub.com Fallback** ⚠️