<jupyter_start><jupyter_text>XGBoost con Binary Encoding y sin BOW<jupyter_code>X = train_bin.drop(["id_original","keyword_original","location_original","text_original","target_label",
                      "clean_text", "hashtags", "labels"], axis=1)
y = train_bin["target_label"]

print(X.head().T)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
# GridSearch CV
# hiper_parametros = {"n_estimators":[50,100,150],
#                    "max_depth":[5,7,9],
#                    "colsample_bytree":[0.5,0.7],
#                    "subsample":[0.7,1],
#                    "learning_rate":[0.1],
#                     "n_jobs": [1]}
# XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
#               colsample_bynode=1, colsample_bytree=0.7, gamma=0,
#               learning_rate=0.1, max_delta_step=0, max_depth=7,
#               min_child_weight=1, missing=None, n_estimators=100, n_jobs=1,
#               nthread=None, objective='binary:logistic', random_state=0,
#               reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
#               silent=None, subsample=0.7, verbosity=1)
# 0.7031002485086537


hiper_parametros = {"n_estimators":[200,300,500],
                   "max_depth":[7,9,11,13],
                   "colsample_bytree":[0.5,0.7],
                   "subsample":[0.7,1],
                   "learning_rate":[0.1],
                    "n_jobs": [1]}
clasif = GridSearchCV(xgb.XGBClassifier(), hiper_parametros, cv=4, scoring='accuracy')
clasif.fit(X_train, y_train)
print(clasif.best_estimator_)
print(clasif.best_score_)<jupyter_output>XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
              colsample_bynode=1, colsample_bytree=0.5, gamma=0,
              learning_rate=0.1, max_delta_step=0, max_depth=9,
              min_child_weight=1, missing=None, n_estimators=200, n_jobs=1,
              nthread=None, objective='binary:logistic', random_state=0,
              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
              silent=None, subsample=1, verbosity=1)
0.7008234793567041<jupyter_text>XGBoost con Binary Encoding y con BOW<jupyter_code>vectorizer = CountVectorizer(stop_words='english')
vectorizer.fit(train["clean_text"])

vec_train = vectorizer.fit_transform(train["clean_text"])
feature_words = vectorizer.get_feature_names()
print(len(feature_words))
df_bow_train = pd.DataFrame(vec_train.toarray(), columns=feature_words)
df_bow_train.head()
vec_test = vectorizer.transform(test["clean_text"])
df_bow_test = pd.DataFrame(vec_test.toarray(), columns=feature_words)
df_bow_test.head()
filter_bow_train = df_bow_train.loc[:,(df_bow_train.sum()>1)]
filter_bow_train.head()
filter_list = filter_bow_train.columns.tolist()
filter_bow_test = df_bow_test.filter(items=filter_list)
filter_bow_test.shape
train_bow_bin = pd.concat([train_bin,filter_bow_train], axis="columns")
print(train_bow_bin.shape)
train_bow_bin.sample(5)
test_bow_bin = pd.concat([test_bin,filter_bow_test], axis="columns")
print(test_bow_bin.shape)
test_bow_bin.sample(5)
X = train_bow_bin.drop(["id_original","keyword_original","location_original","text_original","target_label",
                      "clean_text", "hashtags", "labels"], axis=1)
y = train_bow_bin["target_label"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
# utilizando las 6069 features
model = xgb.XGBClassifier(n_estimators=500, max_depth=11, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train, y_train)
y_test_hat = model.predict(X_test)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
#me quedo con las mejores
X_train_fs, X_test_fs = select_features(X_train, y_train, X_test, chi2, 1000)
X_train_fs2, X_test_fs2 = select_features(X_train, y_train, X_test, chi2, 2000)
model = xgb.XGBClassifier(n_estimators=500, max_depth=7, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train_fs, y_train)
y_test_hat = model.predict(X_test_fs)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
model = xgb.XGBClassifier(n_estimators=400, max_depth=11, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train_fs2, y_train)
y_test_hat = model.predict(X_test_fs2)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
# me quedo con 3000 features
X_train_fs, X_test_fs = select_features(X_train, y_train, X_test, chi2, 3000)
model = xgb.XGBClassifier(n_estimators=400, max_depth=11, colsample_bytree=0.7, learning_rate=0.1, subsample=1)
model.fit(X_train_fs, y_train)
y_test_hat = model.predict(X_test_fs)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
vectorizer = CountVectorizer(stop_words='english', binary=True)
vectorizer.fit(train["clean_text"])

vec_train = vectorizer.fit_transform(train["clean_text"])
feature_words = vectorizer.get_feature_names()
print(len(feature_words))
df_bow_train = pd.DataFrame(vec_train.toarray(), columns=feature_words)
df_bow_train.head()
train_bow_bin = pd.concat([train_bin,df_bow_train], axis="columns")
print(train_bow_bin.shape)
X = train_bow_bin.drop(["id_original","keyword_original","location_original","text_original","target_label",
                      "clean_text", "hashtags", "labels"], axis=1)
y = train_bow_bin["target_label"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
#me quedo con las mejores
X_train_fs, X_test_fs = select_features(X_train, y_train, X_test, chi2, 700)
X_train_fs2, X_test_fs2 = select_features(X_train, y_train, X_test, chi2, 1000)
X_train_fs3, X_test_fs3 = select_features(X_train, y_train, X_test, chi2, 2000)
model = xgb.XGBClassifier(n_estimators=350, max_depth=7, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train_fs, y_train)
y_test_hat = model.predict(X_test_fs)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
model = xgb.XGBClassifier(n_estimators=500, max_depth=9, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train_fs2, y_train)
y_test_hat = model.predict(X_test_fs2)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
model = xgb.XGBClassifier(n_estimators=500, max_depth=13, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train_fs3, y_train)
y_test_hat = model.predict(X_test_fs3)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))

# reduciendo las features
# utilizando las 6069 features
model = xgb.XGBClassifier(n_estimators=500, max_depth=11, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train, y_train)
y_test_hat = model.predict(X_test)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
df_feat_importances = pd.DataFrame(model.feature_importances_, index=X_train.columns, columns=["importancia"]).\
        sort_values(by="importancia",ascending=False)
df_feat_importances.head()
list_fi = df_feat_importances.index[:2000].tolist()
X_fi = X.filter(items=list_fi)
print(X_fi.shape)
print(X_fi.columns)
X_train, X_test, y_train, y_test = train_test_split(X_fi, y, test_size=0.25, random_state=100)
model = xgb.XGBClassifier(n_estimators=500, max_depth=11, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train, y_train)
y_test_hat = model.predict(X_test)
print("Accuracy score: %f" % (accuracy_score(y_test, y_test_hat)))
X_train = train_bow_bin.drop(["id_original","keyword_original","location_original","text_original","target_label","hashtags","labels","clean_text"], axis=1)
X_test = test_bow_bin.drop(["id_original","keyword_original","location_original","text_original","hashtags","labels","clean_text"], axis=1)
y_train = train_bow_bin["target_label"]
list_fi = df_feat_importances.index[:2000].tolist()
X_train_fi = X_train.filter(items=list_fi)
X_test_fi = X_test.filter(items=list_fi)
print(X_train_fi.shape)
print(X_test_fi.shape)
model = xgb.XGBClassifier(n_estimators=500, max_depth=11, colsample_bytree=0.5, learning_rate=0.1, subsample=1)
model.fit(X_train_fi, y_train)
y_pred = model.predict(X_test_fi)
y_pred
test["target"] = y_pred
test[["id_original","target"]]
from google.colab import files

test[["id_original","target"]].rename(columns={"id_original":"id"}).to_csv("pred9_XGB_bowbin", index=False)
files.download("pred9_XGB_bowbin")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1410555815039701, 'fraction_numerical': 0.03619803829985988, 'mean_word_length': 6.4934383202099735, 'pattern_counts': {'":': 13, '<': 8, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '525847', 'license': 'MIT', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3322, 'n_words': 457, 'path': 'TP2/Matias/33_XGBoost_v7.ipynb', 'repo_name': 'aleacevedo/Datos-TP1', 'size': '8564'}