0
$\begingroup$

I have a vector that represents my object and does a job of calculating which object is similar to the other object by using cosine similarity.

To create that vector, I've combined many features that can represent a unique object. For example, I have a vector that looks like this:

a = [1,2,3,4,5,6]

In this vector, 1,2,3 stands for feature A, 4,5 stands for feature B, 6 stands for feature C

My question is:

How can I determine which feature is needed in the vector? I need that results to find which feature I need to have in my vector.

Thank a lot for your advice! Pls help!

$\endgroup$

1 Answer 1

0
$\begingroup$

To determine how each element in a vector contributes to the cosine similarity when compared with other vectors, you can compute the cosine similarity between the vector and other vectors while excluding each element in turn. This will allow you to see how the cosine similarity changes when a particular element is excluded.

For example, suppose you have two vectors a and b that you want to compare using cosine similarity:

a = [1, 2, 3, 4, 5, 6] b = [2, 4, 6, 8, 10, 12]

To determine how each element in vector a contributes to the cosine similarity, you can compute the cosine similarity between a and b while excluding each element in turn:

Excluding element 1: cosine similarity = (2 * 2 + 4 * 4 + 6 * 6 + 8 * 8 + 10 * 10 + 12 * 12) / (sqrt(2^2 + 4^2 + 6^2 + 8^2 + 10^2 + 12^2) * sqrt(2^2 + 4^2 + 6^2 + 8^2 + 10^2 + 12^2)) = 0.94

Excluding element 2: cosine similarity = (1 * 2 + 3 * 4 + 6 * 6 + 8 * 8 + 10 * 10 + 12 * 12) / (sqrt(1^2 + 3^2 + 6^2 + 8^2 + 10^2 + 12^2) * sqrt(2^2 + 4^2 + 6^2 + 8^2 + 10^2 + 12^2)) = 0.86

Excluding element 3: cosine similarity = (1 * 2 + 2 * 4 + 4 * 6 + 8 * 8 + 10 * 10 + 12 * 12) / (sqrt(1^2 + 2^2 + 4^2 + 8^2 + 10^2 + 12^2) * sqrt(2^2 + 4^2 + 6^2 + 8^2 + 10^2 + 12^2)) = 0.82

From this analysis, you can see that element 1 has the greatest impact on the cosine similarity, followed by element 2 and element 3. You could prioritize keeping elements 1, 2, and 3 in the vector and consider removing other lower important elements if necessary.

$\endgroup$
1
  • $\begingroup$ Thank @Mohith. Although I understand your concept, your example is confusing me in the way of calculating cosine similarity. Pls correct me if I'm wrong, I need to remove each feature in my vector and observe how cosine similarity change. If a massive change in the cosine similarity, the excluded feature might be really important. $\endgroup$
    – Can Nguyen
    Dec 23, 2022 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.