class_docstr: Train and apply a topic model to vectorized texts using scikit-learn's
  implementations of LSA, LDA, and NMF models. Also any other topic model implementations that have
  `component_`, `n_topics` and `transform` attributes. Inspect and visualize results.
  Save and load trained models to and from disk.
  
  Prepare a vectorized corpus (i.e. document-term matrix) and corresponding
  vocabulary (i.e. mapping of term strings to column indices in the matrix).
  See :class:`textacy.representations.vectorizers.Vectorizer` for details. In short:
  
  .. code-block:: pycon
  
      >>> vectorizer = Vectorizer(
      ...     tf_type="linear", idf_type="smooth", norm="l2",
      ...     min_df=3, max_df=0.95, max_n_terms=100000)
      >>> doc_term_matrix = vectorizer.fit_transform(terms_list)
  
  Initialize and train a topic model:
  
  .. code-block:: pycon
  
      >>> model = textacy.tm.TopicModel("nmf", n_topics=20)
      >>> model.fit(doc_term_matrix)
      >>> model
      TopicModel(n_topics=10, model=NMF)
  
  Transform the corpus and interpret our model:
  
  .. code-block:: pycon
  
      >>> doc_topic_matrix = model.transform(doc_term_matrix)
      >>> for topic_idx, top_terms in model.top_topic_terms(vectorizer.id_to_term, topics=[0,1]):
      ...     print("topic", topic_idx, ":", "   ".join(top_terms))
      topic 0 : people   american   go   year   work   think   $   today   money   america
      topic 1 : rescind   quorum   order   unanimous   consent   ask   president   mr.   madam   absence
      >>> for topic_idx, top_docs in model.top_topic_docs(doc_topic_matrix, topics=[0,1], top_n=2):
      ...     print(topic_idx)
      ...     for j in top_docs:
      ...         print(corpus[j]._.meta["title"])
      0
      THE MOST IMPORTANT ISSUES FACING THE AMERICAN PEOPLE
      55TH ANNIVERSARY OF THE BATTLE OF CRETE
      1
      CHEMICAL WEAPONS CONVENTION
      MFN STATUS FOR CHINA
      >>> for doc_idx, topics in model.top_doc_topics(doc_topic_matrix, docs=range(5), top_n=2):
      ...     print(corpus[doc_idx]._.meta["title"], ":", topics)
      JOIN THE SENATE AND PASS A CONTINUING RESOLUTION : (9, 0)
      MEETING THE CHALLENGE : (2, 0)
      DISPOSING OF SENATE AMENDMENT TO H.R. 1643, EXTENSION OF MOST-FAVORED- NATION TREATMENT FOR BULGARIA : (0, 9)
      EXAMINING THE SPEAKER'S UPCOMING TRAVEL SCHEDULE : (0, 9)
      FLOODING IN PENNSYLVANIA : (0, 9)
      >>> for i, val in enumerate(model.topic_weights(doc_topic_matrix)):
      ...     print(i, val)
      0 0.302796022302
      1 0.0635617650602
      2 0.0744927472417
      3 0.0905778808867
      4 0.0521162262192
      5 0.0656303769725
      6 0.0973516532757
      7 0.112907245542
      8 0.0680659204364
      9 0.0725001620636
  
  Visualize the model:
  
  .. code-block:: pycon
  
      >>> model.termite_plot(doc_term_matrix, vectorizer.id_to_term,
      ...                    topics=-1,  n_terms=25, sort_terms_by="seriation")
  
  Persist our topic model to disk:
  
  .. code-block:: pycon
  
      >>> model.save("nmf-10topics.pkl")
  
  Args:
      model: Name or instance of an sklearn decomposition model.
      n_topics: Number of topics in the model to be initialized
      **kwargs:
          variety of parameters used to initialize the model; see individual
          sklearn pages for full details
  
  Raises:
      ValueError: if ``model`` not in ``{"nmf", "lda", "lsa"}`` or is not an
          NMF, LatentDirichletAllocation, or TruncatedSVD instance
  
  See Also:
      * http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.NMF.html
      * http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.LatentDirichletAllocation.html
      * http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html
class_docstr_tok: ["Train","and","apply","a","topic","model","to","vectorized","texts","using","scikit-learn's","implementations","of","LSA",",","LDA",",","and","NMF","models",".","Also","any","other","topic","model","implementations","that","have","`","component_","`",",","`","n_topics","`","and","`","transform","`","attributes",".","Inspect","and","visualize","results",".","Save","and","load","trained","models","to","and","from","disk",".","Prepare","a","vectorized","corpus","(","i.e",".","document-term","matrix",")","and","corresponding","vocabulary","(","i.e",".","mapping","of","term","strings","to","column","indices","in","the","matrix",")",".","See",":","class",":","`","textacy.representations.vectorizers.Vectorizer","`","for","details",".","In","short",":","..","code-block",":",":","pycon",">",">",">","vectorizer","=","Vectorizer","(","...","tf_type=","''","linear","''",",","idf_type=","''","smooth","''",",","norm=","''","l2","''",",","...","min_df=3",",","max_df=0.95",",","max_n_terms=100000",")",">",">",">","doc_term_matrix","=","vectorizer.fit_transform","(","terms_list",")","Initialize","and","train","a","topic","model",":","..","code-block",":",":","pycon",">",">",">","model","=","textacy.tm.TopicModel","(","``","nmf","''",",","n_topics=20",")",">",">",">","model.fit","(","doc_term_matrix",")",">",">",">","model","TopicModel","(","n_topics=10",",","model=NMF",")","Transform","the","corpus","and","interpret","our","model",":","..","code-block",":",":","pycon",">",">",">","doc_topic_matrix","=","model.transform","(","doc_term_matrix",")",">",">",">","for","topic_idx",",","top_terms","in","model.top_topic_terms","(","vectorizer.id_to_term",",","topics=","[","0,1","]",")",":","...","print","(","``","topic","''",",","topic_idx",",","``",":","''",",","``","``",".join","(","top_terms",")",")","topic","0",":","people","american","go","year","work","think","$","today","money","america","topic","1",":","rescind","quorum","order","unanimous","consent","ask","president","mr.","madam","absence",">",">",">","for","topic_idx",",","top_docs","in","model.top_topic_docs","(","doc_topic_matrix",",","topics=","[","0,1","]",",","top_n=2",")",":","...","print","(","topic_idx",")","...","for","j","in","top_docs",":","...","print","(","corpus","[","j","]","._.meta","[","``","title","''","]",")","0","THE","MOST","IMPORTANT","ISSUES","FACING","THE","AMERICAN","PEOPLE","55TH","ANNIVERSARY","OF","THE","BATTLE","OF","CRETE","1","CHEMICAL","WEAPONS","CONVENTION","MFN","STATUS","FOR","CHINA",">",">",">","for","doc_idx",",","topics","in","model.top_doc_topics","(","doc_topic_matrix",",","docs=range","(","5",")",",","top_n=2",")",":","...","print","(","corpus","[","doc_idx","]","._.meta","[","``","title","''","]",",","``",":","''",",","topics",")","JOIN","THE","SENATE","AND","PASS","A","CONTINUING","RESOLUTION",":","(","9",",","0",")","MEETING","THE","CHALLENGE",":","(","2",",","0",")","DISPOSING","OF","SENATE","AMENDMENT","TO","H.R",".","1643",",","EXTENSION","OF","MOST-FAVORED-","NATION","TREATMENT","FOR","BULGARIA",":","(","0",",","9",")","EXAMINING","THE","SPEAKER","'S","UPCOMING","TRAVEL","SCHEDULE",":","(","0",",","9",")","FLOODING","IN","PENNSYLVANIA",":","(","0",",","9",")",">",">",">","for","i",",","val","in","enumerate","(","model.topic_weights","(","doc_topic_matrix",")",")",":","...","print","(","i",",","val",")","0","0.302796022302","1","0.0635617650602","2","0.0744927472417","3","0.0905778808867","4","0.0521162262192","5","0.0656303769725","6","0.0973516532757","7","0.112907245542","8","0.0680659204364","9","0.0725001620636","Visualize","the","model",":","..","code-block",":",":","pycon",">",">",">","model.termite_plot","(","doc_term_matrix",",","vectorizer.id_to_term",",","...","topics=-1",",","n_terms=25",",","sort_terms_by=","''","seriation","''",")","Persist","our","topic","model","to","disk",":","..","code-block",":",":","pycon",">",">",">","model.save","(","``","nmf-10topics.pkl","''",")","Args",":","model",":","Name","or","instance","of","an","sklearn","decomposition","model",".","n_topics",":","Number","of","topics","in","the","model","to","be","initialized","*","*","kwargs",":","variety","of","parameters","used","to","initialize","the","model",";","see","individual","sklearn","pages","for","full","details","Raises",":","ValueError",":","if","``","model","``","not","in","``","{","``","nmf","''",",","``","lda","''",",","``","lsa","''","}","``","or","is","not","an","NMF",",","LatentDirichletAllocation",",","or","TruncatedSVD","instance","See","Also",":","*","http",":","\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.NMF.html","*","http",":","\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.LatentDirichletAllocation.html","*","http",":","\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.decomposition.TruncatedSVD.html"]
code: def save(self, filepath: types.PathLike):
      _ = joblib.dump(self.model, filepath, compress=3)
      LOGGER.info("%s model saved to %s", self.model, filepath)
  
imports: from __future__ import annotations
  import logging
  from typing import ClassVar, Iterable, Literal, Optional, Sequence
  import joblib
  import numpy
  import scipy.sparse
  from sklearn.decomposition import NMF, LatentDirichletAllocation, TruncatedSVD
  from ..None import errors, types, viz
label_desc: Use image node_id 4 for calling the TopicModel obj's underlying member method code with example usage: obj.save(filepath) without return types