def plot_all(self): path = "./data/samsung_contest/data_tansformed.csv" df = load_samsung(path) reg_cols = [ 'c02_사망자수', 'c03_사상자수', 'c04_중상자수', 'c05_경상자수', 'c06_부상신고자수', ] label_encoder_cols = [] for k in df.columns: if '_label' in k: label_encoder_cols += [k] onehot_col = [] for k in df.columns: if '_onehot' in k: onehot_col += [k] x_cols = reg_cols + onehot_col origin_cols = [ 'c00_주야', 'c01_요일', 'c02_사망자수', 'c03_사상자수', 'c04_중상자수', 'c05_경상자수', 'c06_부상신고자수', 'c07_발생지시도', 'c08_발생지시군구', 'c09_사고유형_대분류', 'c10_사고유형_중분류', 'c11_법규위반', 'c12_도로형태_대분류', 'c13_도로형태', 'c14_당사자종별_1당_대분류', 'c15_당사자종별_2당_대분류', ] # pprint(label_encoder_cols) # pprint(onehot_col) # pprint(x_cols) # pprint(origin_cols) plot = PlotTools() for key in origin_cols: # plot.dist(df, key, title=f'dist_{key}') plot.count(df, key, title=f'count_{key}') for a_key in origin_cols: for b_key in origin_cols: try: plot.count(df, a_key, b_key, title=f'count_{a_key}_groupby_{b_key}') except BaseException as e: print(a_key, b_key, e)
class df_plotterMixIn: def __init__(self): self.plot = PlotTools() def plot_all(self, df, df_Xs_keys, df_Ys_key): self._df_cols_plot(df, df_Xs_keys, df_Ys_key) @deco_exception_catch def _plot_dist(self, df: DF, col_key: str, partial_df: DF, series: Series, Xs_keys: list, Ys_key: list, path=None): title = f'{col_key}_plot_dist' self.plot.dist(df, col_key, title=title, path=f"./matplot/{title}.png") @deco_exception_catch def _plot_count(self, df: DF, col_key: str, partial_df: DF, series: Series, Xs_keys: list, Ys_key: list, path=None): title = f'{col_key}_plot_count_bar' self.plot.count(df, col_key, title=title, path=f"./matplot/{title}.png") @deco_exception_catch def _plot_violin(self, df: DF, col_key: str, partial_df: DF, series: Series, Xs_keys: list, Ys_key: list, path=None): title = f'{col_key}_plot_violin' self.plot.violin_plot(df, col_key, Ys_key, path=f"./matplot/{title}_1.png", title=title) self.plot.violin_plot(df, Ys_key, col_key, path=f"./matplot/{title}_2.png", title=title) @deco_exception_catch def _plot_joint2d(self, df: DF, col_key: str, partial_df: DF, series: Series, Xs_keys: list, Ys_key: list, path=None): title = f'{col_key}_plot_joint2d' self.plot.joint_2d(df, col_key, Ys_key, path=f"./matplot/{title}.png", title=title) @deco_exception_catch def _plot_dist_groupby(self, df: DF, col_key: str, partial_df: DF, series: Series, Xs_keys: list, Ys_key: list, path=None): title = f'{col_key}_plot_dist_groupby' self.plot.dist_groupby(df, Ys_key, col_key, df, title=title, path=f"./matplot/{title}.png") self.plot.dist_groupby(df, col_key, Ys_key, df, title=title, path=f"./matplot/{title}.png") def _df_cols_plot(self, df, df_Xs_keys, df_Ys_key): with JobPool() as pool: for key in df_Xs_keys: col = df[[key]] series = df[key] args = (df, key, col, series, df_Xs_keys, df_Ys_key) pool.apply_async(self._plot_dist, args=args) pool.apply_async(self._plot_count, args=args) pool.apply_async(self._plot_violin, args=args) pool.apply_async(self._plot_joint2d, args=args)
class DF_PlotTools(LoggerMixIn): def __init__(self, df, y_key, n_job=7, path=None): super().__init__() self.plot = PlotTools() self.n_job = n_job self.df = df self.y_key = y_key self.path = path if self.path is None: self.path = "./plot_outs" def plot_all(self, ): with JobPool(self.n_job) as pool: keys = self.df.keys() df = self.df # plot dist for key in keys: args = [df, key] pool.apply_async(self.plot_dist, args) # plot count for key in keys: args = [df, key] pool.apply_async(self.plot_countbar, args) # plot violin for key in keys: args = [df, key, self.y_key] pool.apply_async(self.plot_violin, args) args = [df, self.y_key, key] pool.apply_async(self.plot_violin, args) # plot_joint2d for key in keys: args = [df, key, self.y_key] pool.apply_async(self.plot_joint2d, args) args = [df, self.y_key, key] pool.apply_async(self.plot_joint2d, args) # plot countbar_groupby for a_key in keys: args = [df, a_key, self.y_key] pool.apply_async(self.plot_count_groupby, args) @deco_exception_catch def plot_dist(self, df: DF, col_key: str, title=None): if title is None: title = f'{col_key}_plot_dist' self.plot.dist(df, col_key, title=title, path=f"./matplot/{title}.png") @deco_exception_catch def plot_countbar(self, df: DF, col_key: str, title=None): if title is None: title = f'{col_key}_plot_count_bar' self.plot.count(df, col_key, title=title, path=f"./matplot/{title}.png") @deco_exception_catch def plot_violin(self, df: DF, a_col, b_col, title=None): if title is None: title = f'{a_col}_{b_col}_plot_violin' self.plot.violin_plot(df, a_col, b_col, path=f"./matplot/{title}.png", title=title) @deco_exception_catch def plot_joint2d(self, df: DF, a_col, b_col, title=None): if title is None: title = f'{a_col}_{b_col}_plot_joint2d' self.plot.joint_2d(df, a_col, b_col, path=f"./matplot/{title}.png", title=title) @deco_exception_catch def plot_dist_groupby(self, df: DF, a_col, groupby_cols, title=None): if title is None: title = f'{a_col}_groupby_{groupby_cols}_plot_dist_groupby' self.plot.dist_groupby(df, a_col, groupby_cols, title=title, path=f"./matplot/{title}.png") @deco_exception_catch def plot_count_groupby(self, df: DF, a_col, groupby_cols, title=None): if title is None: title = f'{a_col}_groupby_{groupby_cols}_count' self.plot.count(df, a_col, groupby_cols, title=title, path=f"./matplot/{title}.png") @deco_exception_catch def plot_violin_groupby(self, df: DF, a_col, b_col, groupby_cols, title=None): if title is None: title = f'{a_col}_{b_col}_groupby_{groupby_cols}_violin' self.plot.violin_plot(df, a_col, b_col, groupby_cols, title=title, path=f"./matplot/{title}.png")