sns画图总结

部署运行你感兴趣的模型镜像

seaborn画图小结

学习kaggle 的记录

如何选择相应的图来展示数据

趋势线

sns.lineplot 线形图

sns.lineplot(data=fifa_data) #以索引为横轴,多个列的值为纵轴
描述变量之间的关系
  • sns.barplot 条形图
  • sns.heatmap 热力图
  • sns.scatterplot 散点图
  • sns.regplot 包含趋势线的散点图
  • sns.lmplot 包含多条趋势线的散点图
  • sns.swarmplot 包含分类变量的散点图
分布图
  • sns.histplot 直方图
  • sns.kdeplot 核密度图
  • sns.jointplot 用来展示两个相关的核密度图

选择合适的风格

# Change the style of the figure to the "dark" theme
sns.set_style("dark")

# Line chart 
plt.figure(figsize=(12,6))

其他可选的风格还有:

  • "darkgrid"
  • "whitegrid"
  • "dark"
  • "white"
  • "ticks"

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

在机器学习中,`classification report` 是评估分类模型性能的重要工具之一,它提供了每个类别的 **precision(精确率)**、**recall(召回率)**、**f1-score(F1分数)** 和 **support(样本数)**。虽然 `sklearn` 的 `classification_report` 默认输出的是文本形式,但可以通过图形化方式将这些指标更直观地展示出来。 ### 1. 使用 `matplotlib` 和 `seaborn` 可视化分类报告 可以通过将 `classification_report` 的输出转换为 `DataFrame`,然后使用 `seaborn.heatmap` 或 `matplotlib` 绘制热力图来实现图形化展示。 #### 示例代码: ```python import seaborn as sns import matplotlib.pyplot as plt import pandas as pd from sklearn.metrics import classification_report # 假设 Y_test 和 Y_pred 是测试集的真实标签和预测结果 # 生成 classification_report 的字典形式输出 report = classification_report(Y_test, Y_pred, output_dict=True) # 转换为 DataFrame report_df = pd.DataFrame(report).transpose() # 使用 seaborn 热力图展示 plt.figure(figsize=(8, 6)) sns.heatmap(report_df.iloc[:-3, :-1], annot=True, cmap="Blues") plt.title("Classification Report Heatmap") plt.show() ``` 该方法通过热力图将 precision、recall 和 f1-score 以颜色深浅的形式呈现,使得结果更易于理解。 --- ### 2. 使用 `sklearn.metrics.plot_classification_report`(第三方扩展) 虽然 `scikit-learn` 本身不直接支持图形化展示 `classification_report`,但社区提供了一些扩展库,例如 `scikit-plot`,可以方便地实现可视化。 #### 安装 scikit-plot: ```bash pip install scikit-plot ``` #### 示例代码: ```python import scikitplot as skplt import matplotlib.pyplot as plt from sklearn.metrics import classification_report # 绘制分类报告 skplt.metrics.plot_classification_report(Y_test, Y_pred, cmap='Blues') plt.show() ``` 该方法直接生成一个美观的图形化分类报告图,支持多种配色方案。 --- ### 3. 使用 Streamlit 构建可视化应用 如果希望将分类报告嵌入 Web 应用中,可以使用 `Streamlit` 结合 `matplotlib` 或 `plotly` 实现交互式展示。 #### 示例代码(结合 Streamlit): ```python import streamlit as st import seaborn as sns import matplotlib.pyplot as plt from sklearn.metrics import classification_report st.subheader("Classification Report Visualization") report = classification_report(Y_test, Y_pred, output_dict=True) report_df = pd.DataFrame(report).transpose() fig, ax = plt.subplots(figsize=(8, 6)) sns.heatmap(report_df.iloc[:-3, :-1], annot=True, cmap="Blues", ax=ax) st.pyplot(fig) ``` 这种方式适合在可视化大屏或交互式仪表板中展示模型性能指标[^2]。 --- ### 4. 使用 SHAP 可视化特征对分类报告的影响 除了展示分类报告本身,还可以结合 `SHAP` 值来解释模型预测结果背后的原因。SHAP 可以可视化每个特征对模型输出的影响,从而帮助理解为何某些样本的 precision 或 recall 较低[^3]。 #### 示例代码: ```python import shap from sklearn.ensemble import RandomForestClassifier model = RandomForestClassifier() model.fit(X_train, Y_train) explainer = shap.TreeExplainer(model) shap_values = explainer.shap_values(X_test) shap.summary_plot(shap_values, X_test, plot_type="bar") ``` --- ### 总结 将 `classification report` 图形化展示可以通过以下方式实现: - 使用 `seaborn` 和 `matplotlib` 绘制热力图; - 利用 `scikit-plot` 提供的封装函数; - 在 `Streamlit` 应用中集成可视化; - 使用 `SHAP` 解释模型预测行为。 这些方法不仅提升了可读性,还增强了模型评估的交互性和可解释性。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值