<jupyter_start><jupyter_text>Primer DataFrame Encodeado Xgboost Regressor Entrenamiento<jupyter_code>DataFrame2 = pd.read_csv('/content/short_df_Encoded.csv')
DataFrame2.head()
DataFrame2.drop(columns = {'Unnamed: 0'}, inplace = True)
DataFrame2.head()
DataFrame2Train = DataFrame2.loc[:,DataFrame2.columns != 'Decision']
DataFrame2Test  =  DataFrame2.loc[:,'Decision']
DataFrame2Train.shape, DataFrame2Test.shape, DataFrame2.shape
x_train, x_test, y_train, y_test = train_test_split(DataFrame2Train,DataFrame2Test,test_size = 0.2, random_state = 0)
xg_Regressor1 = xgb.XGBRegressor(colsample_bytree= 0.8, gamma= 1.5, learning_rate= 0.01, max_depth= 20, min_child_weight= 1, n_estimators= 200, subsample= 0.8)
xg_Regressor1.fit(x_train,y_train)
predsRegressor1 = xg_Regressor1.predict(x_test)
value_pred_Regressor1 = pd.DataFrame()
value_pred_Regressor1['pred'] = predsRegressor1
value_pred_Regressor1['pred'].value_counts()
rmseRegressor1 = np.sqrt(mean_squared_error(y_test, predsRegressor1))
print("RMSE: %f" % (rmseRegressor1))
lossRegressor1 = log_loss(y_test, predsRegressor1)
print(f"Log Loss is {loss}")<jupyter_output>Log Loss is 0.4387157682046272<jupyter_text>Test<jupyter_code>DataFrameTest = pd.read_csv('/content/DataFrame_test_encoded.csv')
DataFrameTest.head()
DataFrameTest.drop(columns = {'Unnamed: 0'}, inplace = True)
DataFrameTest.head()
DescargaPredRegressor1 = pd.DataFrame()
DescargaPredRegressor1['Opportunity_ID'] = DataFrameTest['Opportunity_ID']
DescargaPredRegressor1.set_index('Opportunity_ID', inplace = True)
#Tambien separo para el test
#-----------------------------------------------------------------------
DescargaPredClassificator1 = pd.DataFrame()
DescargaPredClassificator1['Opportunity_ID'] = DataFrameTest['Opportunity_ID']
DescargaPredClassificator1.set_index('Opportunity_ID', inplace = True)
DataFrameTest = DataFrameTest.drop(columns={'Opportunity_ID'})
DataFrameTest.head()
predRegressor_Test1 = xg_Regressor1.predict(DataFrameTest)
DescargaPredRegressor1['target'] = predRegressor_Test1
DescargaPredRegressor1
DescargaPredRegressor1['target'].value_counts()
#csv1 = DescargaPredRegressor1.to_csv('xgboost_regression_pred1.csv')
#files.download('xgboost_regression_pred1.csv')<jupyter_output><empty_output><jupyter_text>Xgboost Clasificator Entrenamiento<jupyter_code>X_Entrenamiento, X_EntrenamientoTest, Y_TestEntrenamiento, Y_TestTest = train_test_split(DataFrame2Train, DataFrame2Test,test_size = 0.2, random_state = 0)
X_Entrenamiento.shape, X_EntrenamientoTest.shape, Y_TestEntrenamiento.shape, Y_TestTest.shape
xg_classificator1 = XGBClassifier(colsample_bytree = 0.8, gamma = 1, learning_rate = 0.01, max_depth = 20, min_child_weight = 1, n_estimators = 200, subsample = 0.8)
xg_classificator1.fit(X_Entrenamiento,Y_TestEntrenamiento)
predsClassificator1 = xg_classificator1.predict(X_EntrenamientoTest)
from sklearn.metrics import accuracy_score, classification_report
print(accuracy_score(Y_TestTest, predsClassificator1))
print(classification_report(Y_TestTest, predsClassificator1))
pd.crosstab(Y_TestTest, predsClassificator1)
value_predClassificator1 = pd.DataFrame()
value_predClassificator1['pred'] = predsClassificator1
value_predClassificator1['pred'].value_counts()
rmseClassificator1 = np.sqrt(mean_squared_error(Y_TestTest, predsClassificator1))
print("RMSE: %f" % (rmseClassificator1))
accuracy = accuracy_score(Y_TestTest, predsClassificator1)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
lossClassificator1 = log_loss(Y_TestTest, predsClassificator1)
print(f"Log Loss is {loss}")<jupyter_output>Log Loss is 0.4387157682046272<jupyter_text>Test<jupyter_code>predClassificator_Test1 = xg_classificator1.predict(DataFrameTest)
DescargaPredClassificator1['target'] = predClassificator_Test1
DescargaPredClassificator1
DescargaPredClassificator1['target'].value_counts()
#csv2 = DescargaPredClassificator1.to_csv('xgboost_clasification_pred1.csv')
#files.download('xgboost_clasification_pred1.csv')<jupyter_output><empty_output><jupyter_text>Segundo DataFrame Encodeado Xgboost Regressor Entrenamiento<jupyter_code>DataFrame3 = pd.read_csv('/content/DataFrame_train_encoded1.csv')
DataFrame3.head()
DataFrame3.drop(columns = {'Unnamed: 0'}, inplace = True)
DataFrame3.head()
DataFrame3Train = DataFrame3.loc[:,DataFrame3.columns != 'Decision']
DataFrame3Test  =  DataFrame3.loc[:,'Decision']
DataFrame3Train.shape, DataFrame3Test.shape, DataFrame3.shape
x_train2, x_test2, y_train2, y_test2 = train_test_split(DataFrame3Train,DataFrame3Test,test_size = 0.2, random_state = 0)
xg_Regressor2 = xgb.XGBRegressor(colsample_bytree= 0.8, gamma= 1.5, learning_rate= 0.01, max_depth= 20, min_child_weight= 1, n_estimators= 200, subsample= 0.8)
xg_Regressor2.fit(x_train2,y_train2)
predsRegressor2 = xg_Regressor2.predict(x_test2)
value_pred_Regressor2 = pd.DataFrame()
value_pred_Regressor2['pred'] = predsRegressor2
value_pred_Regressor2['pred'].value_counts()
rmseRegressor2 = np.sqrt(mean_squared_error(y_test2, predsRegressor2))
print("RMSE: %f" % (rmseRegressor2))
lossRegressor2 = log_loss(y_test2, predsRegressor2)
print(f"Log Loss is {loss}")<jupyter_output>Log Loss is 0.4387157682046272<jupyter_text>Test<jupyter_code>DataFrameTest3 = pd.read_csv('/content/DataFrame_test_encoded1.csv')
DataFrameTest3.head()
DataFrameTest3.drop(columns = {'Unnamed: 0'}, inplace = True)
DataFrameTest3.head()
DescargaPredRegressor2 = pd.DataFrame()
DescargaPredRegressor2['Opportunity_ID'] = DataFrameTest3['Opportunity_ID']
DescargaPredRegressor2.set_index('Opportunity_ID', inplace = True)
#Tambien separo para el test
#-----------------------------------------------------------------------
DescargaPredClassificator2 = pd.DataFrame()
DescargaPredClassificator2['Opportunity_ID'] = DataFrameTest3['Opportunity_ID']
DescargaPredClassificator2.set_index('Opportunity_ID', inplace = True)
DataFrameTest3 = DataFrameTest3.drop(columns={'Opportunity_ID'})
DataFrameTest3.head()
predRegressor_Test2 = xg_Regressor2.predict(DataFrameTest3)
DescargaPredRegressor2['target'] = predRegressor_Test2
DescargaPredRegressor2
DescargaPredRegressor2['target'].value_counts()
#csv3 = DescargaPredRegressor1.to_csv('xgboost_regression_pred2.csv')
#files.download('xgboost_regression_pred2.csv')<jupyter_output><empty_output><jupyter_text>Xgboost Clasificator Entrenamiento<jupyter_code>X_Entrenamiento2, X_EntrenamientoTest2, Y_TestEntrenamiento2, Y_TestTest2 = train_test_split(DataFrame3Train, DataFrame3Test,test_size = 0.2, random_state = 0)
X_Entrenamiento2.shape, X_EntrenamientoTest2.shape, Y_TestEntrenamiento2.shape, Y_TestTest2.shape
xg_classificator2 = XGBClassifier(colsample_bytree = 0.8, gamma = 1, learning_rate = 0.01, max_depth = 20, min_child_weight = 1, n_estimators = 200, subsample = 0.8)
xg_classificator2.fit(X_Entrenamiento2,Y_TestEntrenamiento2)
predsClassificator2 = xg_classificator2.predict(X_EntrenamientoTest2)
from sklearn.metrics import accuracy_score, classification_report
print(accuracy_score(Y_TestTest2, predsClassificator2))
print(classification_report(Y_TestTest2, predsClassificator2))
pd.crosstab(Y_TestTest2, predsClassificator2)
value_predClassificator2 = pd.DataFrame()
value_predClassificator2['pred'] = predsClassificator2
value_predClassificator2['pred'].value_counts()
rmseClassificator2 = np.sqrt(mean_squared_error(Y_TestTest2, predsClassificator2))
print("RMSE: %f" % (rmseClassificator2))
accuracy = accuracy_score(Y_TestTest2, predsClassificator2)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
lossClassificator2 = log_loss(Y_TestTest2, predsClassificator2)
print(f"Log Loss is {loss}")<jupyter_output>Log Loss is 0.4387157682046272<jupyter_text>Test<jupyter_code>predClassificator_Test2 = xg_classificator2.predict(DataFrameTest3)
DescargaPredClassificator2['target'] = predClassificator_Test2
DescargaPredClassificator2
DescargaPredClassificator2['target'].value_counts()
#csv4 = DescargaPredClassificator2.to_csv('xgboost_clasification_pred2.csv')
#files.download('xgboost_clasification_pred2.csv')<jupyter_output><empty_output><jupyter_text>GridSearch<jupyter_code>"""
def timer(start_time=None):
    if not start_time:
        start_time = datetime.now()
        return start_time
    elif start_time:
        thour, temp_sec = divmod((datetime.now() - start_time).total_seconds(), 3600)
        tmin, tsec = divmod(temp_sec, 60)
        print('\n Time taken: %i hours %i minutes and %s seconds.' % (thour, tmin, round(tsec, 2)))
"""
# A parameter grid for XGBoost
params = {
        'min_child_weight': [1, 5, 10],
        'gamma': [0.5, 1, 1.5],
        'subsample': [0.6, 0.8],
        'colsample_bytree': [0.3, 0.6, 0.8],
        'max_depth': [3, 5, 10, 20],
        'learning_rate' : [0.1, 0.01, 0.001],
        'n_estimators' : [10, 100, 150, 200]
        }

#xgb = xgb.XGBRegressor(learning_rate=0.02, n_estimators=600, objective='binary:logistic', silent=True, nthread=1)
"""
folds = 3
param_comb = 5

skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)

random_search = RandomizedSearchCV(xgb, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(DataFrame2Train,DataFrame2Test), verbose=3, random_state=1001 )

# Here we go
start_time = timer(None) # timing starts from this point for "start_time" variable
random_search.fit(DataFrame2Train, DataFrame2Test)
timer(start_time) # timing ends here for "start_time" variable
"""
print('\n All results:')
print(random_search.cv_results_)
print('\n Best estimator:')
print(random_search.best_estimator_)
print('\n Best normalized gini score for %d-fold search with %d parameter combinations:' % (folds, param_comb))
print(random_search.best_score_ * 2 - 1)
print('\n Best hyperparameters:')
print(random_search.best_params_)
results = pd.DataFrame(random_search.cv_results_)
results.to_csv('xgb-random-grid-search-results-01.csv', index=False)
"""
grid = GridSearchCV(estimator=xgb, param_grid=params, scoring='roc_auc', n_jobs=4, cv=skf.split(DataFrame2Train,DataFrame2Test), verbose=3 )
grid.fit(DataFrame2Train, DataFrame2Test)
print('\n All results:')
print(grid.cv_results_)
print('\n Best estimator:')
print(grid.best_estimator_)
print('\n Best score:')
print(grid.best_score_ * 2 - 1)
print('\n Best parameters:')
print(grid.best_params_)
results = pd.DataFrame(grid.cv_results_)
results.to_csv('xgb-grid-search-results-01.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12965636731157956, 'fraction_numerical': 0.04081239772836654, 'mean_word_length': 8.026933101650739, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '75998', 'license': 'MIT', 'n_tokens_mistral': 3993, 'n_tokens_neox': 3784, 'n_words': 553, 'path': 'Gradient Boosting/XGBoost/.ipynb_checkpoints/Xgboost-checkpoint.ipynb', 'repo_name': 'BautistaXifro/Machine-Learning-Datos', 'size': '10389'}