Lesson 2 of 116 min
Adding and scaling vectors
Scale some vectors and add them up, and everything you can reach is their span: a line, a flat sheet, or more.
After this lesson you can combine vectors and say what space they can reach.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
A mix, then its color code
The video's violet, in NumPy: scale each light, then add. A web page or a design tool writes a color as three whole numbers from 0 to 255, often as one hex code. Multiply by 255, round, and write each number as two hex digits.
import numpy as np
red = np.array([1, 0, 0])
blue = np.array([0, 0, 1])
violet = 0.5 * red + 1 * blue
print(violet)
code = np.round(violet * 255).astype(int)
print(code)
print("#" + "".join(f"{n:02x}" for n in code))[0.5 0. 1. ] [128 0 255] #8000ff
Which amounts make a color
The video found orange's amounts with the picture. NumPy finds them for you. Stand the three lights side by side as the columns of one table, then ask np.linalg.solve for the amounts that make orange: red at 1, green at 0.5, blue at 0.
green = np.array([0, 1, 0])
lights = np.column_stack([red, green, blue])
orange = np.array([1, 0.5, 0])
print(np.linalg.solve(lights, orange))[1. 0.5 0. ]
When a color is out of reach
With red and blue only, no amounts make orange, and solve won't take two lights for three numbers. np.linalg.lstsq gives the amounts that come nearest instead. Take the nearest color away from orange, and what is left is the green the sheet can't give.
two = np.column_stack([red, blue])
amounts = np.linalg.lstsq(two, orange)[0]
nearest = two @ amounts
print(amounts)
print(nearest)
print(orange - nearest)[1. 0.] [1. 0. 0.] [0. 0.5 0. ]
A screen's 0.5 is not half the light
The numbers say how strongly each light is told to shine, not how much light comes out. Most screens follow a rule called sRGB: 0.5 gives only about a fifth of the light, and it takes about 0.735 to give half. Code that mixes the light of two pictures turns their numbers into light first, mixes, then turns back.
def light(v):
# sRGB: the number a screen is sent, to the light that comes out
return v / 12.92 if v <= 0.04045 else ((v + 0.055) / 1.055) ** 2.4
print(round(light(0.5), 3))
print(round(light(0.735), 3))0.214 0.499
Run on Python 3.13 and NumPy 2.5.5. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Vectors
A walk is written east first, then north: [3, 2] is three blocks east and two north. Where does the walk [1, -2] end?
2 of 3
Red light is [1, 0, 0] and blue light is [0, 0, 1]: red, green, blue. Can some amounts of red and blue make orange, [1, 0.5, 0]?
3 of 3
Red is [1, 0, 0] and green is [0, 1, 0]. You take 1 × red plus some amount of green, and you get orange, [1, 0.5, 0]. What amount of green?
The ideas in this lesson
- Linear combination
- Scale each vector by an amount, then add them: the result is a linear combination.
- Span
- Everything some vectors can reach with any amounts: a line, a flat sheet, or more.
- Independent
- Vectors are independent when none can be made from the others, so each adds a new direction.
It leans on
- Vectorization
Transcript
What the video says, part by part. Press a time to watch from there.
The bet
Here's a bet. Every color on your screen is mixed from three lights. Red, green and blue. Now turn the green one off. Turn red and blue up or down, as much as you like. Can you make orange?
A color is three numbers
Keep your guess.
How does a screen hold a color as numbers? With three numbers: how strongly its red, green and blue lights shine, in that order. Each one goes from zero, off, to one, full. Red is one, zero, zero. Blue is zero, zero, one. So a color is a vector: a list of numbers, drawn as an arrow. It has three dimensions, one for each light.
Here's the arrow, in a box of colors. Red goes across. Green goes up. And blue goes back, away from you.
Scaling
How do you make the red light dimmer? Multiply every number by the same amount. Take half of red, and its red number drops to a half. A dark red. What happens to the arrow?
It keeps its direction... and it's half as long. Twice red would be twice as long.
Multiplying a vector by one amount is called scaling.
Adding
What happens when both lights shine at once? Add the matching numbers, red's and blue's. You get one, zero, one. Magenta.
On the picture, the blue arrow starts where the red arrow ends.
A linear combination
Now do both. Scale each light, then add them. Half a red, plus a full blue. Violet. Scaling vectors, then adding them up... is called a linear combination. In NumPy, red and blue are arrays, and the whole mix is one line. Scale, then add.
The span
So which colors can red and blue reach, at every amount? Look at the green number. Red has no green, and blue has no green. So every mix of red and blue has no green either.
Try every amount of each light. Every mix lands on one flat sheet. It passes through black, where both lights are off.
Everything the vectors can reach... is called their span.
So the span of red and blue is this flat sheet, with no green anywhere on it.
Orange
So, can red and blue make orange?
Orange is red at full, and green at half. So its point is above the sheet.
No amounts of red and blue... reach it.
If you said yes, it's because orange looks like a kind of red. But a dimmer red light is only a darker red, on the red arrow's line. Orange needs green... and the span of red and blue has none.
Always a sheet?
So two lights reach a flat sheet of colors.
Do two lights always reach a sheet?
Only a line
Take a red light, and a dark red light: red at half.
What can these two reach?
Only shades of red, along one line. The dark red is just the red, dimmed. So it adds no new direction.
Independent
Now back to red and blue, and add green. Green points straight up, off the sheet. No amounts of red and blue make green.
Vectors where none of them can be made from the others... are called independent.
Green gives you the one direction the sheet doesn't have. So red, green and blue reach every color. Orange is red, plus green at half. Add dark red instead of green, and you get nothing new. Dark red is the red, dimmed. So red, blue and dark red are not independent.
Your screen
Every tiny dot of the screen you're watching is three small lights: red, green and blue. Three independent directions. So together, they reach every color your screen can show.
In code: mixup
Scale, then add, works on vectors of any length. Here's a handwritten zero, from a set of digit pictures. It's sixty-four numbers, in an eight by eight grid of grays.
And here's a one. This line takes seventy percent of the zero, and thirty percent of the one. Then it adds them. What picture comes out?
Mostly the zero, with a faint one through it. All sixty-four numbers, mixed in one line.
AI training code does this on purpose. It's called mixup. The model learns from the mixed picture, and from a mixed answer: seventy percent zero, thirty percent one. Many image models are trained this way, and it helps them do better on pictures they've never seen.
What you saw
So here's what you saw. Scaling makes an arrow longer or shorter, along its own line. Adding puts one arrow after the other. Scale, then add, and that's a linear combination. Everything you can reach that way is the span. And a vector that adds a new direction is independent.
Scale, then add
So scale some vectors, then add them up. That's a linear combination. Now try it on this lesson's page, and take the quiz. Make black from red and dark red. Neither amount can be zero.
You use this lesson in Lab 1: Search a store by meaning, after Distance and cosine similarity.
Facts last checked on .
