Example #1
0
def gen_user_get_shop_coupon(offline_source, online_source, X):
    offline = Matrix(
        np.genfromtxt(paths.ccf_path + offline_source,
                      delimiter=',',
                      dtype=str),
        ["uid", "mid", "cid", "dis_rate", "dist", "date_rec", "date"],
        ["%s" for i in xrange(7)])
    online = Matrix(
        np.genfromtxt(paths.ccf_path + online_source, delimiter=',',
                      dtype=str),
        ["uid", "mid", "act", "cid", "dis_rate", "date_rec", "date"],
        ["%s" for i in xrange(7)])

    user_get_shop_coupon = HashSet()
    for i in xrange(offline.ndata):
        uid_str = offline.get_cell(i, "uid")
        cid_str = offline.get_cell(i, "cid")
        mid_str = offline.get_cell(i, "mid")
        if cid_str != 'null':
            if not user_get_shop_coupon.has(uid_str):
                user_get_shop_coupon.set(uid_str, HashSet())
            user_get_shop_coupon.get(uid_str).add_one(mid_str)
    for i in xrange(online.ndata):
        uid_str = online.get_cell(i, "uid")
        cid_str = online.get_cell(i, "cid")
        mid_str = online.get_cell(i, "mid")
        act_str = online.get_cell(i, "act")
        if cid_str != 'null' and act_str != '0':
            if not user_get_shop_coupon.has(uid_str):
                user_get_shop_coupon.set(uid_str, HashSet())
            user_get_shop_coupon.get(uid_str).add_one(mid_str)
    X.join_by_double_key("uid",
                         "mid",
                         "user_get_shop_coupon",
                         user_get_shop_coupon,
                         "%s",
                         dft=0.0)

    def divide(x, y):
        assert float(x) <= float(y)
        if float(y) == 0:
            return 0.0
        else:
            return float(x) * 1.0 / float(y)

    X.gen_arith_feature("user_buy_with_coupon_in_shop",
                        "user_get_shop_coupon",
                        "user_use_shop_coupon_freq",
                        divide,
                        "%s",
                        dft=0.0)
Example #2
0
def gen_user_buy_in_shop(matrix_offline, matrix_online, X):
    user_buy_in_shop = HashSet(default=matrix_offline.default)
    for i in xrange(matrix_offline.ndata):
        date_str = matrix_offline.get_cell(i, 'date')
        uid_str = matrix_offline.get_cell(i, 'uid')
        mid_str = matrix_offline.get_cell(i, "mid")
        if date_str != 'null':
            if not user_buy_in_shop.has(uid_str):
                user_buy_in_shop.set(uid_str, HashSet())
            user_buy_in_shop.get(uid_str).add_one(mid_str)
    for i in xrange(matrix_online.ndata):
        act_str = matrix_online.get_cell(i, 'act')
        uid_str = matrix_online.get_cell(i, 'uid')
        mid_str = matrix_online.get_cell(i, "mid")
        if act_str == '1':
            if not user_buy_in_shop.has(uid_str):
                user_buy_in_shop.set(uid_str, HashSet())
            user_buy_in_shop.get(uid_str).add_one(mid_str)
    X.join_by_double_key("uid",
                         "mid",
                         "user_buy_in_shop",
                         user_buy_in_shop,
                         "%s",
                         dft=0.0)

    def divide(x, y):
        assert float(x) <= float(y)
        if float(y) == 0:
            return 0
        else:
            return float(x) * 1.0 / float(y)

    X.gen_arith_feature("user_buy_in_shop",
                        "user_buy",
                        "user_buy_in_shop_ratio",
                        divide,
                        fmt="%s",
                        dft=0.0)
Example #3
0
def gen_merchant_share(X, month):
    offline = Matrix(
        np.genfromtxt(paths.ccf_path + 'offline_train_{0}.csv'.format(month),
                      delimiter=',',
                      dtype=str),
        ["uid", "mid", "cid", "dis_rate", "dist", "date_rec", "date"],
        ["%s" for i in xrange(7)])
    online = Matrix(
        np.genfromtxt(paths.ccf_path + 'online_train_{0}.csv'.format(month),
                      delimiter=',',
                      dtype=str),
        ["uid", "mid", "act", "cid", "dis_rate", "date_rec", "date"],
        ["%s" for i in xrange(7)])

    merchant_user_buy = HashSet()
    merchant_user_use_coupon = HashSet()
    merchant_user_buy_counter = HashSet()
    merchant_user_use_coupon_counter = HashSet()
    for i in xrange(offline.ndata):
        if i % 100000 == 0:
            print i
        mid_str = offline.get_cell(i, "mid")
        uid_str = offline.get_cell(i, "uid")
        date_str = offline.get_cell(i, "date")
        cid_str = offline.get_cell(i, "cid")
        if date_str != 'null':
            if not merchant_user_buy.has(mid_str):
                merchant_user_buy.set(mid_str, HashSet())
            if not merchant_user_buy.get(mid_str).has(uid_str):
                merchant_user_buy.get(mid_str).add_one(uid_str)
                merchant_user_buy_counter.add_one(mid_str)
            if cid_str != 'null':
                if not merchant_user_use_coupon.has(mid_str):
                    merchant_user_use_coupon.set(mid_str, HashSet())
                if not merchant_user_use_coupon.get(mid_str).has(uid_str):
                    merchant_user_use_coupon.get(mid_str).add_one(uid_str)
                    merchant_user_use_coupon_counter.add_one(mid_str)
    for i in xrange(online.ndata):
        if i % 100000 == 0:
            print i
        mid_str = online.get_cell(i, "mid")
        uid_str = online.get_cell(i, "uid")
        act_str = online.get_cell(i, "act")
        cid_str = online.get_cell(i, "cid")
        if act_str == '1':
            if not merchant_user_buy.has(mid_str):
                merchant_user_buy.set(mid_str, HashSet())
            if not merchant_user_buy.get(mid_str).has(uid_str):
                merchant_user_buy.get(mid_str).add_one(uid_str)
                merchant_user_buy_counter.add_one(mid_str)
            if cid_str != 'null':
                if not merchant_user_use_coupon.has(mid_str):
                    merchant_user_use_coupon.set(mid_str, HashSet())
                if not merchant_user_use_coupon.get(mid_str).has(uid_str):
                    merchant_user_use_coupon.get(mid_str).add_one(uid_str)
                    merchant_user_use_coupon_counter.add_one(mid_str)
    X.join("mid", ["merchant_user_buy"],
           merchant_user_buy_counter, ["%s"],
           dft=0.0)
    X.join("mid", ["merchant_user_use_coupon"],
           merchant_user_use_coupon_counter, ["%s"],
           dft=0.0)
    X.check_point(month)
Example #4
0
def gen_user_buy_coupon_in_shop(matrix_offline, matrix_online, X):
    user_buy_with_coupon_in_shop = HashSet()
    user_buy_without_coupon_in_shop = HashSet()
    for i in xrange(matrix_offline.ndata):
        uid_str = matrix_offline.get_cell(i, "uid")
        cid_str = matrix_offline.get_cell(i, "cid")
        date_str = matrix_offline.get_cell(i, "date")
        mid_str = matrix_offline.get_cell(i, "mid")
        if date_str != 'null':
            if not user_buy_with_coupon_in_shop.has(uid_str):
                user_buy_with_coupon_in_shop.set(uid_str, HashSet())
                user_buy_without_coupon_in_shop.set(uid_str, HashSet())
            if cid_str != 'null':
                user_buy_with_coupon_in_shop.get(uid_str).add_one(mid_str)
            else:
                user_buy_without_coupon_in_shop.get(uid_str).add_one(mid_str)

    for i in xrange(matrix_online.ndata):
        uid_str = matrix_online.get_cell(i, "uid")
        cid_str = matrix_online.get_cell(i, "cid")
        mid_str = matrix_online.get_cell(i, "mid")
        act_str = matrix_online.get_cell(i, "act")
        if act_str == '1':
            if not user_buy_with_coupon_in_shop.has(uid_str):
                user_buy_with_coupon_in_shop.set(uid_str, HashSet())
                user_buy_without_coupon_in_shop.set(uid_str, HashSet())
            if cid_str != 'null':
                user_buy_with_coupon_in_shop.get(uid_str).add_one(mid_str)
            else:
                user_buy_without_coupon_in_shop.get(uid_str).add_one(mid_str)

    X.join_by_double_key("uid",
                         "mid",
                         "user_buy_with_coupon_in_shop",
                         user_buy_with_coupon_in_shop,
                         "%s",
                         dft=0.0)
    X.join_by_double_key("uid",
                         "mid",
                         "user_buy_without_coupon_in_shop",
                         user_buy_without_coupon_in_shop,
                         "%s",
                         dft=0.0)

    def divide(x, y):
        assert float(x) <= float(y)
        if float(y) == 0:
            return 0
        else:
            return float(x) * 1.0 / float(y)

    X.gen_arith_feature("user_buy_with_coupon_in_shop",
                        "user_buy_in_shop",
                        "user_buy_with_coupon_in_shop_ratio",
                        divide,
                        "%s",
                        dft=0.0)
    X.gen_arith_feature("user_buy_without_coupon_in_shop",
                        "user_buy_in_shop",
                        "user_buy_without_coupon_in_shop_ratio",
                        divide,
                        "%s",
                        dft=0.0)
Example #5
0
def gen_coupon_gap(X, month):
    get_coupon_history = HashSet()
    get_coupon_history_user = HashSet()
    for i in xrange(X.ndata):
        if i % 100000 == 0:
            print i
        uid_str = X.get_cell(i, "uid")
        mid_str = X.get_cell(i, "mid")
        date_rec_str = X.get_cell(i, "date_rec")
        if date_rec_str != 'null':
            history_list = get_coupon_history.get(uid_str,
                                                  HashSet()).get(mid_str, [])
            #if date_rec_str not in history_list:
            history_list.append(date_rec_str)
            user_history_list = get_coupon_history_user.get(uid_str, [])
            #if date_rec_str not in user_history_list:
            user_history_list.append(date_rec_str)
    if month > 1:
        last_X = Matrix(
            np.genfromtxt(paths.ccf_path +
                          'offline_1_month{0}.csv'.format(month - 1),
                          delimiter=',',
                          dtype=str),
            ["uid", "mid", "cid", "dis_rate", "dist", "date_rec", "date"],
            ["%s" for i in xrange(7)])
        for i in xrange(last_X.ndata):
            if i % 100000 == 0:
                print i
            uid_str = last_X.get_cell(i, "uid")
            mid_str = last_X.get_cell(i, "mid")
            date_rec_str = last_X.get_cell(i, "date_rec")
            if date_rec_str != 'null':
                history_list = get_coupon_history.get(uid_str, HashSet()).get(
                    mid_str, [])
                #if date_rec_str not in history_list:
                history_list.append(date_rec_str)
                user_history_list = get_coupon_history_user.get(uid_str, [])
                #if date_rec_str not in user_history_list:
                user_history_list.append(date_rec_str)
    if month < 7:
        next_X = Matrix(
            np.genfromtxt(paths.ccf_path +
                          'offline_1_month{0}.csv'.format(month + 1),
                          delimiter=',',
                          dtype=str),
            ["uid", "mid", "cid", "dis_rate", "dist", "date_rec", "date"],
            ["%s" for i in xrange(7)])
        for i in xrange(next_X.ndata):
            if i % 100000 == 0:
                print i
            uid_str = next_X.get_cell(i, "uid")
            mid_str = next_X.get_cell(i, "mid")
            date_rec_str = next_X.get_cell(i, "date_rec")
            if date_rec_str != 'null':
                history_list = get_coupon_history.get(uid_str, HashSet()).get(
                    mid_str, [])
                #if date_rec_str not in history_list:
                history_list.append(date_rec_str)
                user_history_list = get_coupon_history_user.get(uid_str, [])
                #if date_rec_str not in user_history_list:
                user_history_list.append(date_rec_str)
    for i in xrange(X.ndata):
        if i % 100000 == 0:
            print i
        uid_str = X.get_cell(i, "uid")
        mid_str = X.get_cell(i, "mid")
        history_list = get_coupon_history.get(uid_str,
                                              HashSet()).get(mid_str, [])
        get_coupon_history.get(uid_str).set(mid_str, np.sort(history_list))
        user_history_list = get_coupon_history_user.get(uid_str, [])
        get_coupon_history_user.set(uid_str, np.sort(user_history_list))
    col_names = [
        "prev_gap", "next_gap", "user_prev_gap", "user_next_gap", "in_between",
        "prev_gap_prev", "next_gap_prev"
    ]
    gaps = np.zeros((X.ndata, len(col_names)))

    for i in xrange(X.ndata):
        if i % 100000 == 0:
            print i
        uid_str = X.get_cell(i, "uid")
        mid_str = X.get_cell(i, "mid")
        date_rec_str = X.get_cell(i, "date_rec")
        history_list = get_coupon_history.get(uid_str).get(mid_str)
        user_history_list = get_coupon_history_user.get(uid_str)
        k = -1
        for j in xrange(len(history_list)):
            ##if history_list[j] == date_rec_str:
            if j == len(history_list) - 1 or history_list[j +
                                                          1] > date_rec_str:
                break
            else:
                k += 1
        in_between = 1
        if k > 0:
            prev_gap = days_dis(history_list[k], date_rec_str)
        else:
            prev_gap = 0
            in_between = 0
        if k + 2 < len(history_list):
            next_gap = days_dis(date_rec_str, history_list[k + 2])
        else:
            next_gap = 0
            in_between = 0

        k = -1
        for j in xrange(len(user_history_list)):
            ##if user_history_list[j] == date_rec_str:
            if j == len(history_list) - 1 or history_list[j +
                                                          1] > date_rec_str:
                break
            else:
                k += 1
        if k > 0:
            user_prev_gap = days_dis(user_history_list[k], date_rec_str)
        else:
            user_prev_gap = 0
        if k + 2 < len(user_history_list):
            user_next_gap = days_dis(date_rec_str, user_history_list[k + 2])
        else:
            user_next_gap = 0

        k = -1
        for j in xrange(len(history_list)):
            if history_list[j] == date_rec_str:
                ##if j == len(history_list) - 1 or history_list[j + 1] > date_rec_str:
                break
            else:
                k += 1
        if k > 0:
            prev_gap_prev = days_dis(history_list[k], date_rec_str)
        else:
            prev_gap_prev = 0
        if k + 2 < len(history_list):
            next_gap_prev = days_dis(date_rec_str, history_list[k + 2])
        else:
            next_gap_prev = 0
            in_between = 0

        gaps[i, :] = np.array([
            prev_gap, next_gap, user_prev_gap, user_next_gap, in_between,
            prev_gap_prev, next_gap_prev
        ])
    X.cat_col(gaps, col_names, ["%s" for i in xrange(len(col_names))])
    X.check_point("X_{0}_{1}{2}_gap".format(month, 1, month - 1))