What We’ll Build Today
Create a simple AI recommendation system using vector similarity
Master vector operations that power modern AI models
Build intuition for how vectors represent real-world data in AI systems
Youtube Video:
Why This Matters: The Building Blocks of AI Intelligence
Imagine trying to teach a computer that “cat” and “kitten” are similar, but “cat” and “car” are not. How would you do it? The answer lies in vectors - the fundamental data structure that allows AI systems to understand relationships between anything: words, images, user preferences, or product features.
Every time you use Netflix recommendations, Google search, or ChatGPT, vectors are working behind the scenes. They’re how AI systems convert the messy, complex real world into mathematical objects that computers can reason about. Today, we’ll build that foundation.
Core Concepts: Vectors as AI’s Universal Language
1. What Are Vectors in AI Context?
In AI, a vector isn’t just a mathematical concept - it’s a way to represent anything as a list of numbers. Think of a vector as a “fingerprint” for data:
# A user’s movie preferences as a vector
user_vector = [4.2, 1.1, 5.0, 2.8] # [action, romance, sci-fi, comedy]
# A word’s meaning as a vector (simplified)
word_cat = [0.8, 0.2, 0.9, 0.1] # [animal, furniture, pet, vehicle]
word_dog = [0.9, 0.1, 0.9, 0.0] # Similar to cat!
word_chair = [0.0, 0.9, 0.1, 0.0] # Very different from cat
Each number in the vector captures a different aspect or “dimension” of what we’re describing. The magic happens when we compare these vectors.
2. Vector Operations That Power AI
Addition and Subtraction: Combining or contrasting concepts
# Vector arithmetic reveals relationships
# king - man + woman ≈ queen (famous word2vec example)
concept_combination = king_vector - man_vector + woman_vector
Dot Product: Measuring similarity between vectors
import numpy as np
def similarity(vector1, vector2):
# Higher dot product = more similar
return np.dot(vector1, vector2)
# How similar are two users’ movie tastes?
user1 = [4.0, 2.0, 5.0, 3.0] # Loves action and sci-fi
user2 = [3.5, 2.5, 4.8, 2.8] # Similar taste!
print(f”Similarity: {similarity(user1, user2)}”) # High number = similar
Magnitude and Normalization: Standardizing vectors for fair comparison
def normalize_vector(vector):
magnitude = np.sqrt(np.sum(vector**2))
return vector / magnitude
# Normalized vectors let us compare purely by direction, not size
normalized_user1 = normalize_vector(np.array(user1))
3. Distance Between Vectors: The Heart of AI Decision Making
AI systems constantly ask: “How similar is this to that?” Vector distance gives us the answer:
def cosine_similarity(v1, v2):
# Most common similarity measure in AI
# Returns value between -1 (opposite) and 1 (identical)
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
def euclidean_distance(v1, v2):
# Straight-line distance between vectors
return np.sqrt(np.sum((v1 - v2)**2))
4. Practical Applications: Where You’ll See This
Every AI system you interact with uses vector operations:
Search engines: Convert your query to a vector, find similar document vectors
Recommendation systems: Find users with similar preference vectors
Image recognition: Convert pixels to feature vectors, compare to known object vectors
Language models: Every word becomes a vector, relationships emerge from vector math
Implementation: Building a Movie Recommendation Engine
GitHub Link:
https://github.com/sysdr/aiml/tree/main/day9/day9_vectorsLet’s create a simple but real recommendation system using vector operations:
import numpy as np
from typing import List, Tuple
class VectorRecommendationEngine:
def __init__(self):
# Movie database: [action, romance, comedy, drama, sci-fi]
self.movies = {
“The Matrix”: [5.0, 1.0, 2.0, 3.0, 5.0],
“Titanic”: [2.0, 5.0, 1.0, 5.0, 1.0],
“Avengers”: [5.0, 2.0, 3.0, 3.0, 4.0],
“The Notebook”: [1.0, 5.0, 2.0, 4.0, 1.0],
“Interstellar”: [3.0, 2.0, 1.0, 4.0, 5.0]
}
def cosine_similarity(self, v1: List[float], v2: List[float]) -> float:
“”“Calculate how similar two preference vectors are”“”
v1, v2 = np.array(v1), np.array(v2)
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
def recommend_movies(self, user_preferences: List[float], top_n: int = 3) -> List[Tuple[str, float]]:
“”“Find movies similar to user’s taste vector”“”
similarities = []
for movie, features in self.movies.items():
similarity = self.cosine_similarity(user_preferences, features)
similarities.append((movie, similarity))
# Sort by similarity (highest first)
similarities.sort(key=lambda x: x[1], reverse=True)
return similarities[:top_n]
# Try it out!
engine = VectorRecommendationEngine()
# User who loves action and sci-fi, dislikes romance
user_taste = [5.0, 1.0, 3.0, 2.0, 5.0] # [action, romance, comedy, drama, sci-fi]
recommendations = engine.recommend_movies(user_taste)
print(”Recommended movies:”)
for movie, score in recommendations:
print(f” {movie}: {score:.3f} similarity”)
This simple system demonstrates the core principle behind Netflix, Spotify, and Amazon recommendations - it’s all vector math!
Real-World Connection: Production AI Systems
In production AI systems, vectors get much more sophisticated:
Large Language Models like GPT use vectors with thousands of dimensions to represent word meanings. When you chat with AI, it’s comparing your message vector to its knowledge vectors.
Computer Vision systems convert images into feature vectors - a photo of your cat becomes a vector that captures edges, textures, and shapes. Recognition happens by finding the closest match in vector space.
Search Systems convert your query “best pizza near me” into a vector, then find document vectors (web pages, reviews) with the highest similarity scores.
The math stays the same as what we learned today - the scale just gets bigger. You’re now thinking like an AI engineer!
Next Steps: Tomorrow’s Adventure
Tomorrow we’ll explore matrices - think of them as collections of vectors that let us perform operations on hundreds or thousands of vectors simultaneously. We’ll see how matrix operations power neural networks and make AI training possible. The vector foundation you built today will make matrices feel natural and intuitive.
Key Takeaway: Vectors aren’t just math - they’re how AI systems understand and compare anything in the world. Master vector operations, and you’re speaking AI’s native language.



