def stack_method():
    if "mean" in args.model:
        all_preds = [
            "data/te.ins5.ffm.out",
            "data/xgb5_test.out",
            "data/te.ins_bag.ffm.out",
            "data/te.ins5.fm.out",
            "data/te.ins3.ffm.out",
            "data/te.ins20.ffm.out",
            "data/xgb3_test.out",
            "data/te.ins.ffm.out",
        ]
    else:
        all_preds = [
            "data/te.ins5.ffm.out",
            "data/xgb5_test.out",
            "data/te.ins_bag.ffm.out",
            "data/te.ins5.fm.out",
            "data/te.ins3.ffm.out",
            "data/te.ins20.ffm.out",
            "data/xgb3_test.out",

            "data/te.ins.ffm.out",
            "data/te.ins2.ffm.out",
        ]
    delta = 1
    now = datetime.now()
    X = [] 
    true_y = []
    for t, preds in enumerate(read_preds(all_preds)):
        if "nn" in args.model or "xgb" in args.model:
            preds = map(lambda x : relogit(x), preds)
        X.append(preds)
        true_y.append(0)
        if t == delta:
            print "%s: %s"%(t, datetime.now() - now)
            delta *= 2
    if "nn" in args.model:
        model = read_dump("%s_model.dump"%args.model)
        X = np.array(X).astype(np.float32)
        preds = model.predict_proba(X)[:, 1]
    elif "xgb" in args.model:
        dtrain = xgb.DMatrix(np.array(X), label=true_y)
        bst = xgb.Booster({'nthread':9})
        bst.load_model("%s_model.dump"%args.model)
        preds = bst.predict(dtrain)
    elif args.model == "mean":
        X = np.array(X).astype(np.float32)
        preds = np.mean(X, 1)
    with open("result_%s.csv"%args.model, 'w') as outfile:
        outfile.write('ID,IsClick\n')
        avg_p = 0
        cnt = 0
        for ID, p in izip(read_sample("data/sampleSubmission.csv"), preds):
            avg_p += p
            cnt += 1
            outfile.write('%s,%s\n' % (ID, str(p)))
        print cnt, avg_p / cnt
dataset/ETHPy150Open Gzsiceberg/kaggle-avito/ensemble/stack.py/stack_method/original