Intermediate·24 min·0/52 done

NumPy arrays and vectors

Create arrays, check shape and dtype, then compute cosine similarity.

Why it matters: Embeddings are vectors. Batches are matrices. Similarity search is a dot product of normalized vectors.

Basics

Arrays, shape, and dtype

A NumPy array is a typed block of numbers. Operations run over the whole array at once.

Check these first when debugging:

  • shape - sizes of each axis, e.g. (32, 768)
  • dtype - usually float32 in ML code

Common mistake: mixing dtypes and silently doubling memory.

Tiny example

Make a small vector and print shape and dtype.

import numpy as np
a = np.array([0.1, 0.9, 0.3], dtype=np.float32)
print(a.shape, a.dtype)

Basics

Vectorization and broadcasting

Vectorization means writing a * 2 + b instead of a Python loop.

Broadcasting lets arrays of compatible shapes combine. A (768,) mean can subtract from a (32, 768) batch.

Shapes must match from the right: equal size, or one side is 1.

Tiny example

Subtract the column mean from a tiny batch.

import numpy as np
batch = np.array([[1.0, 2.0], [3.0, 4.0]])
print(batch - batch.mean(axis=0))

Basics

Cosine similarity

Cosine similarity is the dot product of two unit vectors.

Normalize each vector (divide by its L2 norm), then multiply with @.

Values near 1 mean similar direction. This is the core of vector search.

Tiny example

Normalize two vectors, then take the dot product.

import numpy as np
a = np.array([1.0, 0.0])
b = np.array([0.8, 0.2])
a_n = a / np.linalg.norm(a)
b_n = b / np.linalg.norm(b)
print(float(a_n @ b_n))

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: center a batch of embeddings

One broadcast subtracts the mean from every row.

In AI / ML: Embedding batches are matrices; mean vectors broadcast across rows.

example.py

Python

import numpy as np

embeddings = np.array([[1.0, 2.0, 3.0], [2.0, 4.0, 6.0]], dtype=np.float32)
mean_vec = embeddings.mean(axis=0)
centered = embeddings - mean_vec
print(mean_vec)
print(centered)

AI / ML example 2

AI / ML: rank docs by cosine score

Normalize, then score a query against a small doc matrix.

In AI / ML: Semantic search ranks documents by cosine similarity to a query vector.

example.py

Python

import numpy as np

def normalize(m):
    norms = np.linalg.norm(m, axis=1, keepdims=True)
    return m / np.clip(norms, 1e-9, None)

docs = np.array([[0.9, 0.1], [0.1, 0.9], [0.8, 0.2]], dtype=np.float32)
labels = ["python", "k8s", "tools"]
query = np.array([[0.85, 0.15]], dtype=np.float32)
scores = (normalize(query) @ normalize(docs).T)[0]
for i in np.argsort(-scores):
    print(labels[i], round(float(scores[i]), 3))

Your turn · answer checked automatically

Exercise

With NumPy and no loops, set scores to the cosine similarity between query and every row of docs. Then set best to the label of the highest score.

Edit the code, then press Run & check. Passing it marks this lesson done.

python console

Exercise

# packages: numpy

~/lesson.py

Ctrl+Enter

console output

ready

$ waiting for Run...

Takeaways

  • 1Check .shape and .dtype first when debugging arrays.
  • 2Prefer vectorized ops; broadcasting applies a small array across a large one.
  • 3Cosine similarity on normalized vectors powers semantic search.