Kaggle / Python ネタです。
苦労して作ったので、ここでも紹介しておきます。notebookはここです。
EDA about Pressure with Colored Charts
これは人工呼吸器の圧力を予測するコンペティションです。入力圧力の平均値によって色分けし、そのカラーマップを使っていくつかグラフを書きたかったのです。苦労しましたが、きれいにできました。とあるGrand Masterからリクエストあったので作ってみたところ。upvoteが増えるといいな。
一部のみ掲載しておきます。全文はkaggle notebookを参照してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
def plot_time_uin_mean_cmap(RClist=RCmorder, xmin=0.8, xmax=1.2, ymin=0, ymax=35, vlmin=0, vlmax=0, alpha=0.5): df = uout1_df a = alpha for rc in RClist: t = df[df['R_Cm']==rc] #print(len(t)) if len(t) == 0: continue # color map cm = plt.cm.get_cmap('gist_ncar') fig = plt.figure(figsize = (16, 4)) ax1 = fig.add_subplot(1, 1, 1) #x = t['time_step'] x = t['time_uout1'] y = t['u_in_half_mean'] im1 = ax1.scatter(x, y, c=y, vmin=ymin, vmax=ymax, label=rc, alpha=a, cmap=cm) ax1.set_xlabel('time_uout1') ax1.set_ylabel('u_in_half_mean') ax1.set_title(f'{rc}: time_uout1 - u_in_half_mean') ax1.set_xlim(xmin, xmax) ax1.set_ylim(ymin, ymax) if vlmin or vlmax: ax1.vlines([vlmin, vlmax], ymin, ymax, "red", linestyles='dashed') fig.colorbar(im1, ax=ax1) plt.show() def plot_double_pre_RCm_cmap(RCm, time_uout1_from=0.995, time_uout1_until=1.005, max_plots=False, ylim=100, alpha=False, vmin=0, vmax=30): df=uout1_df t=df[df['R_Cm']==RCm] t=t[(t['time_uout1']>=time_uout1_from)&(t['time_uout1']<time_uout1_until)] bid_list = t['breath_id'] print('The number of target charts:', len(bid_list)) fig = plt.figure(figsize = (12, 4)) ax1 = fig.add_subplot(1, 2, 1) ax2 = fig.add_subplot(1, 2, 2) cm = plt.cm.get_cmap('gist_ncar') if alpha: a = alpha else: if (len(bid_list)): a = max(1.0/len(bid_list), 0.01) else: a = 1 if not max_plots: max_plots = len(bid_list) ############################## ax1.set_xlabel('Time') ax1.set_ylabel('u_in') title_str = f'time - u_in' ax1.set_title(title_str) ax1.set_ylim(0,ylim) ############################## ax2.set_xlabel('Time') ax2.set_ylabel('Pressure') title_str = f'time - pressure' ax2.set_title(title_str) ax2.set_ylim(0, ylim) ############################## print('The number of plotting:', len(bid_list[:max_plots])) for bid in tqdm(bid_list[:max_plots]): tmp = train.loc[train['breath_id'] == bid].reset_index(drop=True) line_df = train.loc[train['breath_id'] == bid].reset_index(drop=True) x = line_df['time_step'] y1 = line_df['u_in'] y2 = line_df['pressure'] c = line_df['u_in_half_mean'].tolist()[0] #vmin=0 #vmax=30 norm = mcolors.TwoSlopeNorm(vmin=vmin, vmax=vmax, vcenter=(vmin+vmax)/2) #print(cm(norm(c))) im1 = ax1.plot(x, y1, alpha=a, color=cm(norm(c))) im2 = ax2.plot(x, y2, alpha=a, color=cm(norm(c))) #fig.colorbar(mappable, ax=axa) fig.tight_layout() plt.show() def plot_colored_u_in_mean(RCm='05_20', time_from =0.995, time_until = 1.005, vmax = 30, alpha1=0.05, alpha2=False, ylim=100, max_plots=1000): plot_time_uin_mean_cmap(RClist=[RCm], ymax=vmax, xmin=0.96, xmax=1.04, alpha=alpha1, vlmin=time_from, vlmax=time_until) plot_double_pre_RCm_cmap(RCm, time_uout1_from=time_from, time_uout1_until=time_until, alpha=alpha2, max_plots=max_plots, vmin=0, vmax=vmax, ylim=ylim) |