Skip to content

Instantly share code, notes, and snippets.

from collections import defaultdict
n=len(channels)
shapley_values = defaultdict(int)
for channel in channels:
for A in v_values.keys():
if channel not in A.split(","):
cardinal_A=len(A.split(","))
A_with_channel = A.split(",")
# First, let's convert the dataframe "subsets_conversions" into a dictionnary
C_values = user_logs_aggr.set_index("channels").to_dict()["conversions"]
#For each possible combination of channels A, we compute the total number of conversions yielded by every subset of A.
# Example : if A = {c1,c2}, then v(A) = C({c1}) + C({c2}) + C({c1,c2})
v_values = {}
for A in subsets(channels):
v_values[A] = v_function(A,C_values)
@RedaAffane
RedaAffane / coalition_worth.sql
Last active March 6, 2018 09:36
Compute the worth of each coalition
SELECT "channels_subset",
sum("conversion") as "conversions_sum"
FROM
(
SELECT "user_id",
string_agg(DISTINCT("channel"), ',') as "channels_subset",
max("conversion") as "conversion"
FROM
(
SELECT "user_id",