def curveFromBin(binValues, pb, delta=1): ''' binValues: 经排序的数值列表 ''' N = len(binValues) binCumulateCount = np.array(range(1, N + 1)) li = Interpolate(binCumulateCount, binValues, delta) cnt = pb * N / 100 bin = li.evalArray(cnt, sorted=True) return CumulativeCurve(bin, cnt)
def merge(curves, pb): """ curves: 元素为(bins,counts)元组的列表 """ binList = [] for c in curves: binList.append(c[0]) bs = np.unique(np.ravel(np.array(binList))) bsLen = len(bs) binCounts = np.zeros(bsLen) for cbin, cCount in curves: li = Interpolate(cbin, cCount, 0) cs = li.evalArray(bs, sorted=True) binCounts = binCounts + cs newTotal = binCounts[-1] li = Interpolate(binCounts * 100 / newTotal, bs, 0) newbin = li.evalArray(pb, sorted=True) return CumulativeCurve(newbin, pb * newTotal / 100)
def curveFromBinCount(binCounts, pb, delta=1): ''' binCounts: 元素为(bin,count)元组的列表,可以是多次累积,bin可以无序 ''' from collections import Counter c0 = Counter() for bin, cnt in binCounts: c0[bin] += cnt c1 = sorted(c0.items(), key=lambda item: item[0]) clen = len(c1) if c1[0][1] == 0: b = np.zeros(clen) binCumulateCount = np.zeros(clen) b[0] = c1[0][0] binCumulateCount[0] = 0 for i in range(1, clen - 1): b[i] = b[i - 1] + 2 * (c1[i][0] - b[i - 1]) binCumulateCount[i] = binCumulateCount[i - 1] + c1[i][1] b[-1] = c1[-1][0] binCumulateCount[-1] = binCumulateCount[-2] else: b = np.zeros(clen + 1) binCumulateCount = np.zeros(clen + 1) b[0] = c1[0][0] - delta binCumulateCount[0] = 0 for i in range(clen): b[i + 1] = c1[i][0] binCumulateCount[i + 1] = binCumulateCount[i] + c1[i][1] N = binCumulateCount[-1] li = Interpolate(binCumulateCount, b, 0) cnt = pb * N / 100 bin = li.evalArray(cnt, sorted=True) return CumulativeCurve(bin, cnt)