<jupyter_start><jupyter_text>Carga de Datos<jupyter_code>df_completo = pd.read_csv(path+'acetylcholinesterase_02_bioactivity_data_preprocessed_token_descriptors.csv')
X= df_completo.drop(['molecule_chembl_id', 'canonical_smiles', 'standard_value',
       'standard_value_norm', 'pIC50', 'X_seq', 'X_seq_pad', 'MW', 'LogP',
       'NumHDonors', 'NumHAcceptors', 'bioactivity_class', 'Name'], axis=1)
y = df_completo.pIC50.values<jupyter_output><empty_output><jupyter_text>Split de Datos<jupyter_code>X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)<jupyter_output><empty_output><jupyter_text>Modelo LighGBM<jupyter_code>def R2(y_true, y_pred):
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )
def R2_numpy(y_true, y_pred):
    SS_res =  np.sum(np.square( y_true-y_pred ))
    SS_tot = np.sum(np.square( y_true - np.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + np.finfo(float).eps ) )
cat_vars = X.columns.to_list()
min_child_samples=100  # cant minima de hoja hija para hacer split
n_estimators=800000  # cant max de arboles secuenciales, por lo general se pone numero alto, ya q nunca llega por early stopping
learning_rate=0.005
model = LGBMRegressor(min_child_samples=min_child_samples, n_estimators=n_estimators, learning_rate=learning_rate )
fit_params={"early_stopping_rounds":100, 
            "eval_metric" : 'r2',
            "eval_set" : [(X_val, y_val.reshape(-1))],
            'eval_names': ['valid'],
            'verbose': 100,
            'feature_name': 'auto', # that's actually the default
            'categorical_feature': cat_vars  # le paso cuales son los cat para q haga EFB(exlusive Feature Bunding)
           }
model.fit(X_train, y_train.reshape(-1), **fit_params)
model.score(X_val, y_val)
y_val_pred = model.predict(X_val)
print('val_R2: ',R2_numpy(y_val, y_val_pred))<jupyter_output>val_R2:  0.35426855506091404<jupyter_text>HYPER-SEARCH PARAMETERS<jupyter_code>#Defino con que hyper-parametros realizo la busqueda
dim_learning_rate= Real(low=0.01, high=1, prior='log-uniform', name='learning_rate')
dim_boosting_type = Categorical(['gbdt'], name='boosting_type')
dim_subsample = Real(low=0.01, high=1.0, prior='log-uniform', name='subsample')
dim_subsample_freq = Integer(0, 10, name='subsample_freq')
dim_max_depth= Integer(1, 20, name='max_depth') # 	Larger is usually better, but overfitting speed increases.
dim_num_leaves= Integer(2, 100,name='num_leaves') #max number of leaves in one tree        
dim_min_child_samples= Integer(1, 200, name='min_child_samples') # minimal number of data in one leaf
dim_reg_lambda= Real(0.001, 100, 'log-uniform', name='reg_lambda') # L2 regularization
dim_reg_alpha= Real(0.001, 100, 'log-uniform', name='reg_alpha') # L1 regularization
dim_colsample_bytree= Real(0.1, 1.0, 'uniform', name='colsample_bytree') # enabler of bagging fraction
dim_min_child_weight=Integer(0, 10, name='min_child_weight') # minimal number of data in one leaf.
dim_n_estimators= Integer(1, 1000, name='n_estimators') # cant. de estimadores secuenciales (se pone alto stopea earlystopping)        
dimensions = [
    #Regularizacion --> complejidad del modelo
    dim_max_depth,
    dim_min_child_weight,
    dim_reg_lambda,
    dim_reg_alpha,

    #Regularizacion --> aletoriedad
    dim_subsample, 
    dim_subsample_freq,
    dim_colsample_bytree,

    #Otros
    dim_num_leaves,
    dim_min_child_samples,            
    dim_n_estimators,
    dim_boosting_type,
    

]

max_iterSearch = 300
best_accuracy = -10.0
best_parameters = [{'teste': 1}]

# Callback de Checkpoint Saver 
if not os.path.isfile('001_Data_retrieve.ipynb'):
    checkpoint_saver = CheckpointSaver(path+'/checkpoint.pkl', compress=9)
else:
    checkpoint_saver = CheckpointSaver('checkpoint.pkl', compress=9)
#deltaXStopper = DeltaXStopper(0.001) 
#deltaLineStoper = DeadlineStopper(60*5)


# Funcion para LightGBM y skopt
@use_named_args(dimensions=dimensions)
def fitness (max_depth, num_leaves, min_child_samples,reg_lambda, reg_alpha, colsample_bytree, min_child_weight,
            boosting_type, subsample, subsample_freq, n_estimators):
   

    model = LGBMRegressor( max_depth=max_depth,
                          min_child_samples=min_child_samples, 
                          reg_lambda=reg_lambda,
                          subsample=subsample, subsample_freq=subsample_freq,
                          colsample_bytree=colsample_bytree,

                          #num_leaves=num_leaves, 
                          #reg_alpha=reg_alpha,
                          #min_child_weight= min_child_weight, n_estimators=n_estimators,
                          #boosting_type=boosting_type, 
                         n_jobs=2)
    
    ### PARA CON CV ###
    #fit_params={"early_stopping_rounds":30, 
    #      "eval_metric" : 'r2', 
    #      #"eval_set" : [(X_val, y_val.reshape(-1))],
    #      #'eval_names': ['valid'],
    #      'verbose': 0,
    #      'feature_name': 'auto', # that's actually the default
    #      #'categorical_feature': cat_vars
    #   }
    #score = np.mean(cross_val_score(model, X_train, y_train.reshape(-1), cv=KFold(n_splits=4), n_jobs=2, verbose=0
    #                ,scoring= 'r2'
    #                , fit_params= fit_params))
    ###-------------

    ### PARA SIN CV ##
    fit = model.fit(X_train, y_train.reshape(-1), 
                eval_set=[(X_val, y_val.reshape(-1))],
                eval_metric='r2', 
                #feature_name='auto', 
                #categorical_feature=cat_vars, 
                verbose=False,
                early_stopping_rounds=100)
    y_pred  =  model.predict(X_val)  
    score = R2_numpy(y_val, y_pred)
    ###----------------
    

    global best_accuracy
    global best_parameters
    
    if score > best_accuracy:
        #print('mejor score actual:', score)
        #print('mejor score anterior:', best_accuracy)
        best_parameters[0] = model.get_params()        
        best_accuracy = score
        
    del model
    
    return -score

%time search_result = gp_minimize(func=fitness, dimensions=dimensions, n_calls=max_iterSearch, n_jobs=2, verbose=True, acq_func='LCB',callback=[checkpoint_saver])

#PRINT DE RESULTADO

print('best score custom function', best_accuracy)
#print('best score gp_minimize', search_result.fun)
print('best parametrers gp_minimize')
print('best parametrers custom function:')
best_parameters
#plot_convergence(search_result)
#ppE = plot_evaluations(search_result)
#pp = plot_objective(search_result)
model = LGBMRegressor(**best_parameters[0])

model.fit(X_train, y_train.reshape(-1), **fit_params)
y_pred_train = model.predict(X_train)
y_pred_val = model.predict(X_val)
print('R2 para train:', R2_numpy(y_train, y_pred_train))
print('R2 para val  :', R2_numpy(y_val, y_pred_val))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12163336229365769, 'fraction_numerical': 0.022009846510280914, 'mean_word_length': 3.6732070365358593, 'pattern_counts': {'":': 2, '<': 16, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '543349', 'license': 'Apache-2.0', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2466, 'n_words': 439, 'path': '009_Gradient_Boosting.ipynb', 'repo_name': 'GuilleBur/tp_final_bioinformatica', 'size': '6906'}