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()
相关推荐
徐先生 @_@|||27 分钟前
基于Translation插件实现在pycharm本地翻译并阅读英文资料
ide·python·pycharm
xcbrand37 分钟前
文旅行业品牌策划公司找哪家
大数据·运维·人工智能·python
好家伙VCC44 分钟前
**发散创新:基于Rust的轻量级权限管理库设计与开源许可证实践**在现代分布式系统中,**权限控制(RBAC
java·开发语言·python·rust·开源
Dxy12393102161 小时前
Python序列标注模型上下文纠错详解
开发语言·python
ZhengEnCi1 小时前
P2H-Python字符串格式化完全指南-format和f-string的Python编程利器
python
HaiXCoder1 小时前
python从入门到精通-第5章: 函数式编程 — Python的函数式风格
python
HaiXCoder1 小时前
python从入门到精通-第0章: 思维模式碰撞
python
HaiXCoder1 小时前
python从入门到精通-第3章: 数据结构 — Python的"瑞士军刀
python
Orange_sparkle1 小时前
learn claude code学习记录-S02
java·python·学习
小郑加油1 小时前
python学习Day1:python的安装与环境搭载
python·学习·小白记录,保姆式教程