ITPEC FE Subject B April 2024 Question 14
From the answer group below, select the correct answer to be inserted into blank in the description. Here, the array indexes start at 1.
The function calcSim takes two vectors (arrays) as input, calculates their similarity, and returns a value that characterizes the similarity. A large similarity indicates that the vectors are similar, and a small similarity indicates that the vectors are dissimilar. For example, to quantify the similarity between two documents, you can create a vector of occurrences of some words for each of the two documents and input them into calcSim. When the function calcSim( is called, the return value rounded to the first decimal place is blank.
Answer group
Explanation
0.9 — the function computes the cosine similarity of the two vectors, 0.85 before rounding.
The loop builds three sums: sxx of x × x, syy of y × y and sxy of x × y. The result is sxy divided by the square root of sxx × syy.
Result: 17 ÷ √25 × 16 = 17 ÷ √400 = 17 ÷ 20 = 0.85, which rounds to 0.9 at the first decimal place.
Answer (i)
Why not others:
- •(h) 0.8 — truncates 0.85 instead of rounding it
- •(j) 1.0 — the maximum, reached only when one vector is a positive multiple of the other; {2, 2, 1, 0, 4} and {3, 1, 1, 1, 2} are not proportional
- •(g) 0.7 — what 17 ÷ 25 = 0.68 gives, dividing by
sxxalone instead of the root of the product - •(d) 0.4 — what 17 ÷ (25 + 16) = 0.41 gives, adding the two sums of squares instead of taking the root of their product
- •(a) 0.1, (b) 0.2, (c) 0.3, (e) 0.5 and (f) 0.6 — no step of the trace produces these; with every product non-negative,
sxyis 17 against a denominator of 20
Key rule: Cosine similarity is the dot product divided by the product of the vector lengths; tabulate the three sums in one pass and round only at the end.
Math & Data Science · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.