Loading…
DS · Statistics · Program 04 · In the record
Compute and visualize covariance and correlation matrices.
1import pandas as pd2import seaborn as sns3import matplotlib.pyplot as plt4 5df = pd.DataFrame({'A': [10, 20, 20, 30, 40, 50],6 'B': [15, 25, 25, 35, 45, 55],7 'C': [5, 10, 15, 20, 25, 30]})8print("Covariance Matrix:\n", df.cov())9print("\nCorrelation Matrix:\n", df.corr())10 11fig, ax = plt.subplots(1, 2, figsize=(12, 5))12sns.heatmap(df.cov(), annot=True, cmap='coolwarm', ax=ax[0]).set_title("Covariance Matrix")13sns.heatmap(df.corr(), annot=True, cmap='coolwarm', ax=ax[1]).set_title("Correlation Matrix")14plt.show()Covariance Matrix:
A B C
A 216.666667 216.666667 135.0
B 216.666667 216.666667 135.0
C 135.000000 135.000000 87.5
Correlation Matrix:
A B C
A 1.000000 1.000000 0.980469
B 1.000000 1.000000 0.980469
C 0.980469 0.980469 1.000000
Run program 04 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.