Data ScienceMathematics and statistical foundations

Vectors and dot products through a similarity example

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 3 min read
Technically reviewed by Ishaan Sharma
In this article (6 sections)

A vector is an ordered set of values. Its dot product with another equal-length vector multiplies corresponding components and adds the products. The result depends on both direction and magnitude; cosine similarity normalizes by the vector lengths to compare direction.

The meaning of “similar” still depends on how the features were constructed. A correct vector calculation does not establish that two customers, documents or products are similar in the way a business decision requires.

Calculate the dot product by hand

Use abstract signed feature vectors A = (3, 4), B = (6, 8) and C = (4, -3). These are original mathematical teaching values, not customer counts or outputs from an embedding model.

A dot B is 3 × 6 + 4 × 8 = 50. A has Euclidean length 5 and B has length 10. Their cosine similarity is 50 / (5 × 10) = 1.

B is exactly twice A, so the two vectors point in the same direction while having different magnitudes. A dot C is 3 × 4 + 4 × (-3) = 0; the vectors are orthogonal in this feature space.

Reproduce the calculation

python
import numpy as np
from math_core import vector_rows,cosine

vectors = vector_rows()
a,b,c,z = [vectors[k] for k in ['A','B','C','Z']]
assert a.shape==b.shape==(2,)
assert np.dot(a,b)==50
assert np.linalg.norm(a)==5 and np.linalg.norm(b)==10
assert np.isclose(cosine(a,b),1)
assert np.isclose(cosine(a,c),0)
assert np.isclose(np.linalg.norm(a-b),5)
try:
    cosine(a,z)
except ValueError as error:
    assert str(error)=='zero_vector_cosine_undefined'
else:
    raise AssertionError('Zero-vector cosine needs an explicit policy')
print({'dot_A_B':50,'cosine_A_B':cosine(a,b),
       'cosine_A_C':cosine(a,c),'euclidean_A_B':5})

The mathematics lab includes the CSV, helper and executed reference checks. NumPy's dot documentation describes the one-dimensional inner-product operation used here. The local example runs on NumPy 2.4.4; the current stable documentation may describe a newer version.

Distinguish directional similarity from distance

A and B have cosine similarity one, yet their Euclidean distance is five. Neither result is contradictory. Cosine ignores their positive magnitude difference after normalization, while Euclidean distance retains it.

If magnitude represents a meaningful quantity, removing it may discard useful information. If magnitude mainly reflects document length or another nuisance factor, normalization may be appropriate. Decide from the representation and task rather than assuming one similarity measure is universally better.

Orthogonality also has a precise mathematical meaning here. It does not mean the underlying real-world entities are unrelated in every possible sense.

Handle zero and invalid vectors deliberately

Cosine similarity divides by both vector norms, so the ordinary formula is undefined when either vector is zero. The supplied helper raises a clear error. An application might instead return a missing value or use a documented fallback, but it should not silently treat the case as a meaningful similarity score.

The helper also rejects mismatched shapes and nonfinite values. It scales finite components before normalization to avoid a norm overflow in a large-value test. These checks protect the numerical operation; they do not validate whether the features are appropriate.

Preserve feature order and scaling

Corresponding positions must refer to the same feature. Swapping two columns in one vector can produce a valid number with the wrong meaning. Keep feature names and transformation versions with the representation.

Changing the scale of one feature can alter angles and rankings. For learned embeddings, normalization and similarity choices should match the model's intended use and be evaluated on the actual retrieval or comparison task.

No live embedding model is used in this exercise, and no retrieval-quality benchmark is claimed.

Exercise: compare A with -A and with 10A. Predict the dot product, cosine similarity and Euclidean distance before running the calculations. Explain which aspects of the vectors each measure preserves.

NeuraPath's Data Science course connects mathematical foundations with modelling and evaluation. Understanding dot products helps you inspect similarity calculations instead of treating a returned score as self-explanatory evidence.

Continue learning

This article is part of the Mathematics and statistical foundations sequence. Use the neighbouring tasks when you need the prerequisite or the next application.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.

This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.

Explore Data Science
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.