Text Feature Extraction
Convert raw text into sparse count and TF-IDF matrices in JavaScript or TypeScript for browser and Node.js machine-learning pipelines.
View as MarkdownModule overview
Text estimators need numeric features, but materializing a dense document-by-vocabulary matrix wastes memory. The FeatureExtraction module tokenizes strings and returns CSRMatrix sparse matrices that can flow through Pipeline and sparse-aware Naive Bayes estimators.
JavaScript implementation
@kanaries/ml brings the familiar scikit-learn text workflow to browser and Node.js code: use CountVectorizer for token counts, TfidfTransformer for weighting existing count matrices, or TfidfVectorizer for both steps together.
Quick start
import { Bayes, FeatureExtraction, Pipeline } from '@kanaries/ml';
const model = new Pipeline({ steps: [
['tfidf', new FeatureExtraction.TfidfVectorizer()],
['classifier', new Bayes.MultinomialNB()],
] });
model.fit(['red apple sweet', 'blue ocean deep'], [0, 1]);
model.predict(['sweet apple']);Choose an API
- CountVectorizer: sparse token and n-gram counts.
- TfidfTransformer: TF-IDF weighting for an existing count matrix.
- TfidfVectorizer: one-step text-to-TF-IDF conversion.
- Hashing and Dictionary Features: bounded-memory hashing and explicit dictionary expansion.
Univariate Feature Scores
Rank classification and regression features with chi-square, ANOVA F, and k-nearest-neighbor mutual information JavaScript functions from @kanaries/ml.
CountVectorizer
Tokenize browser or Node.js text into a memory-efficient CSR count matrix with the @kanaries/ml JavaScript CountVectorizer implementation.