Python day18

@浙大疏锦行 python day 18.

内容:

  • 昨天学习了聚类算法的一些基本内容,今天继续学习相关知识
  • 分析簇的特征和相关含义(使用可视化来进行分析,也可以使用ai)

代码:

python 复制代码
shap.initjs()
# 初始化 SHAP 解释器
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(x1) # 这个计算耗时
shap_values.shape # 第一维是样本数,第二维是特征数,第三维是类别数

shap.summary_plot(shap_values[:, :, 0], x1, plot_type="bar",show=False)  #  这里的show=False表示不直接显示图形,这样可以继续用plt来修改元素,不然就直接输出了
plt.title("SHAP Feature Importance (Bar Plot)")
plt.show()

# 绘制簇0的分布图
import matplotlib.pyplot as plt

# 总样本中的前四个重要性的特征分布图
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes = axes.flatten()

for i, feature in enumerate(selected_features):
    axes[i].hist(X_cluster0[feature], bins=20)
    axes[i].set_title(f'Histogram of {feature}')
    axes[i].set_xlabel(feature)
    axes[i].set_ylabel('Frequency')

plt.tight_layout()
plt.show()
# 簇2
import matplotlib.pyplot as plt

# 总样本中的前四个重要性的特征分布图
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes = axes.flatten()

for i, feature in enumerate(selected_features):
    axes[i].hist(X_cluster2[feature], bins=20)
    axes[i].set_title(f'Histogram of {feature}')
    axes[i].set_xlabel(feature)
    axes[i].set_ylabel('Frequency')

plt.tight_layout()
plt.show()
相关推荐
Salt_072834 分钟前
DAY 36 官方文档的阅读
python·算法·机器学习·github
k***921637 分钟前
Python 科学计算有哪些提高运算速度的技巧
开发语言·python
superman超哥37 分钟前
仓颉条件变量深度解析与实践:解锁高效并发同步
开发语言·python·c#·仓颉
长空任鸟飞_阿康38 分钟前
LangGraph 技术详解:基于图结构的 AI 工作流与多智能体编排框架
人工智能·python·langchain
love530love1 小时前
ComfyUI 升级 v0.4.0 踩坑记录:解决 TypeError: QM_Queue.task_done() 报错
人工智能·windows·python·comfyui
阿坤带你走近大数据2 小时前
Python基础知识-数据结构篇
开发语言·数据结构·python
小智RE0-走在路上2 小时前
Python学习笔记(7)--集合,字典,数据容器总结
笔记·python·学习
沃斯堡&蓝鸟2 小时前
DAY 29 异常处理
python
Direction_Wind2 小时前
抓包的使用与讲解
python
职业码农NO.12 小时前
智能体推理范式: Plan-and-Execute(规划与执行)
人工智能·python·数据分析·系统架构·知识图谱·agent·集成学习