python
18 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
import numpy as np
embeddings = np.array([
[0.9, 0.1, 0.2],
[0.1, 0.8, 0.3],
[0.7, 0.2, 0.4],
])
query = np.array([0.8, 0.2, 0.1])
normalized_embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
normalized_query = query / np.linalg.norm(query)
cosine_similarity = normalized_embeddings @ normalized_query
ranked_indices = np.argsort(cosine_similarity)[::-1]
print(cosine_similarity)
print(ranked_indices)
1 file · python
Explain with highlit
A lot of machine learning reduces to linear algebra with better tooling. Dot products, norms, matrix multiplication, and projections show up in recommendation, embeddings, PCA, and optimization. I keep the implementation small and testable so it stays understandable when somebody revisits it months later.
Related snips
python
from gensim.models import Word2Vec
sentences = [
['customer', 'refund', 'payment', 'issue'],
['login', 'authentication', 'password', 'reset'],
['delivery', 'shipment', 'tracking', 'delay'],
Word embeddings with gensim for semantic similarity tasks
word-embeddings
gensim
nlp
by Dr. Elena Vasquez
1 tab
python
import numpy as np
features = np.array([
[120.0, 3.0, 10.0],
[90.0, 5.0, 7.0],
[150.0, 2.0, 14.0],
NumPy broadcasting for vectorized feature engineering
numpy
broadcasting
vectorization
by Dr. Elena Vasquez
1 tab
Share this code
Here's the card — post it anywhere.