Galle, LK--:--5/6 sites live

Vector Operations in Python Part 1

ඔයාගෙ dataset එකේ row එකක් — රෝගියෙකුගෙ, නිවසක එක් feature එකක්ම — ඇත්තටම vector එකක්. ඉගෙන ගන්න ML formula ඕනම එකක් තියෙන්නෙ vector maths එකක් වෙස්ගැන්වෙලා.

A row in your dataset — every feature of one patient, one house — is literally a vector. Every ML formula you'll meet is vector maths in disguise.

9 May 20263 min readSelf Guide · Stage 14#Maths & First Models#Maths for ML#Machine learning

Lesson 25 of 64 · Part 2 · Maths & First Models · Maths for ML · bilingual notes (English + සිංහල)

Why this matters. A row in your dataset — every feature of one patient, one house — is literally a vector. Every ML formula you'll meet is vector maths in disguise.

සිංහලෙන්: ඔයාගෙ dataset එකේ row එකක් — රෝගියෙකුගෙ, නිවසක එක් feature එකක්ම — ඇත්තටම vector එකක්. ඉගෙන ගන්න ML formula ඕනම එකක් තියෙන්නෙ vector maths එකක් වෙස්ගැන්වෙලා.

What you'll use this for. The dot product you learn here is, almost unchanged, the exact calculation Linear Regression uses to make a prediction.

සිංහලෙන්: මෙතන ඉගෙන ගන්න dot product එක, එහෙම ම කිට්ටුවෙන්, Linear Regression එකෙන් prediction එකක් හදන ගණනයමයි.

Key principles

  • A vector is just an ordered list of numbers with meaning attached. [age, income, years_experience] is a 3-dimensional vector — nothing more mysterious than that.
    Vector එකක් කියන්නෙ අර්ථයක් තියෙන, පිළිවෙළකට තියෙන numbers ලැයිස්තුවක් විතරයි

Walkthrough

ExamplePython
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
ExamplePython
sns.set()

Plotting a vector

ExamplePython
plt.quiver(0,0,4,5)
plt.show()

Vector Operations in Python Part 1 — figure 1

ExamplePython
plt.quiver(0,0,4,5, scale_units='xy', angles='xy', scale=1)
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 2

ExamplePython
plt.quiver(0,0,4,5, scale_units='xy', angles='xy', scale=1, color='b')
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 3

ExamplePython
plt.quiver(0,0,4,5, scale_units='xy', angles='xy', scale=1, color='b')
plt.quiver(0,0,-3,-6, scale_units='xy', angles='xy', scale=1, color='y')
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 4

Addition of two Vectors

ExamplePython
vector_1 = np.asarray([0,0,2,3])

vector_2 = np.asarray([0,0,3,-2])

sum = vector_1 + vector_2
print(sum)

Output

Output
[0 0 5 1]
ExamplePython
plt.quiver(0,0,2,3, scale_units='xy', angles='xy', scale=1, color='b')
plt.quiver(0,0,3,-2, scale_units='xy', angles='xy', scale=1, color='y')
plt.quiver(0,0,5,1, scale_units='xy', angles='xy', scale=1, color='r')
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 5

Subtraction of 2 vectors

ExamplePython
vector_1 = np.asarray([0,0,2,3])

vector_2 = np.asarray([0,0,3,-2])

difference = vector_1 - vector_2
print(difference)

Output

Output
[ 0  0 -1  5]
ExamplePython
plt.quiver(0,0,2,3, scale_units='xy', angles='xy', scale=1, color='b')
plt.quiver(0,0,3,-2, scale_units='xy', angles='xy', scale=1, color='y')
plt.quiver(0,0,-1,5, scale_units='xy', angles='xy', scale=1, color='r')
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 6

Multiplying a vector by a scalar

ExamplePython
vector_1 = np.asarray([0,0,2,3])

vector_2 = 2*vector_1

print(vector_2)

Output

Output
[0 0 4 6]
ExamplePython
plt.quiver(0,0,2,3, scale_units='xy', angles='xy', scale=1, color='b')
plt.quiver(0,0,4,6, scale_units='xy', angles='xy', scale=1, color='y')
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 7

ExamplePython
vector_1 = np.asarray([0,0,2,3])

vector_2 = -0.5*vector_1

print(vector_2)

Output

Output
[-0.  -0.  -1.  -1.5]
ExamplePython
plt.quiver(0,0,2,3, scale_units='xy', angles='xy', scale=1, color='b')
plt.quiver(0,0,-1,-1.5, scale_units='xy', angles='xy', scale=1, color='y')
plt.xlim(-8,8)
plt.ylim(-8,8)
plt.show()

Vector Operations in Python Part 1 — figure 8


Keep going: ← Lesson 24 · Lesson 26 → · Track it in the Self Guide