forked from MichiganDataScienceTeam/googleanalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore_utils.py
46 lines (31 loc) · 1.25 KB
/
explore_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
def find_most_visit(dataset):
"""Find what is the most visited times of single customer.(only in train set)
args:
dataset (Dataset): the google analytics dataset.
returns:
The most visited times in trainset.
"""
train_df = dataset.train.copy()
train_gdf = train_df.groupby("fullVisitorId")[
'visitNumber'].sum().reset_index()
max_visit = train_gdf['visitNumber'].max()
return max_visit
def find_customer_revenue_percentiles(
dataset,
percentiles=[95, 99, 99.9, 99.99]):
"""Find percentiles of the per-customer revenue.
args:
dataset (Dataset): the google analytics dataset
percentiles (list of floats): the percentiles to find
returns:
The values of the per-customer revenue at the given
percentiles in the training set.
"""
train_df = dataset.train
train_df['revenue'] = train_df['totals.transactionRevenue'].astype(float)
revenue_per_customer = train_df.groupby('fullVisitorId')['revenue']
total_revenue_per_customer = revenue_per_customer.sum().fillna(0) / 10000.0
values = [np.percentile(total_revenue_per_customer.values, percentile)
for percentile in percentiles]
return values