In [298]:
request = "POST https://analyticsreporting.googleapis.com/v4/reports:batchGet?fields=reports(columnHeader%2Cdata(rows%2Ctotals))&key={YOUR_API_KEY}"
request = {
"reportRequests": [
{
"viewId": "123303369",
"dateRanges": [
{
"startDate": "2017-01-01",
"endDate": "2017-04-30"
}
],
"metrics": [
{
"expression": "ga:sessions"
},
{
"expression": "ga:sessionDuration"
},
{
"expression": "ga:goal1Completions"
},
{
"expression": "ga:bounceRate"
}
],
"dimensions": [
{
"name": "ga:city"
},
{
"name": "ga:userAgeBracket"
}
]
}
]
}
In [367]:
import json
with open('data/TMRW_users.json') as file:
input_ugroups = json.load(file)
#input_ugroups
# Define dimensions list
input_ugroups_dimensions = input_ugroups['reports'][0]['columnHeader']['dimensions']
dimension_count = len(input_ugroups_dimensions)
# Define metrics list
input_ugroups_metrics = input_ugroups['reports'][0]['columnHeader']['metricHeader']['metricHeaderEntries']
def create_metric_list(raw_data):
lst = []
for item in raw_data:
lst.append(item['name'])
return lst
input_ugroups_metrics = create_metric_list(input_ugroups_metrics)
# Create input data
input_ugroups_data = input_ugroups['reports'][0]['data']['rows']
input_ugroups_data
Out[367]:
In [368]:
values_list = []
for group in input_ugroups_data:
new_dim_name = group['dimensions'][0] + ", " + group['dimensions'][1]
group[new_dim_name] = group['metrics'][0]
del group['dimensions']
del group['metrics']
conv_rate = round(float(int(group[new_dim_name]['values'][2])/int(group[new_dim_name]['values'][0])*100),2)
values_list.append(group[new_dim_name]['values'])
group[new_dim_name]['values'].append(conv_rate)
#values_list
input_ugroups_data
Out[368]:
In [369]:
# Define each metric dict
ugroups_data = {}
for ugroup in input_ugroups_data:
#print (ugroup)
for gr in ugroup:
ugroups_data[gr] = {'sessions':0,
'duration':0,
'bounce rate':0,
'conversions':0,
'conversion rate':0}
ugroups_data[gr]['sessions'] = int(ugroup[gr]['values'][0])
ugroups_data[gr]['duration'] = float(ugroup[gr]['values'][1])
ugroups_data[gr]['conversions'] = int(ugroup[gr]['values'][2])
ugroups_data[gr]['bounce rate'] = float(ugroup[gr]['values'][3])
ugroups_data[gr]['conversion rate'] = float(ugroup[gr]['values'][4])
#ugroups_data
In [370]:
rows = list(ugroups_data.keys())
rows
Out[370]:
In [371]:
import collections
from collections import OrderedDict
columns = []
for u in ugroups_data:
#print (test[r])
for metric in ugroups_data[u]:
columns.append(metric)
columns = list(OrderedDict.fromkeys(columns))
columns
Out[371]:
In [233]:
import pandas as pd
In [374]:
df = pd.DataFrame(values_list,
index = rows,
columns = columns)
df.to_json(orient='split')
pd.read_json(_, orient='split')
Out[374]:
In [ ]:
In [ ]:
In [ ]:
In [14]:
import numpy as np
def cluster_points(X, mu):
clusters = {}
for x in X:
bestmukey = min([(i[0], np.linalg.norm(x-mu[i[0]])) \
for i in enumerate(mu)], key=lambda t:t[1])[0]
try:
clusters[bestmukey].append(x)
except KeyError:
clusters[bestmukey] = [x]
return clusters
def reevaluate_centers(mu, clusters):
newmu = []
keys = sorted(clusters.keys())
for k in keys:
newmu.append(np.mean(clusters[k], axis = 0))
return newmu
def has_converged(mu, oldmu):
return set([tuple(a) for a in mu]) == set([tuple(a) for a in oldmu])
def find_centers(X, K):
# Initialize to K random centers
oldmu = random.sample(X, K)
mu = random.sample(X, K)
while not has_converged(mu, oldmu):
oldmu = mu
# Assign all points in X to clusters
clusters = cluster_points(X, mu)
# Reevaluate centers
mu = reevaluate_centers(oldmu, clusters)
return(mu, clusters)
In [15]:
import random
def init_board(N):
X = np.array([(random.uniform(-1, 1), random.uniform(-1, 1)) for i in range(N)])
return X
In [16]:
def init_board_gauss(N, k):
n = float(N)/k
X = []
for i in range(k):
c = (random.uniform(-1, 1), random.uniform(-1, 1))
s = random.uniform(0.05,0.5)
x = []
while len(x) < n:
a, b = np.array([np.random.normal(c[0], s), np.random.normal(c[1], s)])
# Continue drawing points from the distribution in the range [-1,1]
if abs(a) < 1 and abs(b) < 1:
x.append([a,b])
X.extend(x)
X = np.array(X)[:N]
return X
In [4]:
import pybrain
dir(pybrain)
In [ ]: