# ---
# 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
# ---

# importing libraries 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline

# ## Importing Data

train = pd.read_csv('train_s3TEQDk.csv')
test = pd.read_csv('test_mSzZ8RL.csv')

# ## Cleaning Train Data

# checking training data headers
train.head()

# visualizing missing training data (if any)
sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')

# replacing 'YES', 'NO' values by '1' and '0' in training data and filling the missing values with mean
train = train.replace(('Yes','No'),(1,0))
train = train.fillna(train.mean())
sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='viridis')

train.head()

# converting categorical features to dummy variables using pandas
gender = pd.get_dummies(train['Gender'],drop_first=True)
RCode = pd.get_dummies(train['Region_Code'],drop_first=True)
Occ = pd.get_dummies(train['Occupation'],drop_first=True)
CCode = pd.get_dummies(train['Channel_Code'],drop_first=True)

# dropping redundant feature columns after encoding 
train.drop(['ID','Gender','Region_Code','Occupation','Channel_Code'],axis=1,inplace=True)

# concatenating dataframes
train = pd.concat([train,gender,RCode,Occ,CCode],axis=1)

train.head()



# ## Cleaning Test Data

# checking testing data headers
test.head()

# visualizing missing testing data (if any)
sns.heatmap(test.isnull(),yticklabels=False,cbar=False,cmap='viridis')

# replacing 'YES', 'NO' values by '1' and '0' in testing data and filling the missing values with mean
test = test.replace(('Yes','No'),(1,0))
test = test.fillna(test.mean())
sns.heatmap(test.isnull(),yticklabels=False,cbar=False,cmap='viridis')

test.head()

# converting categorical features to dummy variables using pandas
gender = pd.get_dummies(test['Gender'],drop_first=True)
RCode = pd.get_dummies(test['Region_Code'],drop_first=True)
Occ = pd.get_dummies(test['Occupation'],drop_first=True)
CCode = pd.get_dummies(test['Channel_Code'],drop_first=True)

# dropping redundant feature columns after encoding 
ID = test['ID']
test.drop(['ID','Gender','Region_Code','Occupation','Channel_Code'],axis=1,inplace=True)

# concatenating dataframes
test = pd.concat([test,gender,RCode,Occ,CCode],axis=1)

test.head()

print('Training Data Shape :',train.shape)
print('\nTesting Data Shape :',test.shape)

X_train = train.drop(['Is_Lead'], axis=1)
X_test = test
y_train = train['Is_Lead']

print('Training Data Shape :',X_train.shape)
print('\nTesting Data Shape :',X_test.shape)

# ## Building A Random Forest Model

# importing random forest classifier and creating an instance
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()

# +
# Optimizing Hyperparameters for Random Forest Algorithm

from sklearn.model_selection import RandomizedSearchCV

# number of trees
n_estimators = [100,200,300,400,500] 

# max number of features to consider at every split
max_features = ['auto','sqrt','log2']

# max number of levels in a tree
max_depth = [10,20,30,40,50]
max_depth.append(None) 

# minimum number of samples required to split a node
min_samples_split = [2,5,10,15,20]

# minimum number of samples required at each leaf node
min_samples_leaf = [1,2,5,10,15]

grid_param = {'n_estimators': n_estimators, 
              'max_depth': max_depth, 
              'max_features': max_features, 
              'min_samples_split': min_samples_split,
              'min_samples_leaf': min_samples_leaf}

rfc_random = RandomizedSearchCV(estimator = rfc,
                                param_distributions = grid_param, 
                                n_iter = 100, cv = 5, verbose = 2, random_state = 42,
                                n_jobs = -1)
# -

rfc_random.fit(X_train, y_train)

rfc_pred = rfc_random.predict_proba(X_test)

rfc_pred

rfc_random.best_params_

# ## Saving Predictions to a csv file

ID = pd.DataFrame(ID)
ID.columns =['ID']
Is_lead = pd.DataFrame(rfc_pred[:,1])
Is_lead.columns =['Is_Lead']
det = pd.concat([ID, Is_lead], join = 'outer', axis = 1)
det.to_csv("Submission3.csv", index=False)



StarcoderdataJupyterScriptsDedupFiltered