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()
相关推荐
落鹜秋水1 小时前
xss总结
开发语言·python
Norvyn_71 小时前
LeetCode|Day19|14. 最长公共前缀|Python刷题笔记
笔记·python·leetcode
单线程_011 小时前
血条识别功能实现及原理
python
拉一次撑死狗2 小时前
Python绘制数据(二)
开发语言·python·信息可视化
淦暴尼2 小时前
用Python做数据分析:5个实战案例
python·信息可视化·数据分析
花火|3 小时前
关于pytorch虚拟环境及具体bug问题修改
人工智能·pytorch·python
斟的是酒中桃3 小时前
【学习记录】智能客服小桃(进度更新ing)
人工智能·python·学习·语言模型·langchain·agent
西猫雷婶3 小时前
python学智能算法(十九)|SVM基础概念-超平面
开发语言·人工智能·python·深度学习·算法·机器学习·支持向量机
小白的程序空间4 小时前
Anaconda Prompt中删除库虚拟环境【保姆级教程】
linux·开发语言·python
EulerBlind5 小时前
【工具】Pycharm隐藏侧边灯泡提示
ide·python·pycharm