2
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
# Step 1: Load the California Housing dataset
california_housing = fetch_california_housing()
df = pd.DataFrame(california_housing.data,
columns=california_housing.feature_names)
# Include target variable in the DataFrame
df['target'] = california_housing.target
# Step 2: Compute the correlation matrix
correlation_matrix = df.corr()
# Step 3: Visualize the correlation matrix using a heatmap
plt.figure(figsize=(12, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm',
fmt='.2f', cbar=True)
plt.title("Correlation Matrix Heatmap")
plt.show()
# Step 4: Create a pair plot to visualize pairwise relationships
between features
sns.pairplot(df, height=2.5, plot_kws={'alpha':0.7})
plt.suptitle("Pair Plot of California Housing Dataset",
y=1.02)
plt.show()
Comments
Post a Comment