Say I have a dataset like this:

Hotel  HasPool AvgPrice
 1        1      $123
 2        0      $234 
 3        1      $200     

Currently I have broken down the dataset into 2 (one containing all continuous, other all categorical). The continuous one, I am calculating euclidean distance b/w all observation whereas for the categorical one I calculate Cosine distance . Is there a way to combine both scores effectively. Or, is there a distance function which works for both data types ? My final output should look like this:

Hotel  1   2   3
  1    1  0.3  0.7 
  2    0.3  1  0.5 
  3    0.7  0.5 1

Any help will be appreciated! Thanks


4 Answers 4


Similarity measures are subjective and so are they ways to combine them. You should decide what is your subjective definition of similarity and then find a way to combine them that fit your definition.

In general, I like to reduce similarity problems into classification problems. Given the dataset of items you have, create a new dataset of item pairs. The concept should be whether the two items in a pair are similar. Each similarity measure you have is a feature of the pair. Note that now you are in the good old classification framework. You can evaluate the similarity measures by computing the mutual information/accuracy/your chosen metric given the concept.

In your case, I would build the dataset this way. First choose pairs of items on which both your measures agree on. Choose pairs that are close by both the Euclidian distance and the cosine distance or pairs that are far by both measures. Note that since the similarity measures agree on these pair, combining them usually leads to agreement too and not that important. However, you should use such pairs so they will stabilize your weighting function. Other than that, you can easily generate these pair, which is always an advantage.

Next, you should choose pair on which your similarity function disagree. Choose some that are considered close by the first and far by the second and vice versa. Manually label the pairs in order to decide whether they are close or not.

Once you built the dataset, you can present your subjective definition of similarity as a set of pairs and distances. Now run any classifier (in your case a linear regression might fit what you look for) and use the result as the combined similarity measure.

  • $\begingroup$ Note that I assumed that in the cases the similarity measures agree, the result is indeed as you intended. If in not so, do some manual labeling there too. $\endgroup$
    – DaL
    Dec 15, 2015 at 11:18

Instead of doing a cosine similarity in the first place, I would like you to have a look at some of the similarity measures which exist for categorical data such as Eskin, IOF, OF, Lin, Lin1, Goodall1, 2, 3, 4, etc.

Since you are working with python, my suggestion to you would be to import the library named Categorical_similarity_measures and construct the proximity matrix for the data and then use clustering using Hierarchical Clustering Analysis (HCA)

Check out this link for the library: https://pypi.org/project/Categorical-similarity-measures/0.4/


To combine the cosine distance and euclidian distance, I would first normalize the euclidian distance on the same scale as the cosine distance [o-1], and then combine them by average. But I am sure you already try that approach. Thus I am wondering what is your definition of "combine both scores effectively" ? Do you need to weight differently the two metrics? How did you came to the final matrix you are showing? Is it intuition? If so, you can investigate this further to better define the weights you want to put on each feature and it associated metrics.

  • $\begingroup$ Yes I did exactly that- scaled them and averaged the two. Also been playing with the weights manually. Eg : 0.2*Matrix1+0.8*Matrix2. and have some reasonable output(intuitively/business sense wise reasonable). I was wondering if there was a less...err..intuition driven way and more solid math-y way to do it. Though now I guess not, since there is no 'right' answer. (Too used to have data tagged with dependent variables ) $\endgroup$
    – UD1989
    Dec 9, 2015 at 7:36

You can use Gower distance to obtain a similarity matrix when the data is mixed-type. This function is implemented in R packages cluster, CluMix and FD.

In cluster package, this is implemented in the function daisy. An example use would be,

 diss_mat <- daisy(data, metric = "gower")

where the columns of data contain the variables. You have to assign the correct type (numeric, factor or ordered factor) for the variables.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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