Heatmap of Species Measurements

Using the iris dataset from scikit-learn, plot a heatmap representing the correlation coefficients between sepal length, sepal width, petal length, and petal width.

Example Output:

Use the imshow method for plotting the heatmap.

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
import numpy as np

def heatmap_species_measurements():
    iris = load_iris()
    correlations = np.corrcoef(iris.data, rowvar=False)
    
    plt.imshow(correlations, cmap='hot', interpolation='nearest')
    plt.colorbar()
    plt.xticks(range(4), ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"], rotation=45)
    plt.yticks(range(4), ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"])
    plt.title("Heatmap of Correlation Coefficients")
    plt.show()

# Example usage
heatmap_species_measurements()

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!