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()
相关推荐
武子康15 小时前
调查研究-155 Open-LLM-VTuber 本地部署与互动实战指南
人工智能·python·深度学习·ai·数字人
北漂人Java15 小时前
Pycharm配置Miniconda教程
python·pycharm
CTA量化套保15 小时前
量化程序 while True 一直跑 CPU 很高:天勤降频与字段过滤
python·区块链
copyer_xyf15 小时前
Python 内存分析:从栈和堆理解对象引用
前端·后端·python
大蚂蚁2号15 小时前
深度剖析Python全局解释器锁(GIL):原理、瓶颈与终极破局方案
python
高洁0115 小时前
打造行业知识图谱三步走
python·深度学习·数据挖掘·知识图谱
装不满的克莱因瓶16 小时前
使用 PyTorch Tensor 的相关数据处理
人工智能·pytorch·python·深度学习·机器学习·ai
如烟花的信页16 小时前
易盾点选逆向分析
javascript·爬虫·python·js逆向
金銀銅鐵16 小时前
用 Tkinter 实现一个简单的罗马数字转化工具
后端·python
ckjoker16 小时前
四大AI Agent架构拆解:我手敲了一个迷你版,发现了7条可迁移的设计原则
python·agent