<jupyter_start><jupyter_text>Training Gradient BoostingUse this notebook for quick experimenting<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import roc_auc_score
from pathlib import Path
import os

def change_directory_to_repo():
    """Changes working directory to the repository root folder."""
    current_dir = Path.cwd()
    for parent in current_dir.parents:
        # Repository is the first folder with the .git folder
        files = list(parent.glob(".git"))
        if files:
            os.chdir(str(parent))

change_directory_to_repo()

from scripts.training import train_gb
def get_dataset(type: str, is_train=True):
    if is_train:
        name = 'train.npz'
    else:
        name = 'val.npz'
    
    if type == 'maccs':
        dataset = np.load('data/features/fingerprints/maccs/' + name)
    elif type == 'morgan':
        dataset = np.load('data/features/fingerprints/morgan/' + name)
    elif type == 'topological':
        dataset = np.load('data/features/fingerprints/topological/' + name)
    else:
        raise NotImplementedError()
    
    X = dataset['X']
    y = dataset['y']
    weights = train_gb.get_weight(y)
    return X, y, weights
def get_classifier(subsample: float, max_depth: int, n_estimators: int):
    gb = GradientBoostingClassifier(
        subsample=subsample,
        max_depth=max_depth,
        n_estimators=n_estimators,
    )
    return gb
def evaluate(classifier, X, y_true):
    y_pred = classifier.predict(X)
    score = roc_auc_score(y_true, y_pred)
    return score
def train_and_eval(parameters):
    X_train, y_train, weights = get_dataset(parameters['fingerprint'])
    classifier = get_classifier(parameters['subsample'], parameters['max_depth'], parameters['n_estimators'])
    classifier.fit(X_train, y_train, sample_weight=weights)
    train_score = evaluate(classifier, X_train, y_train)
    print(f'Train: {train_score}')

    X_val, y_val, _ = get_dataset(parameters['fingerprint'], is_train=False)
    val_score = evaluate(classifier, X_val, y_val)
    print(f'Val: {val_score}')
parameters = {
    'fingerprint': 'topological',
    'subsample': 0.3,
    'max_depth': 3,
    'n_estimators': 300
}

train_and_eval(parameters)<jupyter_output>Train: 0.9620964435014022
Val: 0.8268455602902223
{'fraction_non_alphanumeric': 0.10721735502711723, 'fraction_numerical': 0.016687526074259492, 'mean_word_length': 3.8739837398373984, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '330828', 'license': 'MIT', 'n_tokens_mistral': 824, 'n_tokens_neox': 785, 'n_words': 181, 'path': 'notebooks/train/train_gb.ipynb', 'repo_name': 'SteshinSS/serotonin-affinity', 'size': '2397'}