功能和重量的热图

时间:2015-12-07 03:15:17

标签: python matplotlib heatmap

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

我想将这些特征及其权重显示为热图,我想要显示比重量更轻的特征更重的高重量特征。是否有可能显示不同颜色的正重量而不是负重量?像所有具有正重量的特征(如绿色和正重量)可以具有基于重量值的暗,光,而所有负重量具有红色,并且具有负重量,颜色的强度再次相应于绝对值。

以下是典型特征权重矩阵的外观。它是一个以索引为特征的系列。

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]

编辑:

你的代码给了我这样的东西

enter image description here

我正在寻找一个网格,其中显示所有顶部正面特征,颜色强度由权重的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)应该由一个常见颜色表示,如红色,颜色强度由相应特征权重的绝对值定义

EditII enter image description here

EDIT3:

我运行了这段代码。发出错误

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

1 个答案:

答案 0 :(得分:1)

实际上还有一些工作要做,这应该会给你带来很好的结果:

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

result