<jupyter_start><jupyter_text>Importing Data<jupyter_code>train = pd.read_csv('train_s3TEQDk.csv')
test = pd.read_csv('test_mSzZ8RL.csv')<jupyter_output><empty_output><jupyter_text>Cleaning Train Data<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>Cleaning Test Data<jupyter_code># 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)<jupyter_output>Training Data Shape : (245725, 46)

Testing Data Shape : (105312, 46)<jupyter_text>Building A XGBoost Model<jupyter_code># importing random forest classifier and creating an instance
import xgboost as xgb
# Create XGB Classifier object
xgb_clf = xgb.XGBClassifier(silent=False,tree_method = "exact", predictor = "cpu_predictor",
                            objective = "binary:logistic", seed=123)

# Create parameter grid
parameters = {"learning_rate": [0.1, 0.01, 0.001],
               "gamma" : [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2],
               "max_depth": [2, 4, 7, 10],
               "colsample_bytree": [0.3, 0.6, 0.8, 1.0],
               "subsample": [0.2, 0.4, 0.5, 0.6, 0.7],
               "reg_alpha": [0, 0.5, 1],
               "reg_lambda": [1, 1.5, 2, 3, 4.5],
               "min_child_weight": [1, 3, 5, 7],
               "n_estimators": [10, 50, 100]}

from sklearn.model_selection import RandomizedSearchCV
# Create RandomizedSearchCV Object
xgb_rscv = RandomizedSearchCV(xgb_clf, param_distributions = parameters, scoring = "roc_auc",
                              cv = 10, verbose = 3, random_state = 40 )
xgb_rscv.fit(X_train, y_train)
xgb_pred = xgb_rscv.predict_proba(X_test)
xgb_pred
# Model best estimators
print("Learning Rate: ", xgb_rscv.best_estimator_.get_params()["learning_rate"])
print("Gamma: ", xgb_rscv.best_estimator_.get_params()["gamma"])
print("Max Depth: ", xgb_rscv.best_estimator_.get_params()["max_depth"])
print("Subsample: ", xgb_rscv.best_estimator_.get_params()["subsample"])
print("Max Features at Split: ", xgb_rscv.best_estimator_.get_params()["colsample_bytree"])
print("Alpha: ", xgb_rscv.best_estimator_.get_params()["reg_alpha"])
print("Lambda: ", xgb_rscv.best_estimator_.get_params()["reg_lambda"])
print("Minimum Sum of the Instance Weight Hessian to Make a Child: ",
      xgb_rscv.best_estimator_.get_params()["min_child_weight"])
print("Number of Trees: ", xgb_rscv.best_estimator_.get_params()["n_estimators"])<jupyter_output>Learning Rate:  0.1
Gamma:  2
Max Depth:  7
Subsample:  0.7
Max Features at Split:  0.6
Alpha:  0.5
Lambda:  1
Minimum Sum of the Instance Weight Hessian to Make a Child:  3
Number of Trees:  100<jupyter_text>Saving Predictions to a csv file<jupyter_code>ID = pd.DataFrame(ID)
ID.columns =['ID']
Is_lead = pd.DataFrame(xgb_pred[:,1])
Is_lead.columns =['Is_Lead']
det = pd.concat([ID, Is_lead], join = 'outer', axis = 1)
det.to_csv("Submission6.csv", index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15053116857085588, 'fraction_numerical': 0.024854680296652637, 'mean_word_length': 5.892265193370166, 'pattern_counts': {'":': 8, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '583794', 'license': 'MIT', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1787, 'n_words': 366, 'path': 'JOB-A-THON - May 2021/Code XGBoost Prams Tuned.ipynb', 'repo_name': 'Tonumoy/ML-Assignments', 'size': '4989'}