def roc_hfmc(hfmc, save_path=None, title="ROC图"): ''' :param hfmc: :param save_path: :return: ''' fig = plt.figure(figsize=(5.6, 5.6)) far = [1] far.extend(pofd_hfmc(hfmc).tolist()) far.append(0) pod = [1] pod.extend(pod_hfmc(hfmc).tolist()) pod.append(0) far = np.array(far) pod = np.array(pod) if (far.size < 30): plt.plot(far, pod, color="blue", linewidth=2, marker=".", label="实际预报") else: plt.plot(far, pod, color="blue", linewidth=2, label="实际预报") plt.plot([0, 1], [0, 1], ":", color="k", linewidth=1, label="无技巧预报") plt.xlabel("空报率", fontsize=14) plt.ylabel("命中率", fontsize=14) plt.ylim(0.0, 1.0) plt.xlim(0.0, 1.0) plt.legend(loc=4, fontsize=14) plt.title(title, fontsize=14) plt.xticks(fontsize=14) plt.yticks(fontsize=14) if save_path is None: plt.show() else: plt.savefig(save_path) print("检验结果已以图片形式保存至" + save_path) plt.close()
def roc_auc_hnh(hnh_array): ''' :param hnh_array: :return: ''' ngrade = hnh_array.shape[-2] total_grade_num = hnh_array[..., :, 0] observed_grade_num = hnh_array[..., :, 1] shape = list(total_grade_num.shape) shape.append(4) shape = tuple(shape) hfmc = np.zeros(shape) total_hap = np.sum(observed_grade_num, axis=-1) total_num = np.sum(total_grade_num, axis=-1) sum_axis = len(observed_grade_num.shape) - 1 for i in range(ngrade): hfmc[..., i, 0] = np.sum(observed_grade_num[..., i:], axis=sum_axis) hfmc[..., i, 1] = np.sum(total_grade_num[..., i:], axis=sum_axis) - hfmc[..., i, 0] hfmc[..., i, 2] = total_hap - hfmc[..., i, 0] hfmc[..., i, 3] = total_num - (hfmc[..., i, 0] + hfmc[..., i, 1] + hfmc[..., i, 2]) far = pofd_hfmc(hfmc) pod = pod_hfmc(hfmc) start1 = np.ones_like(total_num) end0 = np.zeros_like(total_num) auc = (start1 - far[..., 0]) * (start1 + pod[..., 0]) for i in range(1, ngrade): auc += (far[..., i - 1] - far[..., i]) * (pod[..., i - 1] + pod[..., i]) auc += (far[..., -1] - end0) * (pod[..., -1]) auc /= 2 return auc
def comprehensive_hnh(hnh_array, member_list=None,vmax = None,log_y = False, save_path=None,show = False,dpi = 300, title="概率预报综合检验图"): ''' :param th_array: :param save_path: :return: ''' sup_fontsize = 10 fig = plt.figure(figsize=(8, 5.6),dpi = dpi) grade_count = hnh_array.shape[-2] grade = 1 / grade_count if grade_count < 1: print('grade_count输入错误,不能小于1') return grade_list = np.arange(0, 1, grade).tolist() grade_list.append(1.1) shape = list(hnh_array.shape) new_th_array = hnh_array.reshape((-1, len(grade_list) - 1, 2)) new_th_array_shape = new_th_array.shape label = [] legend_num = new_th_array_shape[0] if member_list is None: if legend_num == 1: label.append('预报') else: for i in range(legend_num): label.append('预报' + str(i + 1)) else: label.extend(member_list) color_list = meteva.base.tool.color_tools.get_color_list(legend_num) bar_width = 1.0 / (grade_count * (legend_num + 3)) ymax = -9999 mark_line_x = [] mark_line_y = [] for line in range(legend_num): total_grade_num = new_th_array[line, :, 0] observed_grade_num = new_th_array[line, :, 1] total_num = np.sum(total_grade_num) under = np.zeros_like(total_grade_num) under[:] = total_grade_num[:] under[total_grade_num == 0] = 1 ob_rate = observed_grade_num / under ob_rate[total_grade_num == 0] = IV ob_rate_noIV = set_plot_IV(ob_rate) ob_rate[total_grade_num == 0] = np.nan index_iv = np.where(total_grade_num == 0) not_observed_grade_num = total_grade_num - observed_grade_num ngrade = len(total_grade_num) grade = 1 / ngrade x = np.arange(grade / 2, 1, grade) line_x = np.arange(0, 1.01, 0.1) prefect_line_y = np.arange(0, 1.01, 0.1) climate_line_y = np.ones_like(line_x) * np.sum(observed_grade_num) / total_num grid_plt = plt.GridSpec(6, 2) if line == 0: plt.suptitle(title,fontsize= sup_fontsize,y=0.95) plt.subplots_adjust(wspace=0.2, hspace=1) ax3 = plt.subplot(grid_plt[0:4, 1]) ax4 = plt.subplot(grid_plt[4:, :]) ax1 = plt.subplot(grid_plt[0:4, 0]) ax1.plot(x, ob_rate_noIV, "--", linewidth=0.5, color="k") x_iv = x[index_iv[0]] ob_rate_noIV_iv = ob_rate_noIV[index_iv[0]] ax1.plot(x_iv, ob_rate_noIV_iv, "x", color='k',label = None) if line == 0: ax1.plot(line_x, prefect_line_y, '--', label="完美", color="k") ax1.plot(line_x, climate_line_y, ':', label="无技巧", color="k") ax1.plot(x, ob_rate, marker=".", markersize="10", color=color_list[line],label = None) x1 = x + (line -legend_num / 2 + 0.5) * bar_width total_hap = np.sum(observed_grade_num) hfmc = np.zeros((len(total_grade_num), 4)) for i in range(ngrade): hfmc[i, 0] = np.sum(observed_grade_num[i:]) hfmc[i, 1] = np.sum(total_grade_num[i:]) - hfmc[i, 0] hfmc[i, 2] = total_hap - hfmc[i, 0] hfmc[i, 3] = total_num - (hfmc[i, 0] + hfmc[i, 1] + hfmc[i, 2]) far = [1] far.extend(pofd_hfmc(hfmc).tolist()) far.append(0) pod = [1] pod.extend(pod_hfmc(hfmc).tolist()) pod.append(0) far = np.array(far) pod = np.array(pod) if line == 0: ax3.plot([0, 1], [0, 1], ":", color="k", linewidth=1, label="无技巧") ax3.plot(far, pod, color=color_list[line], linewidth=2, label=label[line]) #ax4.bar(x1, observed_grade_num, width=bar_width*0.8, color=color_list[line],label = label[line]) ax4.bar(x1, (not_observed_grade_num+ observed_grade_num), width=bar_width*0.8,edgecolor=color_list[line],label =label[line]) ax4.set_xlabel("预测的概率",fontsize= sup_fontsize *0.9) ax4.set_ylabel("样本数",fontsize= sup_fontsize *0.9) ymax = max(np.max(observed_grade_num+not_observed_grade_num), ymax) mark_line_x.append(x1) mark_line_y.append((not_observed_grade_num+ observed_grade_num)* x) ax1.set_xlim(0.0, 1) ax1.set_ylim(0.0, 1) ax1.set_xlabel("预测的概率",fontsize= sup_fontsize *0.9) ax1.set_ylabel("实况的发生比例",fontsize= sup_fontsize *0.9) ax1.legend(loc=2,fontsize= sup_fontsize *0.9) ax3.set_xlabel("空报率",fontsize= sup_fontsize *0.9) ax3.set_ylabel("命中率",fontsize= sup_fontsize *0.9) ax3.set_ylim(0.0, 1.0) ax3.set_xlim(0.0, 1.0) ax3.legend(loc=4,fontsize= sup_fontsize *0.9) ax4.set_xlim(0.0, 1) ax4.set(title='\n') ax4.set_xticks(np.arange(0,1.01,1/grade_count)) #ax4.legend(loc="upper right", ncol=2) #mark_line_x = np.array(mark_line_x) #mark_line_y = np.array(mark_line_y) #mark_line_x = mark_line_x.T.flatten() #mark_line_y = mark_line_y.T.flatten() #ax4.plot(mark_line_x, mark_line_y, '.', color='k',markersize = bar_width * 300) #lines = ax4.get_children() #ax4.legend([lines[0], lines[grade_count],lines[grade_count * legend_num * 2 ]], ['观测正例', '观测负例',"合理比例"], # loc='upper center', bbox_to_anchor=(0.5, 1.0), ncol=3, prop={'size': 6},fontsize= sup_fontsize *0.9) if log_y: ax4.set_yscale('log') if vmax is None: ax4.set_ylim(0.0, ymax * 1.5) else: ax4.set_ylim(0.0, vmax) if save_path is None: show = True else: plt.savefig(save_path,bbox_inches='tight') print("检验结果已以图片形式保存至" + save_path) if show is True: plt.show() plt.close()
def roc_hfmc(hfmc_array, member_list=None, save_path=None,show = False,dpi = 300, title="ROC图"): ''' :param hfmc: :param save_path: :return: ''' fig = plt.figure(figsize=(5, 5),dpi = dpi) grade_count = hfmc_array.shape[-2] sup_fontsize = 10 grade = 1 / grade_count if grade_count < 1: print('grade_count输入错误,不能小于1') return grade_list = np.arange(0, 1, grade).tolist() grade_list.append(1.1) shape = list(hfmc_array.shape) new_hfmc_array = hfmc_array.reshape((-1, len(grade_list) - 1, 4)) new_hfmc_array_shape = new_hfmc_array.shape label = [] if member_list is None: if new_hfmc_array_shape[0] == 1: label.append('预报') else: for i in range(new_hfmc_array_shape[0]): label.append('预报' + str(i + 1)) else: label.extend(member_list) for line in range(new_hfmc_array_shape[0]): far = [1] far.extend(pofd_hfmc(new_hfmc_array[line, :]).tolist()) far.append(0) pod = [1] pod.extend(pod_hfmc(new_hfmc_array[line, :]).tolist()) pod.append(0) far = np.array(far) pod = np.array(pod) if (far.size < 30): plt.plot(far, pod, linewidth=2, marker=".", label=label[line]) else: plt.plot(far, pod, linewidth=2, label=label[line]) plt.plot([0, 1], [0, 1], ":", color="k", linewidth=1, label="无技巧") plt.xlabel("空报率", fontsize=sup_fontsize * 0.9) plt.ylabel("命中率", fontsize=sup_fontsize * 0.9) plt.ylim(0.0, 1.0) plt.xlim(0.0, 1.0) plt.legend(loc=4, fontsize=sup_fontsize * 0.9) plt.title(title, fontsize=sup_fontsize) plt.xticks(fontsize=sup_fontsize * 0.8) plt.yticks(fontsize=sup_fontsize * 0.8) if save_path is None: show = True else: plt.savefig(save_path,bbox_inches='tight') print("检验结果已以图片形式保存至" + save_path) if show is True: plt.show() plt.close()
def comprehensive_hnh(th_array, save_path=None, title="概率预报综合检验图"): ''' :param th_array: :param save_path: :return: ''' total_grade_num = th_array[:, 0] observed_grade_num = th_array[:, 1] total_num = np.sum(total_grade_num) under = np.zeros_like(total_grade_num) under[:] = total_grade_num[:] under[total_grade_num == 0] = 1 ob_rate = observed_grade_num / under ob_rate[total_grade_num == 0] = IV ob_rate_noIV = set_plot_IV(ob_rate) ob_rate[total_grade_num == 0] = np.nan index_iv = np.where(total_grade_num == 0) not_observed_grade_num = total_grade_num - observed_grade_num ngrade = len(total_grade_num) grade = 1 / ngrade x = np.arange(grade / 2, 1, grade) line_x = np.arange(0, 1.01, 0.1) prefect_line_y = np.arange(0, 1.01, 0.1) climate_line_y = np.ones_like(line_x) * np.sum( observed_grade_num) / total_num fig = plt.figure(figsize=(10, 7)) title_lines = len(title.split("\n")) plt.suptitle(title, y=0.90 + 0.03 * title_lines) plt.subplots_adjust(wspace=0.2, hspace=1) grid_plt = plt.GridSpec(6, 2) ax1 = plt.subplot(grid_plt[0:4, 0]) plt.plot(x, ob_rate_noIV, "--", linewidth=0.5, color="k") x_iv = x[index_iv[0]] ob_rate_noIV_iv = ob_rate_noIV[index_iv[0]] plt.plot(x_iv, ob_rate_noIV_iv, "x", color='k') plt.plot(x, ob_rate, marker=".", markersize="10", label="实际预报", color="r") plt.plot(line_x, prefect_line_y, '--', label="完美预报", color="k") plt.plot(line_x, climate_line_y, ':', label="无技巧预报", color="k") plt.xlim(0.0, 1.0) plt.ylim(0.0, 1) plt.xlabel("预测的概率") plt.ylabel("实况的发生比例") plt.legend(loc=2) ax2 = plt.subplot(grid_plt[4:, 0]) plt.bar(x, total_grade_num, width=0.03) #plt.setp(ax2.get_xticklabels()) plt.xlim(0.0, 1.0) plt.ylabel("样本数") plt.xlabel("预测的概率") total_hap = np.sum(observed_grade_num) hfmc = np.zeros((len(total_grade_num), 4)) for i in range(ngrade): hfmc[i, 0] = np.sum(observed_grade_num[i:]) hfmc[i, 1] = np.sum(total_grade_num[i:]) - hfmc[i, 0] hfmc[i, 2] = total_hap - hfmc[i, 0] hfmc[i, 3] = total_num - (hfmc[i, 0] + hfmc[i, 1] + hfmc[i, 2]) far = [1] far.extend(pofd_hfmc(hfmc).tolist()) far.append(0) pod = [1] pod.extend(pod_hfmc(hfmc).tolist()) pod.append(0) far = np.array(far) pod = np.array(pod) ax3 = plt.subplot(grid_plt[0:4, 1]) plt.plot(far, pod, color="blue", linewidth=2, label="实际预报") plt.plot([0, 1], [0, 1], ":", color="k", linewidth=1, label="无技巧预报") plt.xlabel("空报率") plt.ylabel("命中率") plt.ylim(0.0, 1.0) plt.xlim(0.0, 1.0) plt.legend(loc=4) ax4 = plt.subplot(grid_plt[4:, 1]) plt.bar(x - 0.01, not_observed_grade_num / total_num, width=0.02, edgecolor='r', fill=False, label="未发生") plt.bar(x + 0.01, observed_grade_num / total_num, width=0.02, color='b', label="已发生") plt.xlabel("预测的概率") plt.ylabel("占总样本数的比例") ymax = max(np.max(observed_grade_num / total_num), np.max(not_observed_grade_num / total_num)) * 1.5 plt.ylim(0.0, ymax) plt.xlim(0.0, 1.0) plt.legend(loc="upper right", ncol=2) if save_path is None: plt.show() else: plt.savefig(save_path) print("检验结果已以图片形式保存至" + save_path) plt.close()
def pofd_multi(ob, fo, grade_list): hfmc_array = hfmc_multi(ob, fo, grade_list) pofd_array = pofd_hfmc(hfmc_array) return pofd_array