|
| 1 | +import mars.dataframe as md |
| 2 | +import mars.tensor as mt |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | + |
| 7 | +class BinningMethod(object): |
| 8 | + BUCKET = "bucket" |
| 9 | + QUANTILE = "quantile" |
| 10 | + LOG_BUCKET = "log_bucket" |
| 11 | + |
| 12 | + |
| 13 | +def binning( |
| 14 | + in_md, |
| 15 | + col_name, |
| 16 | + bin_method, |
| 17 | + bins, |
| 18 | + boundaries): |
| 19 | + if boundaries: |
| 20 | + bin_o, bins = md.cut(in_md[col_name], bins=boundaries, labels=False, retbins=True) |
| 21 | + bins_np = bins.to_numpy() |
| 22 | + else: |
| 23 | + if bin_method.lower() == BinningMethod.BUCKET.lower(): |
| 24 | + bin_o, bins = md.cut(in_md[col_name], bins=bins, labels=False, retbins=True) |
| 25 | + bins_np = bins.to_numpy() |
| 26 | + elif bin_method.lower() == BinningMethod.LOG_BUCKET.lower(): |
| 27 | + bin_o, bins = md.cut(mt.log(in_md[col_name]), bins=bins, labels=False, retbins=True) |
| 28 | + bins_np = np.exp(bins.to_numpy()) |
| 29 | + else: |
| 30 | + raise ValueError("Unsupport binning method: {}".format(bin_method)) |
| 31 | + |
| 32 | + return bin_o, bins_np |
| 33 | + |
| 34 | + |
| 35 | +def cumsum(arr, reverse): |
| 36 | + if type(arr) == np.ndarray: |
| 37 | + sum_arr = arr |
| 38 | + elif type(arr) == pd.DataFrame: |
| 39 | + sum_arr = arr.to_numpy() |
| 40 | + else: |
| 41 | + raise ValueError("Invalid input type: {}".format(type(arr))) |
| 42 | + |
| 43 | + for i in range(np.ndim(arr)): |
| 44 | + sum_arr = np.flip(np.cumsum(np.flip(sum_arr, i), i), i) if reverse else np.cumsum(sum_arr, i) |
| 45 | + |
| 46 | + if type(arr) == np.ndarray: |
| 47 | + return sum_arr |
| 48 | + elif type(arr) == pd.DataFrame: |
| 49 | + return pd.DataFrame(sum_arr) |
| 50 | + else: |
| 51 | + raise ValueError("Invalid input type: {}".format(type(arr))) |
| 52 | + |
| 53 | + |
| 54 | +def calc_binning_stats( |
| 55 | + in_md, |
| 56 | + sel_cols, |
| 57 | + bin_methods, |
| 58 | + bin_nums, |
| 59 | + cols_bin_boundaries, |
| 60 | + reverse_cumsum=False): |
| 61 | + cols_bin_stats = [] |
| 62 | + for i in range(len(sel_cols)): |
| 63 | + sel_col = sel_cols[i] |
| 64 | + bin_o, bins = binning(in_md, sel_col, bin_methods[i], bin_nums[i], cols_bin_boundaries.get(sel_col, None)) |
| 65 | + bin_num = len(bins) - 1 |
| 66 | + bin_prob_df = bin_o.value_counts(normalize=True).to_pandas().to_frame() |
| 67 | + bin_prob_df = bin_prob_df.reindex(range(bin_num), fill_value=0) |
| 68 | + bin_cumsum_prob_df = cumsum(bin_prob_df, reverse_cumsum) |
| 69 | + |
| 70 | + cols_bin_stats.append( |
| 71 | + { |
| 72 | + "name": sel_col, |
| 73 | + "bin_boundaries": ','.join(bins.astype(str)), |
| 74 | + "bin_prob": ','.join(bin_prob_df[bin_prob_df.columns[0]].to_numpy().astype(str)), |
| 75 | + "bin_cumsum_prob": ','.join(bin_cumsum_prob_df[bin_cumsum_prob_df.columns[0]].to_numpy().astype(str)) |
| 76 | + } |
| 77 | + ) |
| 78 | + |
| 79 | + return pd.DataFrame(cols_bin_stats) |
| 80 | + |
| 81 | + |
| 82 | +def calc_basic_stats( |
| 83 | + in_md, |
| 84 | + sel_cols): |
| 85 | + stats_data = [ |
| 86 | + { |
| 87 | + "name": sel_col, |
| 88 | + "min": mt.min(in_md[sel_col]).to_numpy(), |
| 89 | + "max": mt.max(in_md[sel_col]).to_numpy(), |
| 90 | + "mean": mt.mean(in_md[sel_col]).to_numpy(), |
| 91 | + "median": mt.median(in_md[sel_col]).to_numpy(), |
| 92 | + "std": mt.std(in_md[sel_col]).to_numpy(), |
| 93 | + } for sel_col in sel_cols |
| 94 | + ] |
| 95 | + |
| 96 | + return pd.DataFrame(stats_data) |
| 97 | + |
| 98 | + |
| 99 | +def calc_stats( |
| 100 | + in_md, |
| 101 | + sel_cols, |
| 102 | + bin_methods, |
| 103 | + bin_nums, |
| 104 | + cols_bin_boundaries, |
| 105 | + reverse_cumsum=False): |
| 106 | + basic_stats_df = calc_basic_stats(in_md, sel_cols) |
| 107 | + cols_bin_stats_df = calc_binning_stats(in_md, sel_cols, bin_methods, bin_nums, cols_bin_boundaries, reverse_cumsum) |
| 108 | + |
| 109 | + stats_df = pd.merge(basic_stats_df, cols_bin_stats_df, how='inner', on='name') |
| 110 | + |
| 111 | + return stats_df |
| 112 | + |
0 commit comments