TfidfVectorizer JavaScript Implementation for Browser and Node.js
Build sparse TF-IDF text features with a scikit-learn-style JavaScript and TypeScript API using @kanaries/ml in browser or Node.js.
View as MarkdownTfidfVectorizer
Algorithm overview
TfidfVectorizer combines vocabulary learning, count vectorization, inverse-document-frequency weighting, and row normalization. Use it for document classification, similarity, and lightweight semantic search baselines.
JavaScript implementation
The @kanaries/ml implementation takes raw string[], returns a CSRMatrix, works in Pipeline, and feeds sparse-aware MultinomialNB without converting the entire corpus to dense arrays.
Quick start example
import { Bayes, FeatureExtraction, Pipeline } from '@kanaries/ml';
const pipeline = new Pipeline({ steps: [
['tfidf', new FeatureExtraction.TfidfVectorizer({ ngramRange: [1, 2] })],
['nb', new Bayes.MultinomialNB()],
] });
pipeline.fit(['red apple sweet', 'blue sea deep'], [0, 1]);
console.log(pipeline.predict(['deep blue ocean']));The equivalent Python workflow uses Pipeline([('tfidf', TfidfVectorizer()), ('nb', MultinomialNB())]).
Detailed API reference
The constructor accepts every CountVectorizer option plus every TfidfTransformer option.
fit(documents: string[]): voidtransform(documents: string[]): CSRMatrixfitTransform(documents: string[]): CSRMatrixgetFeatureNamesOut(): string[]vocabulary: ReadonlyMap<string, number>idf: number[]
Serialize a fitted instance with JSON.stringify(vectorizer) and restore it with the root loadModel export.
TfidfTransformer
Weight dense or CSR term-count matrices with TF-IDF in browser or Node.js using the @kanaries/ml JavaScript implementation.
Feature Hashing and Dictionary Vectorization
Use HashingVectorizer, DictVectorizer, and FeatureHasher for bounded-memory JavaScript and TypeScript feature extraction.