以sklearn的红酒数据集为例
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine
data = load_wine()
df = pd.DataFrame(data.data, columns=data.feature_names)
print("维度:\n",df.shape)
print("特征属性的字段类型:\n",df.dtypes)
print("输出前20行:\n",df.head(20))
print("描述性统计:\n",df.describe())
print("自带数据集直接查看描述性统计:\n",data.DESCR)
# 直方图
df.hist(bins=20, figsize=(15, 10))
plt.show()
# 密度图
import seaborn as sns
fig, axes = plt.subplots(4, 4, figsize=(16, 16))
axes = axes.ravel()
for i, feature in enumerate(df.columns):
sns.kdeplot(data=df[feature], ax=axes[i])
axes[i].set_title(f'Density Plot - {feature}')
# 隐藏多余的子图
for j in range(len(df.columns), len(axes)):
axes[j].set_visible(False)
plt.tight_layout()
plt.show()
# 箱线图
sns.boxplot(data=df, palette="Set2")
plt.show()
# 散点矩阵图
sns.pairplot(df)
plt.show()
# 相关矩阵图
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()