hypertools.tools.text2mat¶
- hypertools.tools.text2mat(data, vectorizer='CountVectorizer', semantic='LatentDirichletAllocation', corpus='wiki')[source]¶
Turns a list of text samples into a matrix using a vectorizer and a text model
- Parameters:
- datalist (or list of lists) of text samples
The text data to transform
- vectorizerstr, dict, class or class instance
The vectorizer to use. A string name is resolved in three tiers (GH #198): (1) the built-in scikit-learn registry – ‘CountVectorizer’ or ‘TfidfVectorizer’; (2) if not found there, the gensim wrapper registry (hypertools.tools.gensim_models; the [gensim] extra installs itself on first use) – ‘Word2Vec’, ‘Doc2Vec’, or ‘FastText’; (3) if still not found, the name is treated as a Hugging Face sentence-transformers model name/id (e.g. ‘all-MiniLM-L6-v2’), via data-wrangler’s HF embedding support. To change default parameters, set to a dictionary e.g. {‘model’ : ‘CountVectorizer’, ‘kwargs’ : {‘max_features’ : 10}} (the legacy {‘model’, ‘params’} form is also still accepted; positional constructor arguments go under ‘args’, and any other top-level key – e.g. a flat {‘model’: ‘CountVectorizer’, ‘max_features’: 10} – raises ValueError naming it). See https://scikit-learn.org/stable/api/sklearn.feature_extraction.html for scikit-learn details. You can also specify your own vectorizer model as a class, or class instance. With either option, the class must have a fit_transform method (see https://scikit-learn.org/stable/data_transforms.html). To set parameters, use the dict form (or a configured class instance); a bare class is instantiated with its defaults.
- semanticstr, dict, class or class instance
Text model to use to transform text data. Built-in options are ‘LatentDirichletAllocation’ or ‘NMF’ (default: LDA for count vectorizers; for embedding vectorizers – gensim ‘Word2Vec’/ ‘Doc2Vec’/’FastText’, or a Hugging Face sentence-transformers model – the semantic stage defaults to none and corpus is unused, since a topic model cannot consume continuous embeddings; a gensim vectorizer warns when it skips the stage, a Hugging Face one skips it silently, and an explicit ‘NMF’ with a Hugging Face vectorizer raises ValueError). Pass None to skip the semantic stage explicitly. String names are resolved using the same three-tier order as vectorizer (sklearn -> gensim -> HuggingFace, GH #198); the gensim tier adds ‘LdaModel’, ‘LsiModel’, and ‘HdpModel’. To change default parameters, set to a dictionary e.g. {‘model’ : ‘NMF’, ‘kwargs’ : {‘n_components’ : 10}} (the legacy {‘model’, ‘params’} form is also still accepted; as for vectorizer, a top-level key other than ‘model’/’args’/’kwargs’ raises ValueError). See https://scikit-learn.org/stable/api/sklearn.decomposition.html for details on the two scikit-learn model options. You can also specify your own text model as a class, or class instance. With either option, the class must have a fit_transform method (see https://scikit-learn.org/stable/data_transforms.html). To set parameters, use the dict form (or a configured class instance); a bare class is instantiated with its defaults.
- corpuslist (or list of lists) of text samples or ‘wiki’, ‘nips’, ‘sotus’.
Text to use to fit the semantic model (optional). If set to ‘wiki’, ‘nips’ or ‘sotus’ and the default semantic and vectorizer models are used, a pretrained model will be loaded which can save a lot of time. Unused (never loaded or embedded) when the vectorizer is a pretrained Hugging Face embedding model and there is no semantic stage.
- Returns:
- transformed datalist of numpy arrays
The transformed text data