python-scipyHow do I create a dendrogram using Python and SciPy?
The dendrogram is a graphical representation of a hierarchical structure (e.g. a tree) that is commonly used to represent the relationships between data points in a dataset. It can be created using Python and SciPy with the following steps:
- Import the necessary libraries:
import scipy.cluster.hierarchy as sch
from matplotlib import pyplot as plt
- Create the data to be used in the dendrogram. For example, a 2x3 matrix:
data = [[2, 3, 4], [5, 6, 7]]
- Use the SciPy library to generate the linkage matrix:
Z = sch.linkage(data, 'ward')
- Generate the dendrogram using the linkage matrix:
dendrogram = sch.dendrogram(Z)
- Plot the dendrogram:
plt.show()
The resulting dendrogram will look like this:
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python and SciPy to create a tutorial PDF?
- How do I create a numpy array of zeros using Python?
- How do I create a numpy array of zeros using Python?
- How do I use the NumPy transpose function in Python?
- How do I use the scipy ttest_ind function in Python?
- How can I use Python and SciPy to find the zeros of a function?
- How can I use the x.shape function in Python Numpy?
See more codes...