Vector Primer
Vector Tutorial for Kids and AI Beginners
What is a Vector?
A vector is like a magical arrow that tells you:
- Which direction to go
- How far to go
Example: 3 steps right and 2 steps up → Vector: [3, 2]
Real Life Examples
- GPS:
[latitude, longitude] - Game character movement:
[x, y] - AI: Words, images, and sound are all represented as vectors
Types of Vectors
- 1D:
[5] - 2D:
[3, 4] - 3D:
[2, 4, 6]
Basic Vector Operations (Using NumPy)
import numpy as np
1 Create a Vector
v = np.array([3, 4])
2 Add Two Vectors
v1 = np.array([1, 2])
v2 = np.array([3, 4])
print(v1 + v2)
3 Subtract Two Vectors
print(v2 - v1)
4 Multiply by Number (Scaling)
print(2 * v1)
5 Dot Product
print(np.dot(v1, v2))
6 Length (Magnitude)
print(np.linalg.norm(v1))
Vector Use in AI
| AI Use | Vector Example | Meaning |
|---|---|---|
| Words | “king” = [0.5, 1.2, …] | Words as numbers |
| Images | Pixels = vector | Image comparison |
| Speech | Sound wave = vector | Voice recognition |
| Movement | [dx, dy] | Robot path |
Mini AI Task
goal = np.array([10, 10])
step1 = np.array([8, 9])
step2 = np.array([5, 3])
dist1 = np.linalg.norm(goal - step1)
dist2 = np.linalg.norm(goal - step2)
if dist1 < dist2:
print("Step1 is closer")
else:
print("Step2 is closer")
Advanced Operations for AI
Normalize a Vector (Unit Vector)
v = np.array([3, 4]) unit_v = v / np.linalg.norm(v) print(unit_v)
Cosine Similarity
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
v1 = np.array([1, 2])
v2 = np.array([2, 3])
print(cosine_similarity(v1, v2))
Matrix-Vector Multiplication
W = np.array([[0.2, 0.8], [0.6, 0.4]])
x = np.array([1, 2])
output = np.dot(W, x)
print(output)
Tip: Vectors are used in almost every part of AI — learning them early builds a superpower for the future!
Important Vector Concepts for AI (Intermediate Level)
Unit Vector / Normalization
Used to scale a vector so its length becomes 1. Useful in many AI models to normalize data.
v = np.array([3, 4])
unit_v = v / np.linalg.norm(v)
print("Unit Vector:", unit_v)
Angle Between Vectors
Helps measure direction difference between vectors. Closer angle = more similar direction.
def angle_between(v1, v2):
cos_theta = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
angle = np.arccos(cos_theta)
return np.degrees(angle)
v1 = np.array([1, 0])
v2 = np.array([0, 1])
print("Angle:", angle_between(v1, v2), "degrees")
Outer Product
Creates a matrix from two vectors. Used in attention mechanisms in Transformers.
v1 = np.array([1, 2])
v2 = np.array([3, 4])
outer = np.outer(v1, v2)
print("Outer Product:
", outer)
Cross Product (3D only)
Used in physics and robotics for direction. Only works with 3D vectors.
v1 = np.array([1, 0, 0])
v2 = np.array([0, 1, 0])
cross = np.cross(v1, v2)
print("Cross Product:", cross)
Vector Projection
Used to “shadow” one vector onto another. Great for AI feature extraction.
def projection(a, b):
return (np.dot(a, b) / np.dot(b, b)) * b
a = np.array([3, 4])
b = np.array([2, 0])
print("Projection of a onto b:", projection(a, b))
Book Recommendations
- Math Adventures with Python – Peter Farrell
- Coding Math – Keith Peters
- AI + Machine Learning for Kids – Dale Lane
- Super Simple Math (DK)
- Linear Algebra for Beginners – Richard Bronson
Go to Core Learning
