diff --git a/toolkit.py b/toolkit.py index 9a0a445826615792eddf0000c3231ac5b1cc0468..8d368613e7c8c34b38772b856e0a634e4533185c 100755 --- a/toolkit.py +++ b/toolkit.py @@ -828,13 +828,19 @@ class ClassificationProject(object): hist, bins = np.histogram(x, **np_kwargs) centers = (bins[:-1] + bins[1:]) / 2 if "weights" in np_kwargs: - errors = [] - for left, right in zip(bins, bins[1:]): - indices = np.where((x >= left) & (x < right))[0] - sumw2 = np.sum(np_kwargs["weights"][indices]**2) - content = np.sum(np_kwargs["weights"][indices]) - errors.append(math.sqrt(sumw2)/content) - errors = np.array(errors) + bin_indices = np.digitize(x, bins) + sumw2 = np.array([np.sum(np_kwargs["weights"][bin_indices==i]**2) + for i in range(1, len(bins)+1)]) + sumw = np.array([np.sum(np_kwargs["weights"][bin_indices==i]) + for i in range(1, len(bins)+1)]) + # move overflow to last bin + # (since thats what np.histogram gives us) + sumw2[-2] += sumw2[-1] + sumw2 = sumw2[:-1] + sumw[-2] += sumw[-1] + sumw = sumw[:-1] + # calculate relative error + errors = np.sqrt(sumw2)/sumw else: errors = np.sqrt(hist)/hist if scale_factor is not None: