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

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression

# # Convertion rate challenge
# ### <NAME>

# ## Goal of the exercise

# The goal of this exercise is to build a model that predicts conversion rate and, based on the model, come up with ideas to improve revenue.
#
# We have data about users who hit our site: whether they converted or not as well as some of their characteristics such as their country, the marketing channel, their age, whether they are repeat users and the number of pages visited during that session (as a proxy for site activity/time spent on site).
#
# The project is to:
# - Predict conversion rate
# - Come up with recommendations for the product team and the marketing team to improve conversion rate

# ## Data exploration

data_original = pd.read_csv('conversion_data.csv')
data_original.head()

# Columns:
# - country : user country based on the IP address
# - age : user age. Self-reported at sign-in step
# - new_user : whether the user created the account during this session or had already an account and simply came back to the site
# - source : marketing channel source
#     - Ads: came to the site by clicking on an advertisement
#     - Seo: came to the site by clicking on search results
#     - Direct: came to the site by directly typing the URL on the browser
# - total_pages_visited: number of total pages visited during the session. This is a proxy for time spent on site and engagement during the session.
# - converted: this is our label. 1 means they converted within the session, 0 means they left without buying anything. 
#
# __The company goal is to increase conversion rate: # conversions / total sessions.__

# Number of rows in the data set:

len(data_original)

data_original.describe()

# A couple of interesting things from the above description of our data:
#
# - The age range for our users is 17 to 123. The max value of this range indicates that there may be some problems with the data recorded, although it is posible for a person of 123 years of age to be using the website, seems a bit strange.
# - 75% of our data points correspond to people under the age of 36.
# - 68% are new users.
# - The mean number of pages visited in a session is 4.9.
# - The mean convertion can be seen as the mean conversion rate of the data set, in this case 3%.

# Let's explore the super high ages we found in the description of the data set.

data_original.sort_values(by='age', ascending=False).head(10)

# Still feels a bit strange to have 2 users with ages 123 and 111. They both converted and have really high number of total pages visited. Since these are only 2 data points (of more than 360,000) if would remove them just to be safe.

data_cleaned = data_original[data_original['age'] < 100]
len(data_cleaned)

# Lets see if our data set has some missing values:

data_cleaned.isnull().values.any()

# No missing values on our data set.

data_cleaned.groupby('converted').mean()

# Seems to be a clear relationship between total_pages visited and converted. Also the mayority of converted are old users. Either seems a good idea to make new users come back, or to find out why people don't convert on the first visit.

data_cleaned.groupby('source').mean()

# There seems to be no much of a relationship for source and convertion rates.

data_cleaned.groupby('country').mean()

# Looking at country-based data, the Chinese convertion rate seems very low.

# ## Data visualization

# %matplotlib inline

data_cleaned['age'].hist()
plt.title('Histogram of Age')
plt.xlabel('Age')
plt.ylabel('Frequency')

data_cleaned['new_user'].hist()
plt.title('Histogram of New user')
plt.xlabel('New user')
plt.ylabel('Frequency')

data_cleaned['total_pages_visited'].hist()
plt.title('Histogram of Total pages visited')
plt.xlabel('Total pages visited')
plt.ylabel('Frequency')

data_cleaned['country'].value_counts().plot(kind='bar')
plt.title('Histogram of Country')
plt.xlabel('Country')
plt.ylabel('Frequency')

data_cleaned['source'].value_counts().plot(kind='bar')
plt.title('Histogram of Source')
plt.xlabel('Source')
plt.ylabel('Frequency')

# ## Data preparation

# For logistic regrasion I'll create dummy variables for country and source.

dummy_country = pd.get_dummies(data_cleaned['country'], prefix='country')
dummy_country.head()

dummy_source = pd.get_dummies(data_cleaned['source'], prefix='source')
dummy_source.head()

data_lr = data_cleaned[['age', 'new_user', 'total_pages_visited', 'converted']].join([dummy_country, dummy_source])
data_lr.head()

# ## Logistic regression

#still don't get why I have to remove the first dummy value for each dummy variable (has to do with the intercept?)
feature_cols = ['age', 'new_user', 'total_pages_visited', 'country_Germany', 'country_UK', 'country_US', 'source_Direct', 'source_Seo']
X = data_lr[feature_cols]
y = data_lr['converted']

model = LogisticRegression()

model = model.fit(X, y)

dict(zip(X.columns, np.transpose(model.coef_)))

model.intercept_



StarcoderdataJupyterScriptsDedupFiltered