Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot multidimensional vectors in Python

I have a Matrix that contains N users and K items. I want to plot that matrix in Python by considering each line as a vector with multiple coordinates. For example a simple point plot require X,Y. My vector hasK coordinates and I want to plot each one of those N vectors as a point to see there similarities. Can anyone help me with that ?

UPDATE:

#Matrix M shape = (944, 1683)
plt.figure()
plt.imshow(M, interpolation='nearest', cmap=plt.cm.ocean)
plt.colorbar()
plt.show()

but this gave me as result : enter image description here

What I want is something like that: enter image description here

like image 228
deltascience Avatar asked Dec 26 '22 02:12

deltascience


1 Answers

It is difficult from this question to be sure if my answer is relevant, but here's my best guess. I believe deltascience is asking how multidimensional vectors are generally plotted into two-dimensional space, as would be the case with a scatter plot. I think the best answer is that some kind of dimension reduction algorithm is generally performed. In other words, you don't do this by finding the right matplotlib code; you get your data into the right shape (one list for the X axis, and another list for the Y axis) and you then plot it using a typical matplotlib approach:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

M = np.random.rand(944, 1683)

pca = PCA(n_components=2)
reduced = pca.fit_transform(M)

# We need a 2 x 944 array, not 944 by 2 (all X coordinates in one list)
t = reduced.transpose()

plt.scatter(t[0], t[1])
plt.show()

Here are some relevant links:

https://stats.stackexchange.com/questions/63589/how-to-project-high-dimensional-space-into-a-two-dimensional-plane

http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html

https://towardsdatascience.com/the-art-of-effective-visualization-of-multi-dimensional-data-6c7202990c57

https://www.evl.uic.edu/documents/etemadpour_choosingvisualization_springer2016.pdf

July 2019 Addendum: It didn't occur to me at the the time, but another way people often visualize multi-dimensional data is with network visualization. Each multi-dimensional array in this context would be a node, and the edge weight would be something like the cosine similarity of two nodes, or the Euclidian distance. Networkx in python has some really nice visualization options.

like image 55
Matt L. Avatar answered Jan 01 '23 23:01

Matt L.