特征和权重的热图

Heat map of features and weights

我有 运行 机器学习算法。现在我有一个系列,其索引=结果模型的特征和列是相应的权重。

我想将特征及其权重显示为热图,其中我想显示权重高的特征比权重小的特征更暗。是否也可以用不同颜色显示正权重而不是负权重?就像所有具有正权重的特征(如绿色)和正权重内的特征一样,可以根据权重值具有深色、浅色,而所有负权重特征为红色,并且具有负权重的颜色强度再次根据绝对值变化。

这是典型的特征权重矩阵的样子。它是一个以索引为特征的系列。

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 weight size 相对应。您的代码首先无法正确对齐标签。其次,它提供了很多颜色。

假设这是数据。

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) 

编辑:

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])