-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcohort.py
101 lines (74 loc) · 2.94 KB
/
cohort.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# PART ONE
# Important libraries
import pandas as pd
data = pd.read_csv("cohorts.csv")
print(data.head())
# Checking if there's any missing values or null values
missing_values = data.isnull().sum()
print(missing_values)
# Data types of the different columns
data_types = data.dtypes
print(data_types)
# Convert 'Date' column to datetime format
data['Date'] = pd.to_datetime(data['Date'], format='%d/%m/%Y')
# Display the descriptive statistics of the dataset
descriptive_stats = data.describe()
print(descriptive_stats)
# PART TWO OF THE ANALYSIS
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
pio.templates.default = "plotly_white"
# Trend analysis for New and Returning Users
fig = go.Figure()
# New Users
fig.add_trace(go.Scatter(x=data['Date'], y=data['New users'], mode='lines+markers', name='New Users'))
# Returning Users
fig.add_trace(go.Scatter(x=data['Date'], y=data['Returning users'], mode='lines+markers', name='Returning Users'))
# Update layout
fig.update_layout(title='Trend of New and Returning Users Over Time',
xaxis_title='Date',
yaxis_title='Number of Users')
fig.show()
fig = px.line(data_frame=data, x='Date', y=['Duration Day 1', 'Duration Day 7'], markers=True, labels={'value': 'Duration'})
fig.update_layout(title='Trend of Duration (Day 1 and Day 7) Over Time', xaxis_title='Date', yaxis_title='Duration', xaxis=dict(tickangle=-45))
fig.show()
# PART THREE
import seaborn as sns
import matplotlib.pyplot as plt
# Correlation matrix
correlation_matrix = data.corr()
# Plotting the correlation matrix
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
plt.title('Correlation Matrix of Variables')
plt.show()
# PART FOUR
# Grouping data by week
data['Week'] = data['Date'].dt.isocalendar().week
# Calculating weekly averages
weekly_averages = data.groupby('Week').agg({
'New users': 'mean',
'Returning users': 'mean',
'Duration Day 1': 'mean',
'Duration Day 7': 'mean'
}).reset_index()
print(weekly_averages.head())
fig1 = px.line(weekly_averages, x='Week', y=['New users', 'Returning users'], markers=True,
labels={'value': 'Average Number of Users'}, title='Weekly Average of New vs. Returning Users')
fig1.update_xaxes(title='Week of the Year')
fig1.update_yaxes(title='Average Number of Users')
fig2 = px.line(weekly_averages, x='Week', y=['Duration Day 1', 'Duration Day 7'], markers=True,
labels={'value': 'Average Duration'}, title='Weekly Average of Duration (Day 1 vs. Day 7)')
fig2.update_xaxes(title='Week of the Year')
fig2.update_yaxes(title='Average Duration')
fig1.show()
fig2.show()
# Creating a cohort matrix
cohort_matrix = weekly_averages.set_index('Week')
# Plotting the cohort matrix
plt.figure(figsize=(12, 8))
sns.heatmap(cohort_matrix, annot=True, cmap='coolwarm', fmt=".1f")
plt.title('Cohort Matrix of Weekly Averages')
plt.ylabel('Week of the Year')
plt.show()