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()
相关推荐
吴佳浩11 小时前
Langchain 浅出
python·langchain·llm
smj2302_7968265211 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
mortimer12 小时前
破局视频翻译【最后一公里】––从语音克隆到口型对齐的完整工程思路
python·github·aigc
门框研究员14 小时前
解锁Python的强大能力:深入理解描述符
python
子不语18015 小时前
Python——函数
开发语言·python
daidaidaiyu15 小时前
一文入门 LangChain 开发
python·ai
JJ1M816 小时前
用 Python 快速搭建一个支持 HTTPS、CORS 和断点续传的文件服务器
服务器·python·https
汤姆yu17 小时前
基于python大数据的小说数据可视化及预测系统
大数据·python·信息可视化
x***J34817 小时前
Python多线程爬虫
开发语言·爬虫·python
m***D28617 小时前
Python网络爬虫实战案例
开发语言·爬虫·python