Merging bins

[1]:
from physt.binnings import *
from physt import h1, h2
import numpy as np

np.random.seed(42)

%matplotlib inline
[2]:
data = np.random.rand(100)
[3]:
hh = h1(data, 120)
hh.plot(errors=True);
_images/merge-bins_3_0.png
[4]:
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
_images/merge-bins_4_0.png
[5]:
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
_images/merge-bins_5_0.png
[6]:
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
_images/merge-bins_6_0.png
[7]:
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
_images/merge-bins_7_0.png
[8]:
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
_images/merge-bins_8_0.png
[9]:
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
_images/merge-bins_9_0.png

By min frequency

[10]:
data = np.random.normal(0, 1, 5000)
hh = h1(data, 120)
hh.plot();
_images/merge-bins_11_0.png
[11]:
hh.merge_bins(min_frequency=100, inplace=True)
hh.plot(density=True);
_images/merge-bins_12_0.png
[12]:
hh.merge_bins(min_frequency=600, inplace=True)
hh.plot(density=True);
_images/merge-bins_13_0.png

The same can be done for 2D histograms (i.e. each column, each row should contain more than the minimum). Unfortunately, a general, irregular-shaped binning is not yet supported.

[13]:
# 2D example
data1 = np.random.normal(0, 1, 600)
data2 = np.random.rand(600)
[14]:
hh = h2(data1, data2, 23)
ax = hh.plot(show_zero=0, cmap="rainbow", show_colorbar=False);
ax.set_title("Before merging")
hh.merge_bins(min_frequency=30, inplace=True)
ax = hh.plot(density=True, show_zero=False, cmap="rainbow", show_colorbar=False)
ax.set_title("After merging");
_images/merge-bins_16_0.png
_images/merge-bins_16_1.png