-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: weiwee <wbwmat@gmail.com>
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from ._op_hist import Hist | ||
from ._op_quantile import GKSummary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |