Python 特征和权重的热图
我运行了一个机器学习算法。现在我有一个系列,其索引=结果模型的特征,列是相应的权重 我想以热图的形式显示特征及其权重,其中我想显示权重较高的特征,比权重较轻的特征暗。也可以用与负重不同的颜色显示正重吗?。与所有具有正权重(如绿色)和正权重内的特征一样,基于权重值,所有具有正权重(如绿色)的特征可以具有暗、亮,而所有具有负权重(如红色)的特征可以具有负权重(如红色),同样,颜色的强度也会随着绝对值的变化而变化 下面是一个典型的特征权重矩阵的样子。它是一个以索引为特征的系列Python 特征和权重的热图,python,matplotlib,heatmap,Python,Matplotlib,Heatmap,我运行了一个机器学习算法。现在我有一个系列,其索引=结果模型的特征,列是相应的权重 我想以热图的形式显示特征及其权重,其中我想显示权重较高的特征,比权重较轻的特征暗。也可以用与负重不同的颜色显示正重吗?。与所有具有正权重(如绿色)和正权重内的特征一样,基于权重值,所有具有正权重(如绿色)的特征可以具有暗、亮,而所有具有负权重(如红色)的特征可以具有负权重(如红色),同样,颜色的强度也会随着绝对值的变化而变化 下面是一个典型的特征权重矩阵的样子。它是一个以索引为特征的系列 adm_hr_ls_7
adm_hr_ls_7 [-0.0151751599842]
admittype_elective [-0.0767214648205]
admission_age_inyears [0.629567909855]
patient_race_caucasian [-0.0543069188]
gender_female [-0.0831126807492]
marital_status_married [-0.0219135568879]
religion_none [-0.0629291312093]
employmentstatus_retired [0.0620868529898]
employmentstatus_not_employed [0.0195733078954]
编辑:
你的密码给了我类似的东西
我正在寻找一个网格,在该网格中,所有顶部的正面特征都以权重的abs值引导的颜色强度显示。所有正权重都有一种不同强度的颜色。类似地,所有顶部负重(同样是abs术语中的顶部负重)都有一种颜色,其强度与abs重量大小相对应。您的代码首先无法正确对齐标签。第二,它给了很多颜色
假设这是数据
admission_age_inyears [3.86703690989]
emergencydepartmentlengthofstayminutes [3.84708584711]
current_los_from_admissions [3.83956976064]
total_time_in_progressive_inpatient [3.63955027973]
total_time_spent_inpatient [2.59339330312]
nbr_of_hosp_last_90_days [2.44570139977]
total_time_spent_in_er [2.37914969651]
prior_admittype_emergency [2.18467109815]
nbr_inpatient_visits [2.09615621507]
curr_rx_gen_atorvastatin_calcium [2.08752966479]
substanceusehistory [1.91340885366]
timetofirstnurseminutes
to_be_discharged_to_hospice [-0.323042070071]
tot_est_median_age_years [-0.33548236033]
total_current_pharma_laxatives [-0.348768315972]
curr_rx_gen_rivaroxaban [-0.359848868739]
dis_notes_contact_info [-0.360264143656]
total_speak_indo_european [-0.373310297224]
patient_race_african_american [-0.391335453176]
financialclass_commercial [-0.427463083689]
curr_rx_gen_epinephrine_hcl [-0.44205667523]
tot_est_age_55_to_64_years [-0.451699358283]
percent_high_school_grad_or_higher [-0.461380248502]
tot_est_age_65_to_74_years
我想要的是,前10-15个正权重应该由一种常见颜色(比如绿色)表示,这样每个特征的颜色强度由相应特征权重的abs值定义。类似的,所有负权重特征(前10-15)应由一种常见颜色(如红色)表示,颜色的强度由相应特征权重的abs值定义
伊迪迪
编辑3:
我运行了这个代码。出错
n_features = 50
feature_names = ["feature_"+str(i) for i in range(n_features)]
weights = coef_lren.values
# select top 15 high and low features
indices = np.argsort(np.abs(weights))
n_top = 15
top = np.hstack((indices[:n_top], indices[-n_top:]))[::-1]
vmax = np.abs(weights).max()
plt.clf()
plt.imshow(weights[top].reshape((-1,1)),interpolation='nearest', cmap="seismic", vmin=-vmax, vmax=vmax)
plt.axes().xaxis.set_visible(False)
plt.colorbar()
tick_marks = np.arange(2 * n_top)
plt.yticks(tick_marks, [feature_names[i] for i in top])
433 not np.can_cast(self._A.dtype, np.float)):
--> 434 raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float
实际上还有一点工作要做,这应该会给你带来好的结果:
# define the range for the color mapping
# make sure the color map is centered on 0
# >> use maximum absolute value and not the real min and max (default behaviou)
vmax = np.abs(my_weights).max()
plt.imshow(my_weights.reshape((-1,1)), cmap="seismic", vmin=-vmax, vmax=vmax)
# add feature names
feature_names = ['foo', 'bar', ...]
tick_marks = np.arange(len(feature_names))
plt.yticks(tick_marks, feature_names)
编辑:
是的,这是可能的。人们通常使用“地震”彩色地图。你能提供一个例子吗?在线文档已经有很多例子了。。。只需在
imshow
中添加kwcmap=“seismic”
或您喜欢的绘图功能。这里您的my_功能就是我上面提到的系列?此外,如果在X轴上,我可以具有前十个特征,在Y轴上,目标变量值(1或0)的相应影响是否可能?。同样的概念是,my_features将是您想要显示的数值,如果您愿意,可以称之为weights。对于你的另一个问题,我不明白你想要什么…那是你的工作,追踪你程序中的错误。我不知道你在做什么,也不知道错误在哪里抛出!你确定coef_lren.values
是一个有效数组吗?我的代码运行良好,没有正确的修改吗?如果是这样的话,请接受答案,试着单独调试您的修改,或者发表一篇关于它的新文章……一般来说,一切都是可能的,但是您需要稍微阅读一下文档!要更改图形大小,请查找plt.axes()。设置纵横比(…)
import numpy as np
from matplotlib import pyplot as plt
n_features = 50
feature_names = ["feature_"+str(i) for i in range(n_features)]
weights = np.random.randn(n_features)
# select top 15 high and low features
indeces = np.argsort(weights)
n_top = 15
top = np.hstack((indeces[:n_top], indeces[-n_top:]))[::-1]
vmax = np.abs(weights).max()
plt.clf()
plt.imshow(weights[top].reshape((-1,1)),interpolation='nearest', cmap="seismic", vmin=-vmax, vmax=vmax)
plt.axes().xaxis.set_visible(False)
plt.colorbar()
tick_marks = np.arange(2 * n_top)
plt.yticks(tick_marks, [feature_names[i] for i in top])