Multidimensional scaling for dissimilarity visualization
By: Valentina Alto
Tags: Python, Dimensionality Reduction, Distance Matrix, Multidimensional Scaling, Dissimilarity

Multidimensional scaling for dissimilarity visualization

An implementation with Python

Multidimensional scaling is a powerful technique used to visualize in 2-dimensional space the (dis)similarity among objects. The idea is that we can derive to what extent two objects are similar, based on the geometric distance they exhibit in the 2D plan.

Namely, imagine we see the following:

We can think that pizza and pasta are kind of similar food items (they are both carbohydrates, both typical Italian food, and people tend to like them both), while cucumber is far from them (it is not that similar).

But which are the data from which this visualization is retrieved?

The starting point is building the so-called distance matrix: given N objects, a distance matrix is a MxM, symmetric matrix which determines how dissimilar from each other are all the couples of objects.

Retrieving the example above, we can namely make a survey asking people whether or not they would eat a given food item. Imagine we interview N people for M food items. If individual i would eat item j, then the cell (i,j) will be filled with a 1, otherwise, it will be filled with a 0.

Then, for each pair of food item, we can derive a 2x2 dissimilarity matrix as follows:

Where:

a is the total amount of time both item 1 and 2 are eaten

d is the total amount of time neither item 1 nor 2 are eaten

b is the total amount of time item 1 is eaten, while item 2 is not

c is the total amount of time item 2 is eaten, while item 1 is not

Finally, we can compute a dissimilarity score. In this case, I will use the Jaccard dissimilarity index, given by:

Now, each couple of food items will have a corresponding Jaccard dissimilarity score: we can hence build an MxM distance matrix filled with corresponding scores.

Where J_ik is the score corresponding to the couple of food items i and k.

Note: the principal diagonal of the distance matrix is filled with 0. Indeed, the Jaccard score for a couple of the same food item is 0 (b and c are indeed 0).

Now let’s implement it in Python. I will initialize a random dataset with 10 food items and 100 interviewed. Then, I will compute the corresponding dissimilarity matrix, which will be a 10x10 square matrix.

#importing necessary packages

import numpy as np

import scipy

import matplotlib.pyplot as plt from sklearn.metrics import pairwise_distances #jaccard diss.

from sklearn import manifold # multidimensional scaling foods_binary = np.random.randint(2, size=(100, 10)) #initial dataset

print(foods_binary.shape) Output: (100,10)

Now let’s initialize the distance matrix, using the Jaccard dissimilarity:

dis_matrix = pairwise_distances(foods_binary, metric = 'jaccard')

print(dis_matrix.shape) Output: (100,100)

Finally, let’s fit the multidimensional scaling algorithm to the distance matrix and plot the results:

mds_model = manifold.MDS(n_components = 2, random_state = 123,

dissimilarity = 'precomputed')

mds_fit = mds_model.fit(dis_matrix)

mds_coords = mds_model.fit_transform(dis_matrix)



food_names = ['pasta', 'pizza', 'meat', 'eggs', 'cheese', 'ananas', 'pear', 'bread', 'nuts', 'milk'] plt.figure()

plt.scatter(mds_coords[:,0],mds_coords[:,1],

facecolors = 'none', edgecolors = 'none') # points in white (invisible)

labels = food_names

for label, x, y in zip(labels, mds_coords[:,0], mds_coords[:,1]):

plt.annotate(label, (x,y), xycoords = 'data')

plt.xlabel('First Dimension')

plt.ylabel('Second Dimension')

plt.title('Dissimilarity among food items')

plt.show()

Note that we initialized a random original dataset, hence those results might be counterintuitive, yet they drive an important message. Imagining our dataset being true, they are telling us that eggs, cheese, and meat are almost perfect substitutes, since they satisfy the same need (represented by the area on the graph). On the other side, bread lies in a very isolated area, meaning that it has no competition in terms of substitutes.

Note that all the empty areas represent potential needs which have not been satisfied yet. That’s why multidimensional scaling might be a very powerful tool to investigate whether there is room for intervention in some markets, whether there are opportunities that have not been sized yet.

References