Skip to main content
Question 2. Which Python package is most appropriate for creating dense vector representations of text for a semantic search application? Would it be NumPy, spaCy, matplotlib, or scikit-learn? Answer: spaCy spaCy is a production-ready natural language processing library that includes easy access to pre-trained dense word and document vectors when you use models that ship with vectors (for example, en_core_web_lg). While:
  • NumPy is the core numerical library used to manipulate vectors (arrays)
  • matplotlib is for visualization
  • scikit-learn offers ML utilities and some vectorizers (e.g., TF-IDF)
spaCy directly provides token and document .vector embeddings that are convenient for semantic search workflows.
Use a spaCy model that includes vectors (for example, en_core_web_lg) when you need ready-made dense embeddings. For higher-quality sentence or paragraph embeddings, consider transformer-based libraries such as sentence-transformers (SBERT) or Hugging Face models, and combine those embeddings with spaCy or your retrieval pipeline as needed.

Quick comparison

Install and load a spaCy model with vectors

Shell:
Python example:
Notes:
  • spaCy’s .vector on a Doc or Token returns a dense numpy.ndarray that you can use directly for similarity computations (cosine similarity), indexing into vector stores, or as input to downstream models.
  • For improved semantic-search accuracy at sentence/paragraph level, consider transformer-based encoders such as sentence-transformers or Hugging Face embedding models; these typically yield higher-quality embeddings than classic spaCy vectors for sentence semantics.
  • Use sklearn.metrics.pairwise.cosine_similarity or scipy.spatial.distance.cosine (or faiss/annoy for large-scale search) to compute nearest neighbors efficiently.

References and further reading

Watch Video