<jupyter_start><jupyter_text>CLASIFICADOR CON LAS PROBABILIDADES<jupyter_code>cnn_predictions_dir = get_path(model_config.model_predictions_cnn_dir)

merge_list = []
for weight, frozen_layers in zip([*repeat('imagenet', 6), 'random'], ['ALL', '0FT', '1FT', '2FT', '3FT', '4FT']):
    
    data = db[['PROCESSED_IMG', 'IMG_LABEL', 'TRAIN_VAL', *XGB_COLS[XGB_CONFIG]]].copy()
    data.loc[:, 'LABEL'] = data.IMG_LABEL.map({k: v for v, k in enumerate(sorted(data.IMG_LABEL.unique(), reverse=False))})
    
    l = []
    for file in search_files(get_path(cnn_predictions_dir, weight, frozen_layers, create=False), ext='csv'):

        l.append(
            pd.read_csv(file, sep=';')[['PROCESSED_IMG', 'PROBABILTY']].\
                assign(WEIGHTS=weight, FT=frozen_layers, CNN=get_filename(file))
        )

    merge_list.append(pd.merge(left=data, right=pd.concat(l, ignore_index=True), on='PROCESSED_IMG', how='left'))

all_data = pd.concat(merge_list, ignore_index=True)

all_data.head()
# Se escoge el mejor modelo en función de las metricas AUC de validacion
cnn_selection = all_data.groupby(['CNN', 'FT', 'WEIGHTS', 'TRAIN_VAL'], as_index=False).apply(
    lambda x: pd.Series({
        'AUC': roc_auc_score(x.LABEL, x.PROBABILTY),
        'ACCURACY': accuracy_score(x.LABEL, round(x.PROBABILTY)),
        'RECALL': recall_score(x.LABEL, round(x.PROBABILTY)),
        'PRECISION': precision_score(x.LABEL, round(x.PROBABILTY)),
        'F1': f1_score(x.LABEL, round(x.PROBABILTY))
    })
)

selected_cnns = cnn_selection[cnn_selection.TRAIN_VAL == 'val'].sort_values('AUC', ascending=False).\
    groupby('CNN', as_index=False).first()
selected_cnns
selected_cnns = cnn_selection[cnn_selection.TRAIN_VAL == 'val'].sort_values('F1', ascending=False).\
    groupby('CNN', as_index=False).first()
selected_cnns
selected_cnns = cnn_selection[cnn_selection.TRAIN_VAL == 'val'].sort_values('RECALL', ascending=False).\
    groupby('CNN', as_index=False).first()
selected_cnns
selected_cnns = cnn_selection[cnn_selection.TRAIN_VAL == 'val'].sort_values('ACCURACY', ascending=False).\
    groupby('CNN', as_index=False).first()
selected_cnns
selected_cnns = cnn_selection[cnn_selection.TRAIN_VAL == 'val'].sort_values('PRECISION', ascending=False).\
    groupby('CNN', as_index=False).first()
selected_cnns<jupyter_output><empty_output><jupyter_text>Observando los resultados, la métrica a optimizar será AUC y despues se decide el threshold<jupyter_code>selected_cnns = cnn_selection[cnn_selection.TRAIN_VAL == 'val'].sort_values('AUC', ascending=False).\
    groupby('CNN', as_index=False).first()

final_list = []
for _, row in selected_cnns.iterrows():
    final_list.append(
        all_data[(all_data.CNN == row.CNN) & (all_data.FT == row.FT) & (all_data.WEIGHTS == row.WEIGHTS)]
    )

final_df = pd.concat(final_list, ignore_index=True).\
    set_index(['PROCESSED_IMG', 'LABEL', 'TRAIN_VAL', *XGB_COLS[XGB_CONFIG], 'CNN'])['PROBABILTY'].unstack()\
    .reset_index()

final_df
# generación del conjunto de datos de train para gradient boosting
cols = [*XGB_COLS[XGB_CONFIG], *all_data.CNN.unique().tolist()]
train_x = final_df.loc[final_df.TRAIN_VAL == 'train', cols].values
train_y = final_df.loc[final_df.TRAIN_VAL == 'train', 'LABEL']
#### Empiezan los algoritmos de model ensambling
import warnings
warnings.filterwarnings('ignore')
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

clf = GridSearchCV(RandomForestClassifier(random_state=81), 
                   param_grid={'n_estimators': [100, 150, 200, 250, 300], 'max_depth': np.arange(0, 10)}, 
                   scoring='roc_auc', 
                   cv=10) 
clf.fit(train_x, train_y)
data_csv = final_df[['PROCESSED_IMG', 'TRAIN_VAL', 'LABEL']].assign(PREDICTION=clf.predict(final_df[cols]))
data_csv.groupby('TRAIN_VAL').apply(lambda x: pd.Series({
        'AUC': roc_auc_score(x.LABEL, x.PREDICTION),
        'ACCURACY': accuracy_score(x.LABEL, round(x.PREDICTION)),
        'RECALL': recall_score(x.LABEL, round(x.PREDICTION)),
        'PRECISION': precision_score(x.LABEL, round(x.PREDICTION)),
        'F1': f1_score(x.LABEL, round(x.PREDICTION))
    }))
from sklearn.ensemble import GradientBoostingClassifier

clf = GridSearchCV(GradientBoostingClassifier(random_state=81), 
                   param_grid={'n_estimators': [100, 150, 200, 250, 300], 'max_depth': np.arange(0, 10), 'criterion': ['squared_error', 'friedman_mse']}, 
                   scoring='roc_auc', 
                   cv=10) 
clf.fit(train_x, train_y)
data_csv = final_df[['PROCESSED_IMG', 'TRAIN_VAL', 'LABEL']].assign(PREDICTION=clf.predict(final_df[cols]))
data_csv.groupby('TRAIN_VAL').apply(lambda x: pd.Series({
        'AUC': roc_auc_score(x.LABEL, x.PREDICTION),
        'ACCURACY': accuracy_score(x.LABEL, round(x.PREDICTION)),
        'RECALL': recall_score(x.LABEL, round(x.PREDICTION)),
        'PRECISION': precision_score(x.LABEL, round(x.PREDICTION)),
        'F1': f1_score(x.LABEL, round(x.PREDICTION))
    }))
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV

clf = GridSearchCV(RandomForestRegressor(random_state=81), 
                   param_grid={'n_estimators': [100, 150, 200, 250, 300], 'max_depth': np.arange(0, 10)}, 
                   scoring='roc_auc', 
                   cv=10) 
clf.fit(train_x, train_y)
data_csv = final_df[['PROCESSED_IMG', 'TRAIN_VAL', 'LABEL']].assign(PREDICTION=clf.predict(final_df[cols]))
data_csv.groupby('TRAIN_VAL').apply(lambda x: pd.Series({
        'AUC': roc_auc_score(x.LABEL, x.PREDICTION),
        'ACCURACY': accuracy_score(x.LABEL, round(x.PREDICTION)),
        'RECALL': recall_score(x.LABEL, round(x.PREDICTION)),
        'PRECISION': precision_score(x.LABEL, round(x.PREDICTION)),
        'F1': f1_score(x.LABEL, round(x.PREDICTION))
    }))
from sklearn.ensemble import GradientBoostingRegressor

clf = GridSearchCV(GradientBoostingRegressor(random_state=81), 
                   param_grid={'n_estimators': [100, 150, 200, 250, 300], 'max_depth': np.arange(0, 10), 'criterion': ['squared_error', 'friedman_mse']}, 
                   scoring='roc_auc', 
                   cv=10) 
clf.fit(train_x, train_y)
data_csv = final_df[['PROCESSED_IMG', 'TRAIN_VAL', 'LABEL']].assign(PREDICTION=clf.predict(final_df[cols]))
data_csv.groupby('TRAIN_VAL').apply(lambda x: pd.Series({
        'AUC': roc_auc_score(x.LABEL, x.PREDICTION),
        'ACCURACY': accuracy_score(x.LABEL, round(x.PREDICTION)),
        'RECALL': recall_score(x.LABEL, round(x.PREDICTION)),
        'PRECISION': precision_score(x.LABEL, round(x.PREDICTION)),
        'F1': f1_score(x.LABEL, round(x.PREDICTION))
    }))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup