Conditions | 7 |
Total Lines | 114 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | ''' |
||
19 | def missingval_plot(data, cmap='PuBuGn', figsize=(20, 12), sort=False, spine_color='#EEEEEE'): |
||
20 | ''' |
||
21 | Two-dimensional visualization of the missing values in a dataset. |
||
22 | |||
23 | Parameters: |
||
24 | ---------- |
||
25 | data: 2D dataset that can be coerced into an ndarray. If a Pandas DataFrame is provided, the index/column information is used to label the plots. |
||
26 | |||
27 | cmap: colormap, default 'PuBuGn' |
||
28 | Any valid colormap can be used. E.g. 'Greys', 'RdPu'. More information can be found in the matplotlib documentation. |
||
29 | |||
30 | figsize: tuple, default (20,12) |
||
31 | Use to control the figure size. |
||
32 | |||
33 | sort: bool, default False |
||
34 | Sort columns based on missing values in descending order and drop columns without any missing values |
||
35 | |||
36 | spine_color: color-code, default '#EEEEEE' |
||
37 | Set to 'None' to hide the spines on all plots or use any valid matplotlib color argument. |
||
38 | |||
39 | Returns: |
||
40 | ------- |
||
41 | ax: matplotlib Axes. Axes object with the heatmap. |
||
42 | ''' |
||
43 | |||
44 | if sort: |
||
45 | mv_cols_sorted = data.isna().sum(axis=0).sort_values(ascending=False) |
||
46 | final_cols = mv_cols_sorted.drop(mv_cols_sorted[mv_cols_sorted.values == 0].keys().tolist()).keys().tolist() |
||
47 | data = data[final_cols] |
||
48 | print('Displaying only columns with missing values.') |
||
49 | |||
50 | # Identify missing values |
||
51 | mv_cols = data.isna().sum(axis=0) |
||
52 | mv_rows = data.isna().sum(axis=1) |
||
53 | mv_total = mv_cols.sum() |
||
54 | mv_cols_rel = mv_cols / data.shape[0] |
||
55 | total_datapoints = data.shape[0]*data.shape[1] |
||
56 | |||
57 | if mv_total == 0: |
||
58 | print('No missing values found in the dataset.') |
||
59 | else: |
||
60 | # Create figure and axes |
||
61 | fig = plt.figure(figsize=figsize) |
||
62 | grid = fig.add_gridspec(nrows=6, ncols=6, left=0.05, right=0.48, wspace=0.05) |
||
63 | ax1 = fig.add_subplot(grid[:1, :5]) |
||
64 | ax2 = fig.add_subplot(grid[1:, :5]) |
||
65 | ax3 = fig.add_subplot(grid[:1, 5:]) |
||
66 | ax4 = fig.add_subplot(grid[1:, 5:]) |
||
67 | |||
68 | # ax1 - Barplot |
||
69 | colors = plt.get_cmap(cmap)(mv_cols / np.max(mv_cols)) # color bars by height |
||
70 | ax1.bar(range(len(mv_cols)), np.round((mv_cols_rel)*100, 2), color=colors) |
||
71 | ax1.get_xaxis().set_visible(False) |
||
72 | ax1.set(frame_on=False, xlim=(-.5, len(mv_cols)-0.5)) |
||
73 | ax1.set_ylim(0, np.max(mv_cols_rel)*100) |
||
74 | ax1.grid(linestyle=':', linewidth=1) |
||
75 | ax1.yaxis.set_major_formatter(ticker.PercentFormatter(decimals=0)) |
||
76 | ax1.tick_params(axis='y', colors='#111111', length=1) |
||
77 | |||
78 | # annotate values on top of the bars |
||
79 | for rect, label in zip(ax1.patches, mv_cols): |
||
80 | height = rect.get_height() |
||
81 | ax1.text(.1 + rect.get_x() + rect.get_width() / 2, height+0.5, label, |
||
82 | ha='center', |
||
83 | va='bottom', |
||
84 | rotation='90', |
||
85 | alpha=0.5, |
||
86 | fontsize='small') |
||
87 | |||
88 | ax1.set_frame_on(True) |
||
89 | for _, spine in ax1.spines.items(): |
||
90 | spine.set_visible(True) |
||
91 | spine.set_color(spine_color) |
||
92 | ax1.spines['top'].set_color(None) |
||
93 | |||
94 | # ax2 - Heatmap |
||
95 | sns.heatmap(data.isna(), cbar=False, cmap='binary', ax=ax2) |
||
96 | ax2.set_yticks(np.round(ax2.get_yticks()[0::5], -1)) |
||
97 | ax2.set_yticklabels(ax2.get_yticks()) |
||
98 | ax2.set_xticklabels( |
||
99 | ax2.get_xticklabels(), |
||
100 | horizontalalignment='center', |
||
101 | fontweight='light', |
||
102 | fontsize='medium') |
||
103 | ax2.tick_params(length=1, colors='#111111') |
||
104 | for _, spine in ax2.spines.items(): |
||
105 | spine.set_visible(True) |
||
106 | spine.set_color(spine_color) |
||
107 | |||
108 | # ax3 - Summary |
||
109 | fontax3 = {'color': '#111111', |
||
110 | 'weight': 'normal', |
||
111 | 'size': 12, |
||
112 | } |
||
113 | ax3.get_xaxis().set_visible(False) |
||
114 | ax3.get_yaxis().set_visible(False) |
||
115 | ax3.set(frame_on=False) |
||
116 | |||
117 | ax3.text(0.1, 0.9, f"Total: {np.round(total_datapoints/1000,1)}K", transform=ax3.transAxes, fontdict=fontax3) |
||
118 | ax3.text(0.1, 0.7, f"Missing: {np.round(mv_total/1000,1)}K", transform=ax3.transAxes, fontdict=fontax3) |
||
119 | ax3.text(0.1, 0.5, f"Relative: {np.round(mv_total/total_datapoints*100,1)}%", transform=ax3.transAxes, fontdict=fontax3) |
||
120 | ax3.text(0.1, 0.3, f"Max-col: {np.round(mv_cols.max()/data.shape[0]*100)}%", transform=ax3.transAxes, fontdict=fontax3) |
||
121 | ax3.text(0.1, 0.1, f"Max-row: {np.round(mv_rows.max()/data.shape[1]*100)}%", transform=ax3.transAxes, fontdict=fontax3) |
||
122 | |||
123 | # ax4 - Scatter plot |
||
124 | ax4.get_yaxis().set_visible(False) |
||
125 | for _, spine in ax4.spines.items(): |
||
126 | spine.set_color(spine_color) |
||
127 | ax4.tick_params(axis='x', colors='#111111', length=1) |
||
128 | |||
129 | ax4.scatter(mv_rows, range(len(mv_rows)), s=mv_rows, c=mv_rows, cmap=cmap, marker=".") |
||
130 | ax4.set_ylim(0, len(mv_rows)) |
||
131 | ax4.set_ylim(ax4.get_ylim()[::-1]) # invert y-axis |
||
132 | ax4.grid(linestyle=':', linewidth=1) |
||
133 | |||
274 |