Skip to content

Instantly share code, notes, and snippets.

@dmitrysarov
Last active October 7, 2019 13:53
Show Gist options
  • Save dmitrysarov/1e0aa79d4981ee0ef4e36e65502d4239 to your computer and use it in GitHub Desktop.
Save dmitrysarov/1e0aa79d4981ee0ef4e36e65502d4239 to your computer and use it in GitHub Desktop.
If data is too large, this class can iteratively absorb data
import numpy as np
import matplotlib.pyplot as plt
class Iterative_hist(object):
'''
Collect distribution information in iterative manner.
Useful when whole data not fit memory.
Usage:
if min and max data value is not known, call set_min_max() method, iterative, on hall dataset
call add_data() method, for data addition
'''
def __init__(self, bins=1000, min_value=None, max_value=None):
self.min_value = min_value
self.max_value = max_value
if min_value and max_value:
self.hist_bins = np.linspace(self.min, self.max+(self.max-self.min)/self.bins, self.bins)
self.bins = bins
self.hist = np.zeros(bins)
# self.state = False #not updates
def update_min_max(self, data):
'''
update data minimum and maximum values
'''
min_value = np.min(data)
max_value = np.max(data)
if not self.min_value or min_value < self.min_value:
self.min_value = min_value
if not self.max_value or max_value > self.max_value:
self.max_value = max_value
# self.state = True #updated
def update_hist(self, data):
'''
update histogramm
'''
assert self.min_value is not None and self.max_value is not None, ' Minimum and maximum values should be defined, given {}, {}. Call update_min_max()'.format(self.min_value, self.max_value)
# if self.state:
self.hist_bins = np.linspace(self.min_value, self.max_value+(self.max_value-self.min_value)/self.bins, self.bins)
# self.state = False
self.hist += np.bincount(np.digitize(data, self.hist_bins), minlength=self.bins)
def plot(self, log_scale=False):
plt.bar(self.hist_bins, self.hist, width=self.hist_bins[1]-self.hist_bins[0])
if log_scale:
plt.yscale('log')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment