资料来源:火山引擎-开发者社区
本文为 AI 编程社知识库精选文章。作者为湖南大学赵芷谦同学。
AI 编程社(AI Coding Club,简称 ACC)是一个面向新时代开发者的共创共学社区,专注于分享 AI 编程领域的最新资讯、应用和实践,通过交流、学习和共创等方式,让更多人成为新时代的 10x 开发者。
1.本实践材料 ========
为了帮助编程初学者们更轻松地学习本文内容、上手实践项目,推荐使用 Trae 进行编程获得技术支持以及问题解答。
2.本实践需要的语法基础 =============
2.1 Python 基础与环境配置
1.模块导入(import):
包管理工具:需通过 pip
安装 pandas
, seaborn
, matplotlib
, scikit
-learn 等库。
开发环境:代码适合在 Jupyter Notebook 或 IDE(如 PyCharm, VS Code)中运行。
2.2 数据处理基础 (Pandas)
1.DataFrame 创建方法扩展
字典键自动成为列名,值作为列数据
适用于外部数据导入场景
2.列操作增强
2.3 数据可视化增强
1.Seaborn 高级配置
2.Matplotlib 图表优化
3.开始实践 =======
3.1 代码实践目的
通过代码实现对鸢尾花数据集的加载、处理,并利用小提琴图展示不同类别鸢尾花在各个特征上的分布情况,以便直观地观察和比较不同类别之间的特征差异。
3.2 分析数据集
鸢尾花数据集是一个经典的多分类数据集,常用于机器学习和数据分析的教学和实践。代码通过使用sklearn.datasets
模块中的load\_iris
函数加载数据集,并对数据集进行了基本的探索性数据分析(EDA),包括查看数据集的形状、特征名称、类别标签以及数据的统计描述。
3.3 代码实现步骤(借助 Trae)
3.3.1 环境配置与数据加载
加载数据集:
iris.data
:特征数据(4 列,共 150 条样本)
iris.target
:类别标签(0, 1, 2)
3.3.2 数据预处理
标签映射:
将数值标签(0/1/2)转换为可读的类别名称
3.3.3 数据可视化
3.3.4 结果与分析
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load\_iris
# 加载鸢尾花数据集 iris = load\_iris()
# 将数据集转换为DataFrame df = pd.DataFrame(data=iris.data, columns=iris.feature\_names)
df['target'] = iris.target
# 将目标标签映射为类别名称
df['target'] = df['target'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
# 设置Seaborn的绘图风格
sns.set(style="whitegrid")
# 绘制特征分布的小提琴图矩阵
plt.figure(figsize=(10, 6))
sns.violinplot(x='target', y='sepal length (cm)', data=df)
sns.violinplot(x='target', y='sepal width (cm)', data=df)
sns.violinplot(x='target', y='petal length (cm)', data=df)
sns.violinplot(x='target', y='petal width (cm)', data=df)
# 设置图表标题和坐标轴标签
plt.title('Violin Plot of Iris Dataset Features')
plt.xlabel('Species')
plt.ylabel('Feature Values')
# 显示图表
plt.show()
复制代码
1.花萼宽度(sepal width) :setosa 的分布最集中,且中位数最高
2.花瓣长度与宽度(petal length/width) :setosa 明显小于其他两类,versicolor 和 virginica 存在部分重叠
3.类别区分度 :花瓣特征(长度、宽度)比花萼特征更具区分性
3.3.5 技术细节与改进方向
技术亮点:
使用 seaborn.violinplot
高效绘制多维度分布图
通过 map
方法实现标签语义化,提升可解释性
潜在改进:
图表优化:分面绘制(FacetGrid)或并列显示多个子图,避免重叠
交互式可视化:使用 plotly
库生成可交互图表,支持动态探索
改进后: Trae 提示词:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load\_iris
import plotly.express as px
# 加载鸢尾花数据集
iris = load\_iris()
# 将数据集转换为DataFrame
df = pd.DataFrame(data=iris.data, columns=iris.feature\_names)
df['target'] = iris.target
# 将目标标签映射为类别名称
df['target'] = df['target'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
# 设置Seaborn的绘图风格
sns.set(style="whitegrid")
# 使用FacetGrid分面绘制小提琴图
g = sns.FacetGrid(df, col="target", height=4, aspect=1)
g.map(sns.violinplot, "target", "sepal length (cm)", palette="Set2")
g.map(sns.violinplot, "target", "sepal width (cm)", palette="Set2")
g.map(sns.violinplot, "target", "petal length (cm)", palette="Set2")
g.map(sns.violinplot, "target", "petal width (cm)", palette="Set2")
# 设置图表标题和坐标轴标签
g.fig.suptitle('Violin Plot of Iris Dataset Features')
g.set\_axis\_labels('Species', 'Feature Values')
# 显示图表
plt.show()
# 使用Plotly生成可交互图表
fig = px.violin(df, x='target', y=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], box=True, points='all', hover\_data=df.columns, color='target')
fig.update\_layout(title='Interactive Violin Plot of Iris Dataset Features', xaxis\_title='Species', yaxis\_title='Feature Values')
fig.show()
复制代码
3.3.6 总结
本实践通过数据预处理与可视化,直观展示了鸢尾花数据集中不同类别的特征分布差异。小提琴图能够同时反映数据分布密度与统计指标,适用于多维数据的对比分析。此方法可扩展至其他分类任务的特征探索场景。
4.探索 =====
Trae 提示词
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load\_iris
import plotly.express as px
from sklearn.ensemble import RandomForestClassifier
import shap
import umap
from sklearn.manifold import TSNE
# 加载鸢尾花数据集 iris = load\_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature\_names)
df['target'] = iris.target
df['target'] = df['target'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
# 快速探索:使用seaborn的pairplot
sns.pairplot(df, hue='target')
plt.show()
# 快速探索:使用plotly交互图表 fig = px.scatter\_matrix(df, dimensions=iris.feature\_names, color='target')
fig.show()
# 模型解释:使用SHAP值 X = df.drop('target', axis=1)
y = df['target']
model = RandomForestClassifier()
model.fit(X, y)
explainer = shap.TreeExplainer(model)
shap\_values = explainer.shap\_values(X)
shap.summary\_plot(shap\_values, X)
# 高维数据:使用UMAP非线性降维 reducer = umap.UMAP()
embedding = reducer.fit\_transform(X)
plt.figure()
plt.scatter(embedding[:, 0], embedding[:, 1], c=iris.target, cmap='viridis')
plt.colorbar()
plt.title('UMAP projection of the Iris dataset')
plt.show()
# 高维数据:使用t-SNE非线性降维 tsne = TSNE(n\_components=2, random\_state=42)
tsne\_embedding = tsne.fit\_transform(X)
plt.figure()
plt.scatter(tsne\_embedding[:, 0], tsne\_embedding[:, 1], c=iris.target, cmap='viridis')
plt.colorbar()
plt.title('t-SNE projection of the Iris dataset')
plt.show()
# 报告展示:箱线图+小提琴图组合
plt.figure(figsize=(12, 8))
for i, feature in enumerate(iris.feature\_names): plt.subplot(2, 2, i + 1) sns.boxplot(x='target', y=feature, data=df) sns.violinplot(x='target', y=feature, data=df, inner=None, color='0.8')
plt.show()
复制代码
图形为矩阵式布局。对角线位置是核密度估计图,展示鸢尾花单个特征(萼片长度、萼片宽度、花瓣长度、花瓣宽度)的分布情况,通过曲线形状能看出数据的集中趋势和离散程度。非对角线位置是散点图,呈现不同特征之间的关系,比如花瓣长度和萼片长度之间的关联。
图中不同颜色的点对应鸢尾花的不同品种,蓝色代表山鸢尾(setosa)、橙色代表变色鸢尾(versicolor)、绿色代表维吉尼亚鸢尾(virginica)。从图中可观察到不同品种在各个特征上的分布差异。
数据特征分析 :
萼片长度:山鸢尾的萼片长度普遍较短,维吉尼亚鸢尾和变色鸢尾的萼片长度分布范围更广且有重叠。
萼片宽度:山鸢尾的萼片宽度分布相对集中,维吉尼亚鸢尾和变色鸢尾的分布较为分散。
花瓣长度和宽度:这两个特征在区分不同品种时表现较为明显,山鸢尾的花瓣长度和宽度明显小于维吉尼亚鸢尾和变色鸢尾,而后两者在这两个特征上也有一定区分度。
特征相关性 :
从散点图能看出,部分特征之间存在一定线性关系,如花瓣长度和花瓣宽度之间呈现出正相关趋势,即花瓣长度较长时,花瓣宽度往往也较大。
通过这张图,可以直观地了解鸢尾花数据集不同特征的分布情况以及不同品种之间的差异,为进一步的数据建模和分析提供参考。
评论