<jupyter_start><jupyter_text>Loading model<jupyter_code>with open(f'../../../src/models/Churn{churn}-Hyperotimization-f1-Dummies.md', 'rb') as file:
    rf = pickle.load(file)<jupyter_output><empty_output><jupyter_text>Checking Feature importances<jupyter_code>with open(f'../../../src/models/Churn{churn}-Hyperotimization-f1-Dummies-TRAINING-COLUMNS.md', 'rb') as file:
    columns = pickle.load(file)
fi = pd.DataFrame(rf.feature_importances_,index=columns, columns=['importance'])
fi.sort_values('importance', ascending=False).iloc[0:30].plot.bar()
top_features = fi.sort_values('importance', ascending=False).iloc[0:30].index
top_features<jupyter_output><empty_output><jupyter_text>Retrain model with 20 features<jupyter_code>df_train = pd.read_feather(f"../../../data/processed/Churn_{churn}_train_set.feather")
df_opt = pd.read_feather(f"../../../data/processed/Churn_{churn}_optimization_set.feather")
X_train = df_train.drop([f"BAJA_{churn}m", "DATECO_RAZON_SOCIAL_EMPLEADOR","FECHA_NACIMIENTO","time_window","index"], axis=1)
y_train = df_train[f"BAJA_{churn}m"]
del df_train
X_opt = df_opt.drop([f"BAJA_{churn}m", "DATECO_RAZON_SOCIAL_EMPLEADOR","FECHA_NACIMIENTO","time_window","index"], axis=1)
y_opt = df_opt[f"BAJA_{churn}m"]
del df_opt<jupyter_output><empty_output><jupyter_text>Feature Eng Dummifying<jupyter_code>X_train = pd.get_dummies(X_train)
X_train = X_train.fillna(-999)<jupyter_output><empty_output><jupyter_text>Filtering top 20 columns<jupyter_code>X_train = X_train[top_features]
X_train.head()
X_opt = pd.get_dummies(X_opt)
X_opt = X_opt.fillna(-999)
for col in [col for col in X_train.columns if col not in X_opt.columns]:
    X_opt[col] = -999
X_opt = X_opt.drop([col for col in X_opt.columns if col not in X_train.columns], axis=1)
len(X_opt.columns) 
len(X_train.columns)<jupyter_output><empty_output><jupyter_text>Optimization<jupyter_code>from hyperopt import tpe, fmin, space_eval, Trials, STATUS_OK, hp
from sklearn.metrics import f1_score, roc_auc_score 
from numpy.random import RandomState
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from tqdm import tqdm_notebook as tqdm
from sklearn.utils.class_weight import compute_class_weight 
compute_class_weight("balanced", y_train.unique(), y_train)
space = {
    "algo": hp.choice("algo", [RandomForestClassifier, ExtraTreesClassifier]),
    "params": {
        "n_estimators": hp.choice("n_estimators", [40, 80, 100]),  # 100, 400
        "min_samples_leaf": hp.choice("min_samples_leaf", [5, 20, 35, 55]), # , 65
        "max_depth": hp.choice("max_depth", [5, 10, 15]), # 10,
        "max_features": hp.choice("max_features", ['sqrt', 0.5, None]),
        "class_weight": hp.choice("class_weight", [{0:1, 1:30}, {0:1, 1:40},  {0:1, 1:50}]) ,
    }
}
evals = 60
pbar = tqdm(total=evals)

def __optimizerFunc(args):
    pbar.update(0.5)
    args["params"]["verbose"] = 1
    args["params"]["n_jobs"] = -1
    args["params"]["random_state"] = 42
    model = args["algo"](**args["params"])
    print(f"Trying: {model}")
    model.fit(X_train, y_train)
    y_pred = model.predict(X_opt) 
    pbar.update(0.5)
    # greater is better
    return {"loss": -f1_score(y_opt, y_pred), "status": STATUS_OK,}
          
trials = Trials()
__bestClassifier = fmin(fn=__optimizerFunc,
                        space=space,
                        algo=tpe.suggest,
                        max_evals=evals,
                        trials=trials,
                        rstate=RandomState(42)
                        )
pbar.close()
print(trials.results)
print("Best Config:")
opt_config = space_eval(space, __bestClassifier)
print(opt_config)
rf_min = opt_config['algo'](**opt_config['params'])
rf_min.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Metrics<jupyter_code>df_val = pd.read_feather(f"../../../data/processed/Churn_{churn}_validation_set.feather")
X_val = df_val.drop([f"BAJA_{churn}m", "DATECO_RAZON_SOCIAL_EMPLEADOR","FECHA_NACIMIENTO","time_window","index"], axis=1)
y_val = df_val[f"BAJA_{churn}m"]
del df_val
X_val = pd.get_dummies(X_val)
X_val = X_val.fillna(-999)
for col in [col for col in X_train.columns if col not in X_val.columns]:
    X_val[col] = -999
X_val = X_val.drop([col for col in X_val.columns if col not in X_train.columns], axis=1)
len(X_val.columns)
predictions = rf_min.predict(X_val)
cmap = sns.color_palette("coolwarm", 7)
sns.set(font_scale=1)
sns.heatmap(confusion_matrix(y_val, predictions), annot=True, fmt="d", linewidths=.5, cmap=cmap)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15503533568904593, 'fraction_numerical': 0.020980565371024735, 'mean_word_length': 5.1787175989085945, 'pattern_counts': {'":': 9, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '330008', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1755, 'n_words': 270, 'path': 'notebooks/3. Model/Hipotesis 2/3. Hyperoptimization - f1 - Dummies - Top 20 features.ipynb', 'repo_name': 'Voolkia/ML', 'size': '4528'}