Created
April 14, 2023 12:17
-
-
Save Pangoraw/cca9a49c8f9b40041ff51662b279300c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import math | |
class ZeroToOneBinner: | |
def __init__(self): | |
self.bins = 8 | |
self.counts = [0 for _ in range(self.bins)] | |
self.total = 0 | |
def update(self, values): | |
for i in range(self.bins): | |
pv = i / self.bins | |
v = (i + 1) / self.bins | |
self.counts[i] += ((pv <= values) & (values < v)).sum().item() | |
self.total += values.shape[0] | |
def show(self): | |
chars = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] | |
m = max(max(self.counts), 1) | |
importance = [c / m for c in self.counts] | |
print(" ", end="") | |
for imp in importance: | |
cidx = math.floor(imp * (self.bins - 0.001)) | |
c = chars[cidx] | |
print(c, end="") | |
print() | |
if __name__ == "__main__": | |
import numpy as np | |
binner = ZeroToOneBinner() | |
binner.update(0.5 + 0.1 * np.random.randn(1000)) | |
binner.show() # ▁▁▂██▃▁▁ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment