From 0221c7050970e64bfd6215010eb6c7a4a435b09e Mon Sep 17 00:00:00 2001 From: weiwee Date: Fri, 14 Jul 2023 14:22:06 +0800 Subject: [PATCH] add hist mock Signed-off-by: weiwee --- python/fate/arch/tensor/inside/__init__.py | 1 + python/fate/arch/tensor/inside/_op_hist.py | 45 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 python/fate/arch/tensor/inside/_op_hist.py diff --git a/python/fate/arch/tensor/inside/__init__.py b/python/fate/arch/tensor/inside/__init__.py index fdbbfc3879..70d990614c 100644 --- a/python/fate/arch/tensor/inside/__init__.py +++ b/python/fate/arch/tensor/inside/__init__.py @@ -1 +1,2 @@ +from ._op_hist import Hist from ._op_quantile import GKSummary diff --git a/python/fate/arch/tensor/inside/_op_hist.py b/python/fate/arch/tensor/inside/_op_hist.py new file mode 100644 index 0000000000..a2ddb7823f --- /dev/null +++ b/python/fate/arch/tensor/inside/_op_hist.py @@ -0,0 +1,45 @@ +class Hist: + def __init__(self): + self.data = {} + + def update(self, features, labels): + shape_x, shape_y = features.shape + for i in range(shape_x): + for j in range(shape_y): + v = features[i, j] + if j not in self.data: + self.data[j] = {} + if v not in self.data[j]: + self.data[j][v] = labels[i] + else: + self.data[j][v] += labels[i] + + def merge(self, hist): + for k in hist.data: + if k not in self.data: + self.data[k] = hist.data[k] + else: + for kk in hist.data[k]: + if kk not in self.data[k]: + self.data[k][kk] = hist.data[k][kk] + else: + self.data[k][kk] += hist.data[k][kk] + return self + + def cumsum(self): + for k in self.data: + s = 0 + for kk in sorted(self.data[k].keys()): + s += self.data[k][kk] + self.data[k][kk] = s + return self + + +if __name__ == "__main__": + import numpy as np + + hist = Hist() + features = np.array([[1, 0], [0, 1], [2, 1], [2, 0]]) + labels = np.array([0, 1, 0, 0]) + hist.update(features, labels) + print(hist.data)