Member-only story
Graph Similar Sentences With NetworkX
See how sentences relate to each other

For my research, I calculated how similar sentences were to each other.
This process involves transforming each sentence into a vector: a construct with direction and magnitude. Then, we take the cosine of the angle between the vectors. The closer the cosine is to one, the more similar the sentences are.
I created a 2D array showing each sentence’s cosine similarity to another. But I had trouble seeing something meaningful in a sea of numbers.
I converted this 2D array to a graph and plotted it with NetworkX, a Python library. That way, I could easily see clusters of similar sentences.
Here’s how you can do that.
Install Dependencies
If you don’t have Python on your computer, install the latest version from Python’s website. If you have trouble with that, try this tutorial.
For this project, you will need to install these libraries:
- matplotlib
- NetworkX
- sklearn
To do this, activate your virtual environment and run this command:
pip install matplotlib networkx scikit-learn
If you get a ModuleNotFoundError, your modules are probably installed in the wrong place. You may have many versions of Python and pip installed, so the wrong ones were used.
To fix this, identify where you need to install dependencies. Run this code:
import sys
print(sys.executable)
Copy the output. It might look something like /Users/your_name/your_folder/your_virtual_environment/bin/python
.
Then, run this command. Replace the first part with what you just copied and install the necessary modules.
/Users/your_name/your_folder/your_virtual_environment/bin/python -m pip install matplotlib networkx scikit-learn
Set Up To Build the Graph
Here’s the gist of it:
- Each node in this graph will represent a sentence.
- Each edge in this graph will connect…