def test_group_add_bin(): # original group_add obj = np.random.randn(10, 1) lab = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 2], dtype=np.int32) cts = np.array([3, 3, 4], dtype=np.int32) exp = np.zeros((3, 1), np.float64) lib.group_add(exp, cts, obj, lab) # bin-based group_add bins = np.array([3, 6], dtype=np.int32) out = np.zeros((3, 1), np.float64) counts = np.zeros(len(out), dtype=np.int32) lib.group_add_bin(out, counts, obj, bins) assert_almost_equal(out, exp)
def algo4(): rizer = lib.DictFactorizer() labs1, _ = rizer.factorize(labels, sort=False) k1 = len(rizer.uniques) rizer = lib.DictFactorizer() labs2, _ = rizer.factorize(labels2, sort=False) k2 = len(rizer.uniques) group_id = labs1 * k2 + labs2 max_group = k1 * k2 if max_group > 1e6: rizer = lib.Int64Factorizer(len(group_id)) group_id, _ = rizer.factorize(group_id.astype('i8'), sort=True) max_group = len(rizer.uniques) out = np.empty(max_group) counts = np.zeros(max_group, dtype='i4') lib.group_add(out, counts, data, group_id)
def f2(): rizer = lib.DictFactorizer() labs, counts = rizer.factorize(xarr, sort=False) k = len(rizer.uniques) out = np.empty(k) lib.group_add(out, counts, data, labs)
def algo3_sort(): rizer = lib.DictFactorizer() labs, counts = rizer.factorize(labels, sort=True) k = len(rizer.uniques) out = np.empty(k) lib.group_add(out, counts, data, labs)