[PYTHON] Vectors are compressed to two dimensions by principal component analysis and visualized by matplotlib --Compress vectors to 2-dimension using Principal Component Analysis and visualize it with matplotlib.

Preparation-Preparation

vecs #Double array of numpy
name #label

Principal Component Analysis

from sklearn.decomposition import PCA #Principal component analyzer

pca = PCA(n_components=2)
pca.fit(vecs)
x = pca.transform(vecs)

for i in range(len(x)):
  X.append(x[j][0])
  Y.append(x[j][1])

Visualization-Visualization

fig, ax = pyplot.subplots(figsize=(15,15))
ax.scatter(X, Y)
for i, txt in enumerate(Name):
  ax.annotate(txt, (X[i], Y[i]))
pyplot.savefig("img.png ") #Save

Example --Example

Results of extracting and visualizing the vector corresponding to the country name from the pre-learning model of Wikipedia2Vec Visualization of country vectors, which was extracted Wikipedia2Vec model. img.png

Recommended Posts

Vectors are compressed to two dimensions by principal component analysis and visualized by matplotlib --Compress vectors to 2-dimension using Principal Component Analysis and visualize it with matplotlib.
Dimensional compression with self-encoder and principal component analysis
Collaborative filtering with principal component analysis and K-means clustering
Clustering and principal component analysis by K-means method (beginner)
Principal component analysis using python from nim with nimpy
Visualize the correlation matrix by principal component analysis in Python