<jupyter_start><jupyter_text>Load Dataset<jupyter_code>data = pd.read_csv('conversion_project.csv')
data.head()
data.info()
data.describe()
for column in data.columns:
    uniques = sorted(data[column].unique())
    print('{0:20s} {1:5d}\t'.format(column, len(uniques)), uniques[:5])<jupyter_output>country                  4	 ['China', 'Germany', 'UK', 'US']
age                     60	 [17, 18, 19, 20, 21]
new_user                 2	 [0, 1]
source                   3	 ['Ads', 'Direct', 'Seo']
total_pages_visited     29	 [1, 2, 3, 4, 5]
converted                2	 [0, 1]<jupyter_text>Remove Outliers<jupyter_code>data[data['age'] > 90]
data = data[data['age'] < 100]<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code># Visualization of different countries

grouped = data[['country', 'converted']].groupby('country').mean().reset_index()

fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (18,6))
sns.countplot(x = 'country', hue = 'converted', data = data, ax = ax[0])
ax[0].set_title('Count Plot of Country', fontsize = 16)
ax[0].set_yscale('log')
sns.barplot(x='country', y='converted', data = data, ax = ax[1])
ax[1].set_title('Mean Conversion Rate per Country',fontsize = 16)
plt.tight_layout()
plt.show()
#Visualization of different user types
grouped = data[['new_user', 'converted']].groupby('new_user').mean().reset_index()

fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (18,6))
sns.countplot(x = 'new_user', hue = 'converted', data = data, ax = ax[0])
ax[0].set_title('Count plot of User Types', fontsize = 16)
ax[0].set_yscale('log')
sns.barplot(x = 'new_user', y = 'converted', data = data, ax = ax[1])
ax[1].set_title('Mean Conversion Rate per User Type', fontsize = 16)
plt.tight_layout()
plt.show()
# Visualization of different sources
grouped = data[['source', 'converted']].groupby('source').mean().reset_index()

fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (18,6))
sns.countplot(x = 'source', hue = 'converted', data = data, ax = ax[0])
ax[0].set_title('Count Plot of Different Sources', fontsize = 16)
ax[0].set_yscale('log')
sns.barplot(x = 'source', y='converted', data = data, ax = ax[1])
ax[1].set_title('Mean Conversion Rate per Source', fontsize = 16)
plt.tight_layout()
plt.show()
# Visualization of different sources

grouped = data[['age', 'converted']].groupby('age').mean().reset_index()
hist_kws = {'histtype':'bar', 'edgecolor':'black', 'alpha':0.2}

fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (18, 6))
sns.distplot(data[data['converted'] == 0]['age'], label = 'Converted 0', ax = ax[0], hist_kws = hist_kws)
sns.distplot(data[data['converted'] == 1]['age'], label = 'Converted 1', ax = ax[0], hist_kws = hist_kws)
ax[0].set_title('Count Plot of Age', fontsize = 16)
ax[0].legend()
ax[1].plot(grouped['total_pages_visited'], grouped['converted'], '.-')
ax[1].set_title('Mean Conversion Rate vs. Age', fontsize = 16)
ax[1].set_xlabel('age')
ax[1].set_ylabel('Mean Conversion Rate')
ax[1].grid(True)
plt.show()
# Visualization of different sources
grouped = data[['total_pages_visited', 'converted']].groupby('total_pages_visited').mean().reset_index()

fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (18,6))
sns.distplot(data[data['converted'] == 0]['total_pages_visited'], label = 'Converted 0', ax = ax[0], hist_kws = hist_kws)
sns.distplot(data[data['converted'] == 1]['total_pages_visited'], label = 'Converted 1', ax = ax[0], hist_kws = hist_kws)
ax[0].set_title('Count Plot of Age')
ax[0].legend()
ax[1].plot(grouped['total_pages_visited'], grouped['converted'], '.-')
ax[1].set_title('Mean Conversion Rate vs. Total_pages_visited', fontsize = 16)
ax[1].set_xlabel('total_pages_visited')
ax[1].set_ylabel('Mean Conversion Rate')
ax[1].grid(True)
plt.show()<jupyter_output><empty_output><jupyter_text>Machine Learning<jupyter_code>h2o.init()
h2o.remove_all()
h2o_df = H2OFrame(data)

h2o_df['new_user'] = h2o_df['new_user'].asfactor()
h2o_df['converted'] = h2o_df['converted'].asfactor()

h2o_df.summary()
# Split into 75% training and 25% test dataset
strat_split = h2o_df['converted'].stratified_split(test_frac = 0.25, seed = 42)

train = h2o_df[strat_split == 'train']
test = h2o_df[strat_split == 'test']

feature = ['country','age','new_user','source','total_pages_visited']
target = 'converted'
# Build random forest model

model = H2ORandomForestEstimator(balance_classes = True, ntrees = 100, max_depth = 20,
                                 mtries = -1, seed = 42, score_each_iteration = True)
model.train(x = feature, y = target, training_frame = train)
model.varimp_plot()
# Feature importance

importance = model.varimp(use_pandas = True)

fig, ax = plt.subplots(figsize = (10, 8))
sns.barplot(x = 'scaled_importance', y='variable', data = importance)
plt.show()
# Make Predictions 

train_true = train.as_data_frame()['converted'].values
test_true = test.as_data_frame()['converted'].values
train_pred = model.predict(train).as_data_frame()['p1'].values
test_pred = model.predict(test).as_data_frame()['p1'].values

train_fpr, train_tpr, _ = roc_curve(train_true, train_pred)
test_fpr, test_tpr, _ = roc_curve(test_true, test_pred)
train_auc = np.round(auc(train_fpr, train_tpr), 3)
test_auc = np.round(auc(test_fpr, test_tpr), 3)
# classification report

print(classification_report(y_true = test_true, y_pred = (test_pred > 0.5).astype(int)))
fig, ax = plt.subplots(figsize = (8,6))
ax.plot(train_fpr, train_tpr, label = 'Train AUC: ' + str(train_auc))
ax.plot(test_fpr, test_tpr, label = 'Test AUC: '+str(test_auc))
ax.set_xlabel('False Positive Rate', fontsize = 12)
ax.set_ylabel('True Positve Rate', fontsize = 12)
ax.legend(fontsize = 12)
plt.show()
_ = model.partial_plot(train, cols = feature, figsize = (8,15))
h2o.cluster().shutdown()<jupyter_output>H2O session _sid_8a40 closed.
{'fraction_non_alphanumeric': 0.18, 'fraction_numerical': 0.030862068965517242, 'mean_word_length': 5.729698375870069, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '153381', 'license': 'MIT', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2187, 'n_words': 450, 'path': 'Collection of Take Home Data Science Challenge/conversion rate.ipynb', 'repo_name': 'reinfforce/Data-Science-Project', 'size': '5800'}