1

I have a dictionary of list :

topic_words_dict = {0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929]],
 1: [[-0.862131, 0.890915, 1.07759, 0.295002],[-0.437658, 0.279271, 0.627497, 0.322339]],
 2: [[-0.671647, 0.670583, 0.937155, 0.334581], [-0.675347, 0.466983, 0.8505440000000001, 0.5795710000000001]],
 3: [[-0.8414590000000001, 0.797826, 1.124295, 0.40925300000000003], [-0.567535, 0.40820300000000004, 0.811368, 0.429982]],
 4: [[-0.8560549999999999, 1.0617020000000001, 1.579302, 0.282398], [-0.576105, 0.5029239999999999, 0.9392, 0.400042]],
 5: [[-0.858527, 0.924175, 1.333083, 0.336538], [-0.562329, 0.37295500000000004, 0.9964350000000001, 0.439751]]
 }

where keys 0 to 5 represents 6 topics and values represents embeddings of words. According to "topic_words_dict" dictionary each topic contains embeddings of two words for example:

0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929]],

here topic "0" contains tow words embeddings [-0.669712, 0.6868, 0.9821409999999999, 0.287708] and [-0.925967, 0.6138399999999999, 1.247525, 0.740929]
In Python 3.x
How to visualize it using Scatter plot that will shows cluster of words (dots) under their topics, where each topic will represent as label. something as below:


plt.scatter(values, label=key)
plt.legend()

I didn't find some clear documentation that easily I can understand. Please Help. Thank you for your valuable time.

iforcebd
  • 87
  • 11
  • Maybe `for key in topic_words_dict: plt.scatter(topic_words_dict[key][0], topic_words_dict[key][1], label=key)` – JohanC Oct 13 '20 at 12:54
  • Could you please give me link of documentation please, because problem is iterating the key in python 3.x – iforcebd Oct 13 '20 at 22:08

1 Answers1

2

In Python 3.x, when looping through a dictionary, its key and the corresponding value can be retrieved at the same time using the items() method.

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

topic_words_dict = {0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929]],
 1: [[-0.862131, 0.890915, 1.07759, 0.295002],[-0.437658, 0.279271, 0.627497, 0.322339]],
 2: [[-0.671647, 0.670583, 0.937155, 0.334581], [-0.675347, 0.466983, 0.8505440000000001, 0.5795710000000001]],
 3: [[-0.8414590000000001, 0.797826, 1.124295, 0.40925300000000003], [-0.567535, 0.40820300000000004, 0.811368, 0.429982]],
 4: [[-0.8560549999999999, 1.0617020000000001, 1.579302, 0.282398], [-0.576105, 0.5029239999999999, 0.9392, 0.400042]],
 5: [[-0.858527, 0.924175, 1.333083, 0.336538], [-0.562329, 0.37295500000000004, 0.9964350000000001, 0.439751]]
 }

for key, value in topic_words_dict.items():
    ax.scatter(value[0],value[1],label=key)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.legend()

enter image description here

Sameeresque
  • 2,464
  • 1
  • 9
  • 22
  • Thank you very much – iforcebd Oct 13 '20 at 23:58
  • Happy to help, if this answer helped resolve your problem, you want to check the green mark. This will help keep the focus on unanswered questions. – Sameeresque Oct 14 '20 at 00:00
  • I have one more question please {0: [[-0.669712, 0.6868, 0.9821409999999999, 0.287708],[-0.925967, 0.6138399999999999, 1.247525, 0.740929],[-0.925967, 0.6138399999999999, 1.247525, 0.740929] ,..... , [-0.925967, 0.6138399999999999, 1.247525, 0.740929] ] If we have many list under each key say 200 in that case how to write this line ax.scatter(value[0],value[1],label=key). I mean value[0], value[1] for 200 list – iforcebd Oct 14 '20 at 00:36
  • Is value[0] and value[1] not your x and y-axis respectively. If not, then, what are your x-axis values? If you want to plot several lists, you can add an inner loop over value after the first for loop. – Sameeresque Oct 14 '20 at 00:43
  • https://stackoverflow.com/questions/64350898/plotting-dictionary-of-list-topic-word-in-python3-x @Sameeresqu – iforcebd Oct 14 '20 at 10:01