Skip to content

Commit

Permalink
add hist mock
Browse files Browse the repository at this point in the history
Signed-off-by: weiwee <wbwmat@gmail.com>
  • Loading branch information
sagewe committed Jul 21, 2023
1 parent ff78d28 commit 0221c70
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/fate/arch/tensor/inside/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from ._op_hist import Hist
from ._op_quantile import GKSummary
45 changes: 45 additions & 0 deletions python/fate/arch/tensor/inside/_op_hist.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0221c70

Please sign in to comment.