Пример #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)
Пример #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)
Пример #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)
Пример #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)