# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %matplotlib inline
import pandas as pd
import json
from pandas.io.json import json_normalize
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set(color_codes=True)
from pylab import plot, show, text
import datetime
import matplotlib.dates as mdates
import pylab

# +
# load as Pandas dataframe
df_users = pd.read_csv('takehome_users.csv', encoding='latin-1')

# look at basic column descriptions
df_users.describe()

# -

# put zero for NaN values
df_users = df_users.fillna(0)

# peek at the dataframe
df_users.head(3)

# what organization has the most users?
# ANSWER: most users belong to organization id: 1 -> 10 except 8
df_users.org_id.value_counts().head(10)

# who invited most users
df_users.invited_by_user_id.value_counts().head(10)

# who are these users who invited the most users , 11770
df_users[(df_users['object_id'] == 10741) | (df_users['object_id'] == 2527) | (df_users['object_id'] == 2308)| (df_users['object_id'] == 1525)| (df_users['object_id'] == 11770)]['name']

# check if there are blank email
print(len(df_users[df_users.email == None]))

# how many opted in to mailing list
df_users.opted_in_to_mailing_list.value_counts()

# +
# Create a pie chart

# create dataframe for creation source
df_mail = df_users.opted_in_to_mailing_list.value_counts()
df_mail = df_mail.reset_index()

# Put parameter values
plt.pie(
    df_mail['opted_in_to_mailing_list'],
    labels=df_mail['index'],
    shadow=False,
    startangle=0,
    autopct='%1.1f%%',
    )

# Add title
plt.title('25% percent of the users has opted_in_to_mailing_list')
plt.axis('equal')

# Display plot 
plt.tight_layout()
plt.show()
# -

# how many enabled for marketing drip
df_users.enabled_for_marketing_drip.value_counts()


# +
# Create a pie chart

# create dataframe for creation source
df_drip = df_users.enabled_for_marketing_drip.value_counts()
df_drip = df_drip.reset_index()

# Put parameter values
plt.pie(
    df_drip['enabled_for_marketing_drip'],
    labels=df_drip['index'],
    shadow=False,
    startangle=0,
    autopct='%1.1f%%',
    )

# Add title
plt.title('15% of users have enabled_for_marketing_drip')
plt.axis('equal')

# Display plot
plt.tight_layout()
plt.show()
# -

#creation source distribution
df_users.creation_source.value_counts()

# +
# Create a pie chart

# create dataframe for creation source
df_source = df_users.creation_source.value_counts()
df_source = df_source.reset_index()

# Put parameter values
plt.pie(
    df_source['creation_source'],
    labels=df_source['index'],
    shadow=False,
    startangle=0,
    autopct='%1.1f%%',
    )

# Add title
plt.title('Percent distribution of creation source')
plt.axis('equal')

# Display plot
plt.tight_layout()
plt.show()
# -

# read file into dataframe
df_engage = pd.read_csv('takehome_user_engagement.csv')
df_engage.head(3)


# top 10 active users (may or may not be adopted users)
df_engage.user_id.value_counts().head(10)

# who are these top 3 users?
df_users[(df_users['object_id'] == 3623) | (df_users['object_id'] == 906) | (df_users['object_id'] == 1811)]


# +
# convert string time stamp into datetime
df_engage['time_stamp'] = pd.to_datetime(df_engage['time_stamp'])

# change index to time_stamp column for timegrouper function (later on)
df_engage.index = pd.to_datetime(df_engage.time_stamp, unit='D')


# +
# create dataframe with users that has logged into the product on three separate days in at least one sevenday period
df_adoption = df_engage.groupby(['user_id', pd.TimeGrouper(freq='7D')]).filter(lambda x: len(x)>1).groupby('user_id').sum()

# reset index
df_adoption = df_adoption.reset_index()
# -

# peek at some data 
df_adoption.head(3)


# +
# merge users and adopted users dataframe
df = df_users.merge(df_adoption, left_on='object_id', right_on='user_id', how='outer')

# drop column user_id since it is duplicate with object_id
df.drop('user_id', axis=1, inplace=True)

# replace NaN with zero
df = df.fillna(0)

from datetime import datetime
# convert unix timestamp to datetime
df['last_session_creation_time'] = df['last_session_creation_time'].apply(
    lambda x: datetime.strptime(str(datetime.fromtimestamp(float(int(x)))), '%Y-%m-%d %H:%M:%S'))
df['creation_time'] = df['creation_time'].apply(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S'))

#calculate active days
df['days_since_signup'] = df['last_session_creation_time'] - df['creation_time']
df['days_since_signup'] = df['days_since_signup'].apply(lambda x: abs(x.total_seconds()/60/60/24/30)) 

#convert creation_source into numeric values
df['creation_source']= df['creation_source'].astype('category')
cat_columns = df.select_dtypes(['category']).columns
df[cat_columns] = df[cat_columns].apply(lambda x: x.cat.codes)

# create column if us
df['adopted_user']=df['visited'].apply(lambda x: int(x > 0))

# column visited is not needed
df.drop('visited', axis=1, inplace=True)

df.head(3)

# +
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

#construct the dataset X, y
X = df[['creation_source', 'opted_in_to_mailing_list', 'enabled_for_marketing_drip', 'days_since_signup']]
y = (df.adopted_user == 1)

# Split the data into a training and test set.
Xlr, Xtestlr, ylr, ytestlr = train_test_split(X.values, y.values, test_size=0.20, random_state=5)

clf = LogisticRegression()
# Fit the model on the trainng data.
clf.fit(Xlr, ylr)
# Print the accuracy from the testing data.
print("Accuracy score: ", accuracy_score(clf.predict(Xtestlr), ytestlr))

# Print importance of each features
clf.fit(Xlr / np.std(Xlr, 0), ylr)
print("Regression coefficients: ", clf.coef_)
print("Intecept: ", clf.intercept_) 
print("Column names: ", (X.columns.values))

# -

# <h3>Results/Interpretation/Further Research:</h3>
# <br>
# <b>Results: </b>
# <br>I used the algorithm Logistics Regression since the target variable is binary (0, 1) if the user is adopted (1) or not (0).  The features are 'creation_source', 'opted_in_to_mailing_list', 'enabled_for_marketing_drip', 'days_since_signup'.  Accuracy score is 83.75% and the regression coefficients are  [[-0.1408732   0.03920644 -0.01876419 -1.59402912]]. <br>
# <b>Interpretation: </b>
# <br>Creation source most important is Org invite and least is signup google auth. Having a high value on creation source is 0.14 unit <font color='red'>decrease</font> in the odds of being an adopted user.<br>
# Opted in mailing list is 0.03 unit <font color='blue'>increase</font> in the probability that the user becomes adopted user. <br>
# Enabled for marketing drip is .02 unit <font color='red'>decrease</font> in the likelihood of being an adopted user. <br>
# Number of days since signup is 1.6 unit <font color='red'>decrease</font> in the chance that user is an adopted user <br>
# <b>Further research/data: </b> <br>
# (Assuming that Relax Inc. is a spa facility) Add user information such as gender, age, location, salary range and number of friends/co-workers/relatives who are also member.   Also, user profile on social network and their outlook regarding about health and fitness can also help to improve this prediction.
#
#

# #END OF REPORT

StarcoderdataJupyterScriptsDedupFiltered