Lesson · ⏱ 8 min read

📐 Vectors and matrices: the language of AI

EnglishHinglish

🏠 Real-life example

Describe a phone with numbers: [price, battery, camera MP, weight] = [20000, 5000, 50, 190]. That list is a vector, and two phones with similar vectors are similar phones. A spreadsheet of 100 phones, each row a vector, is a matrix. AI models are mostly huge amounts of fast vector and matrix maths.

💡 In simple words

A vector is an ordered list of numbers that represents something: a data row, an image or the meaning of a word. A matrix is a grid of numbers, i.e. many vectors stacked together. Key operations: the dot product measures how aligned two vectors are (the basis of similarity search), and matrix multiplication transforms vectors (what every neural-network layer does). Eigenvectors describe the main directions of variation in data. You don't need to compute these by hand; NumPy and PyTorch do it. But knowing what they mean makes papers and models readable.

🪜 Step by step

  1. A vector is a list of numbers: a house as [3 bedrooms, 1200 sq ft, 8 km from centre].
  2. A matrix is a table of numbers: many houses, one row each.
  3. The dot product multiplies matching numbers and adds them. A model's prediction is often a dot product of features and weights.
  4. Matrix multiplication does many dot products at once. GPUs are fast at exactly this, which is why they power AI.
  5. Cosine similarity compares the direction of two vectors. Similar meanings have similar directions, which powers semantic search.

🔑 Key terms

Vector
An ordered list of numbers describing one thing.
Dot product
Multiply matching numbers and add them up. Higher means more aligned.
Matrix multiplication
Transforming many vectors at once. The core work of a neural layer.

👀 See it in action

python
import numpy as np
you   = np.array([5, 1, 4])   # likes: cricket, cooking, movies (0–5)
riya  = np.array([4, 0, 5])
vikas = np.array([0, 5, 1])
print(you @ riya)    # 40 → very similar tastes
print(you @ vikas)   # 9  → quite different

The dot product as a simple 'taste similarity' score. Recommenders use this idea.

⚠️ Common mistakes to avoid

  • Mixing up shapes. A (3×2) matrix can only multiply something with 2 rows. Print shapes when debugging.
  • Comparing vectors with very different scales (rupees vs counts) without scaling. One feature dominates distances.

✅ Quick check

Two vectors have a large positive dot product. They are probably…

  1. Similar / pointing the same way
  2. Empty
  3. Very different
Show answer

A. Similar / pointing the same way: A large positive dot product means the vectors point in a similar direction, i.e. they're similar.

🙋 Questions learners ask

How much linear algebra do I need?

For most engineering roles: vectors, dot products, matrix multiplication and shapes. Research roles need much more.

What is NumPy's role?

NumPy stores vectors and matrices efficiently and does fast maths on them. Pandas and most ML libraries are built on top of it.

📌 Remember

Everything in AI becomes vectors, and similarity is just vector maths.

📖 Words from this lesson (AI dictionary)

Learn this as part of a full course

Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:

ML Engineer · AI Research Engineer

Continue in the course