ENع
→Vectors
الدرس 19 · الجبر الخطي والتفاضل والتكامل

Vectors

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

Vectors are the shared language of graphics and machine learning: a point on the screen, a direction toward a light, a word turned into numbers. Follow two vectors, a = [4, 3] and b = [1, 2], as the figure adds them tip to tail, scales them, measures their length, and uses the dot product to find the angle between them.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 19 · تفاعلي
01 / 10

A vector is a list

A vector is an ordered list of numbers, called components. a = [4, 3] means 4 steps along x, then 3 along y: an arrow from the origin. In machine learning the numbers are features; in graphics, positions and directions.

>>> a = [4, 3]
>>> a[0], a[1]            # the x and y components
(4, 3)
>>> len(a)                # a 2-dimensional vector
2
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A vector is a list

    A vector is an ordered list of numbers, called components. a = [4, 3] means 4 steps along x, then 3 along y: an arrow from the origin. In machine learning the numbers are features; in graphics, positions and directions.

    >>> a = [4, 3]
    >>> a[0], a[1]            # the x and y components
    (4, 3)
    >>> len(a)                # a 2-dimensional vector
    2
  2. Adding vectors

    Add vectors component by component: a + b = [4 + 1, 3 + 2] = [5, 5]. On the plane, slide b so its tail sits on the tip of a. The sum runs from the origin to where b now ends.

    >>> a, b = [4, 3], [1, 2]
    >>> [p + q for p, q in zip(a, b)]
    [5, 5]
    >>> a + b                 # careful: + joins two lists
    [4, 3, 1, 2]
  3. Scaling by a number

    Multiplying by a plain number, a scalar, multiplies every component: 2b = [2 × 1, 2 × 2] = [2, 4]. The arrow keeps its direction and doubles in length. A negative scalar flips it the other way.

    >>> b = [1, 2]
    >>> [2 * q for q in b]
    [2, 4]
    >>> [-1 * q for q in b]   # a negative scalar flips it
    [-1, -2]
    >>> 2 * b                 # careful: * repeats a list
    [1, 2, 1, 2]
  4. Length from Pythagoras

    The length, or norm, of a is written |a|. Its components are the two short sides of a right triangle, so Pythagoras gives |a| = √(4² + 3²) = √25 = 5. In more dimensions, square every component, add, take the root.

    >>> import math
    >>> math.sqrt(4**2 + 3**2)
    5.0
    >>> a = [4, 3]
    >>> math.hypot(*a)
    5.0
    >>> math.hypot(1, 2, 2)     # works in 3D too
    3.0
  5. Unit vectors

    Divide a vector by its length to get a unit vector: length 1, same direction. â = a ÷ 5 = [0.8, 0.6]. Graphics code normalises directions like this before it uses them, for example in lighting.

    >>> import math
    >>> a = [4, 3]
    >>> n = math.hypot(*a)
    >>> [x / n for x in a]
    [0.8, 0.6]
    >>> math.hypot(0.8, 0.6)
    1.0
  6. The dot product

    The dot product multiplies matching components and adds the results: a · b = 4 × 1 + 3 × 2 = 10. Two vectors go in, one plain number comes out. It measures how much the two point the same way.

    >>> import math
    >>> a, b = [4, 3], [1, 2]
    >>> sum(p * q for p, q in zip(a, b))
    10
    >>> math.sumprod(a, b)     # Python 3.12 and later
    10
    >>> math.sumprod(b, a)     # order does not matter
    10
  7. Dot product and angle

    The dot product also equals |a| |b| cos θ, where θ is the angle between the arrows. Here |b| = √5 ≈ 2.236, so cos θ = 10 ÷ (5 × 2.236) ≈ 0.894, and θ ≈ 26.6°.

    >>> import math
    >>> cos_t = 10 / (5 * math.hypot(1, 2))
    >>> cos_t
    0.8944271909999159
    >>> math.degrees(math.acos(cos_t))
    26.565051177077994
  8. Zero means perpendicular

    Take c = [−3, 4]. Then a · c = 4 × (−3) + 3 × 4 = 0. A zero dot product means cos θ = 0, so θ = 90°: the two arrows are perpendicular, at right angles.

    >>> import math
    >>> math.sumprod([4, 3], [-3, 4])
    0
    >>> math.degrees(math.acos(0))
    90.0
  9. Graphics: lighting a surface

    Graphics shades surfaces with dot products. With unit vectors n, the way a surface faces, and l, toward the light, its brightness is max(0, n · l). n = [0, 1] and l = â = [0.8, 0.6] give 0.6: 60% lit.

    >>> import math
    >>> n, l = [0, 1], [0.8, 0.6]
    >>> max(0, math.sumprod(n, l))
    0.6
    >>> max(0, math.sumprod(n, [0.8, -0.6]))   # light from below
    0
  10. ML: cosine similarity

    Machine learning turns words, images and users into vectors with hundreds of numbers. Cosine similarity, a · b ÷ (|a| |b|), scores how alike two are: a with b gives 0.894, with c gives 0, with −a gives −1.

    >>> import math
    >>> def cos_sim(u, v):
    ...     return math.sumprod(u, v) / (math.hypot(*u) * math.hypot(*v))
    ...
    >>> cos_sim([4, 3], [1, 2])
    0.8944271909999159
    >>> cos_sim([4, 3], [-3, 4]), cos_sim([4, 3], [-4, -3])
    (0.0, -1.0)

اختبار يتاح بعد الخطوة الأخيرة.