### Install GRANDE dependencies Source: https://github.com/s-marton/grande/blob/master/README.md Command to install core runtime requirements and optional dependencies for notebooks and examples. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install GRANDE via pip Source: https://github.com/s-marton/grande/blob/master/README.md Command to install the latest version of the library from the GitHub repository. ```bash pip install git+https://github.com/s-marton/GRANDE.git ``` -------------------------------- ### Initialize GRANDE PyTorch environment Source: https://github.com/s-marton/grande/blob/master/README.md Setup script for loading data and configuring the environment for binary classification tasks. ```python # Enable GPU (optional) import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Load data from sklearn.model_selection import train_test_split import openml import numpy as np import sklearn dataset = openml.datasets.get_dataset(46915, download_data=True, download_qualities=True, download_features_meta_data=True) X, y, categorical_indicator, attribute_names = dataset.get_data(target=dataset.default_target_attribute) X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_valid, y_train, y_valid = train_test_split(X_temp, y_temp, test_size=0.2, random_state=42) ``` -------------------------------- ### Binary Classification - Complete Workflow Source: https://context7.com/s-marton/grande/llms.txt A complete example demonstrating GRANDE for binary classification with OpenML datasets, including data loading, preprocessing, training, and evaluation. ```APIDOC ## Binary Classification - Complete Workflow A complete example demonstrating GRANDE for binary classification with OpenML datasets, including data loading, preprocessing, training, and evaluation. ### Setup ```python os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Use GPU if available import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, f1_score, roc_auc_score import openml from GRANDE import GRANDE ``` ### Data Loading and Preprocessing ```python # Load dataset from OpenML (churn prediction) dataset = openml.datasets.get_dataset(46915) X, y, categorical_indicator, attribute_names = dataset.get_data( target=dataset.default_target_attribute ) # Prepare data (pandas DataFrames work directly) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) ``` ### Model Initialization and Training ```python # Initialize and train model params = { 'depth': 4, 'n_estimators': 256, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'dropout': 0.2, 'selected_variables': 0.8, 'optimizer': 'adam', 'epochs': 100, 'batch_size': 64, 'early_stopping_epochs': 20, 'problem_type': 'binary', 'random_seed': 42, 'verbose': 1, } model = GRANDE(params=params) # Train with validation set for early stopping model.fit( X=X_train, # Training features (DataFrame or array) y=y_train, # Training labels (Series or array) X_val=X_val, # Validation features (optional) y_val=y_val # Validation labels (optional) ) ``` ### Prediction and Evaluation ```python # Get probability predictions probabilities = model.predict_proba(X_test) # For binary classification: probabilities shape is (n_samples, 2) y_pred_proba = probabilities[:, 1] # Positive class probability y_pred = np.round(y_pred_proba) # Convert to class labels # Evaluate binary classification accuracy = accuracy_score(y_test, y_pred) f1 = f1_score(y_test, y_pred, average='macro') roc_auc = roc_auc_score(y_test, y_pred_proba) print(f"Accuracy: {accuracy:.4f}") print(f"F1 Score: {f1:.4f}") print(f"ROC AUC: {roc_auc:.4f}") # Get class predictions directly predictions = model.predict(X_test) print(classification_report(y_test, predictions)) ``` ``` -------------------------------- ### Example Output Log Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_MULT.ipynb Sample console output showing accuracy, F1 score, and ROC AUC metrics for the evaluated models. ```text Accuracy GRANDE: 0.9570871261378413 F1 Score GRANDE: 0.32602436323366557 ROC AUC GRANDE: 0.487305056710775 Accuracy XGB: 0.9570871261378413 F1 Score XGB: 0.32602436323366557 ROC AUC XGB: 0.47009195809703846 Accuracy CatBoost: 0.9570871261378413 F1 Score CatBoost: 0.32602436323366557 ROC AUC CatBoost: 0.4693427063642092 ``` -------------------------------- ### CatBoost Training Output Log Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_MULT.ipynb Example output log from a CatBoost model training process, showing learning progress and evaluation metrics on a validation set. ```text Learning rate set to 0.109938 0: learn: 0.9182557 test: 0.9188767 best: 0.9188767 (0) total: 130ms remaining: 2m 10s 1: learn: 0.7869023 test: 0.7895294 best: 0.7895294 (1) total: 184ms remaining: 1m 31s 2: learn: 0.6835755 test: 0.6883721 best: 0.6883721 (2) total: 271ms remaining: 1m 30s 3: learn: 0.6018571 test: 0.6085947 best: 0.6085947 (3) total: 333ms remaining: 1m 22s 4: learn: 0.5353552 test: 0.5434583 best: 0.5434583 (4) total: 389ms remaining: 1m 17s 5: learn: 0.4800435 test: 0.4894480 best: 0.4894480 (5) total: 458ms remaining: 1m 15s 6: learn: 0.4340411 test: 0.4449823 best: 0.4449823 (6) total: 524ms remaining: 1m 14s 7: learn: 0.3956256 test: 0.4077597 best: 0.4077597 (7) total: 585ms remaining: 1m 12s 8: learn: 0.3620414 test: 0.3752527 best: 0.3752527 (8) total: 660ms remaining: 1m 12s 9: learn: 0.3337102 test: 0.3481237 best: 0.3481237 (9) total: 721ms remaining: 1m 11s 10: learn: 0.3094077 test: 0.3252190 best: 0.3252190 (10) total: 816ms remaining: 1m 13s 11: learn: 0.2885490 test: 0.3055748 best: 0.3055748 (11) total: 873ms remaining: 1m 11s 12: learn: 0.2707640 test: 0.2891248 best: 0.2891248 (12) total: 942ms remaining: 1m 11s ``` -------------------------------- ### Load and Split Churn Dataset Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_BINARY.ipynb Loads the churn dataset from OpenML, splits it into training, validation, and test sets, and prints the size of each set. Ensure the 'openml' and 'sklearn' libraries are installed. ```python import openml from sklearn.model_selection import train_test_split # Load churn dataset dataset = openml.datasets.get_dataset(46915, download_data=True, download_qualities=True, download_features_meta_data=True) X, y, categorical_indicator, attribute_names = dataset.get_data(target=dataset.default_target_attribute) categorical_feature_indices = [idx for idx, idx_bool in enumerate(categorical_indicator) if idx_bool] X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_valid, y_train, y_valid = train_test_split(X_temp, y_temp, test_size=0.2, random_state=42) print("Training set size:", len(X_train)) print("Validation set size:", len(X_valid)) print("Test set size:", len(X_test)) ``` -------------------------------- ### XGBoost Training Output Log Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_MULT.ipynb Example output log from an XGBoost model training process, showing evaluation metrics at each boosting round. ```text [0] validation_0-mlogloss:0.74545 [1] validation_0-mlogloss:0.54790 [2] validation_0-mlogloss:0.42437 [3] validation_0-mlogloss:0.34345 [4] validation_0-mlogloss:0.29068 [5] validation_0-mlogloss:0.25474 [6] validation_0-mlogloss:0.23194 [7] validation_0-mlogloss:0.21750 [8] validation_0-mlogloss:0.20821 [9] validation_0-mlogloss:0.20224 [10] validation_0-mlogloss:0.19929 [11] validation_0-mlogloss:0.19864 [12] validation_0-mlogloss:0.19976 [13] validation_0-mlogloss:0.20059 [14] validation_0-mlogloss:0.20224 [15] validation_0-mlogloss:0.20517 [16] validation_0-mlogloss:0.20575 [17] validation_0-mlogloss:0.20623 [18] validation_0-mlogloss:0.20773 [19] validation_0-mlogloss:0.20963 [20] validation_0-mlogloss:0.21130 [21] validation_0-mlogloss:0.21192 [22] validation_0-mlogloss:0.21233 [23] validation_0-mlogloss:0.21344 [24] validation_0-mlogloss:0.21629 [25] validation_0-mlogloss:0.21784 [26] validation_0-mlogloss:0.21867 [27] validation_0-mlogloss:0.21993 [28] validation_0-mlogloss:0.22265 [29] validation_0-mlogloss:0.22523 [30] validation_0-mlogloss:0.22601 ``` -------------------------------- ### Train CatBoost Classifier with Early Stopping Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_REG.ipynb Train a CatBoost Classifier model. This example shows how to use the `Pool` object for data with categorical features and includes early stopping. ```python from catboost import CatBoostClassifier, Pool model_catboost = CatBoostClassifier(n_estimators=1000, early_stopping_rounds=20) train_data = Pool( data=X_train_raw, label=y_train, cat_features=categorical_feature_indices, #weight=calculate_sample_weights(y_train) ) eval_data = Pool( data=X_valid_raw, label=y_valid, cat_features=categorical_feature_indices, #weight=calculate_sample_weights(y_valid), ) model_catboost.fit(X=train_data, eval_set=eval_data) preds_catboost = model_catboost.predict_proba(X_test_raw) ``` -------------------------------- ### Train CatBoost Regressor with Early Stopping Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_REG.ipynb Train a CatBoost Regressor model. This example demonstrates using the `Pool` object for data with categorical features and applies early stopping. ```python from catboost import CatBoostRegressor, Pool model_catboost = CatBoostRegressor(n_estimators=1000, early_stopping_rounds=20) train_data = Pool( data=X_train_raw, label=y_train, cat_features=categorical_feature_indices, ) eval_data = Pool( data=X_valid_raw, label=y_valid, cat_features=categorical_feature_indices, ) model_catboost.fit(X=train_data, eval_set=eval_data) preds_catboost = model_catboost.predict(X_test_raw) ``` -------------------------------- ### predict() - Get Class Predictions Source: https://context7.com/s-marton/grande/llms.txt The predict method returns class labels for classification tasks or continuous predictions for regression. This is a convenience method that applies argmax to probability predictions for classification. ```APIDOC ## predict() - Get Class Predictions The predict method returns class labels for classification tasks or continuous predictions for regression. This is a convenience method that applies argmax to probability predictions for classification. ### Method `predict` ### Parameters #### Request Body - **X** (pandas DataFrame or numpy array) - Required - Features for prediction. ### Request Example ```python import numpy as np from sklearn.metrics import classification_report from GRANDE import GRANDE # Get class predictions directly predictions = model.predict(X_test) # For classification: returns class labels (0, 1, 2, ...) print(f"Predictions shape: {predictions.shape}") print(f"Unique classes: {np.unique(predictions)}") print(f"Sample predictions: {predictions[:10]}") ``` ### Response #### Success Response (200) - **predictions** (numpy array) - An array of predicted class labels for each sample. #### Response Example ```json { "predictions": [0, 1, 1, 0, 1, 0, 0, 1, 1, 0] } ``` ### Evaluation Example ```python # Classification report print(classification_report(y_test, predictions)) ``` ``` -------------------------------- ### Configure and Train GRANDE Model Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_BINARY.ipynb Instantiate the GRANDE model with specified parameters and train it using training and validation datasets. This snippet shows the model initialization and the fit method call. ```python params = { 'early_stopping_epochs': 50, 'use_freq_enc': False, 'use_robust_scale_smoothing': False, 'problem_type': 'binary', 'random_seed': 42, 'verbose': 2, } model_grande = GRANDE(params=params) model_grande.fit(X=X_train, y=y_train, X_val=X_valid, y_val=y_valid) preds_grande = model_grande.predict_proba(X_test) ``` -------------------------------- ### Prepare and Train GRANDE Model Source: https://context7.com/s-marton/grande/llms.txt Initializes the GRANDE model with specific hyperparameters and trains it using a validation set for early stopping. ```python X = pd.DataFrame({ 'age': [25, 30, 35, 40, 45, 50, 55, 60], 'income': [50000, 60000, 75000, 80000, 90000, 100000, 110000, 120000], 'category': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'], 'is_member': [True, False, True, True, False, True, False, True] }) y = pd.Series([0, 0, 1, 1, 0, 1, 1, 1]) # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) # Initialize and train model params = { 'depth': 4, 'n_estimators': 256, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'dropout': 0.2, 'selected_variables': 0.8, 'optimizer': 'adam', 'epochs': 100, 'batch_size': 64, 'early_stopping_epochs': 20, 'problem_type': 'binary', 'random_seed': 42, 'verbose': 1, } model = GRANDE(params=params) # Train with validation set for early stopping model.fit( X=X_train, # Training features (DataFrame or array) y=y_train, # Training labels (Series or array) X_val=X_val, # Validation features (optional) y_val=y_val # Validation labels (optional) ) ``` -------------------------------- ### predict_proba() - Get Probability Predictions Source: https://context7.com/s-marton/grande/llms.txt The predict_proba method returns class probabilities for classification tasks. For binary classification, it returns a 2D array with probabilities for both classes. ```APIDOC ## predict_proba() - Get Probability Predictions The predict_proba method returns class probabilities for classification tasks or predicted values for regression. For binary classification, it returns a 2D array with probabilities for both classes. ### Method `predict_proba` ### Parameters #### Request Body - **X** (pandas DataFrame or numpy array) - Required - Training features. - **y** (pandas Series or numpy array) - Required - Training labels. - **X_val** (pandas DataFrame or numpy array) - Optional - Validation features. - **y_val** (pandas Series or numpy array) - Optional - Validation labels. ### Request Example ```python import numpy as np from sklearn.metrics import roc_auc_score, accuracy_score, f1_score from GRANDE import GRANDE # Assuming model is already trained... # Get probability predictions probabilities = model.predict_proba(X_test) # For binary classification: probabilities shape is (n_samples, 2) # probabilities[:, 0] = P(class=0) # probabilities[:, 1] = P(class=1) print(f"Prediction shape: {probabilities.shape}") print(f"Sample predictions:\n{probabilities[:5]}") ``` ### Response #### Success Response (200) - **probabilities** (numpy array) - A 2D array where each row corresponds to a sample and columns represent class probabilities. For binary classification, shape is (n_samples, 2). #### Response Example ```json { "probabilities": [ [0.8234, 0.1766], [0.3421, 0.6579], [0.1245, 0.8755], [0.9123, 0.0877], [0.4532, 0.5468] ] } ``` ### Evaluation Example ```python # Evaluate binary classification y_pred_proba = probabilities[:, 1] # Positive class probability y_pred = np.round(y_pred_proba) # Convert to class labels accuracy = accuracy_score(y_test, y_pred) f1 = f1_score(y_test, y_pred, average='macro') roc_auc = roc_auc_score(y_test, y_pred_proba) print(f"Accuracy: {accuracy:.4f}") print(f"F1 Score: {f1:.4f}") print(f"ROC AUC: {roc_auc:.4f}") ``` ``` -------------------------------- ### Initialize and Train GRANDE Model Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_MULT.ipynb Configures model hyperparameters, initializes the GRANDE instance, and executes the training and prediction workflow. ```python from GRANDE import GRANDE params = { 'depth': 5, 'n_estimators': 1024, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.01, 'learning_rate_leaf': 0.05, 'learning_rate_embedding': 0.01, 'use_category_embeddings': True, 'embedding_dim_cat': 8, 'use_numeric_embeddings': False, 'embedding_dim_num': 8, 'embedding_threshold': 1, 'loo_cardinality': 10, 'dropout': 0.2, 'selected_variables': 0.8, 'data_subset_fraction': 1.0, 'bootstrap': False, 'missing_values': False, 'optimizer': 'adam', #nadam, radam, adamw, adam 'cosine_decay_restarts': False, 'reduce_on_plateau_scheduler': True, 'label_smoothing': 0.0, 'use_class_weights': False, 'focal_loss': False, 'swa': False, 'es_metric': True, # if True use AUC for binary, MSE for regression, val_loss for multiclass 'epochs': 250, 'batch_size': 256, 'early_stopping_epochs': 50, 'use_freq_enc': False, 'use_robust_scale_smoothing': False, 'problem_type': 'multiclass', 'random_seed': 42, 'verbose': 2, } model_grande = GRANDE(params=params) model_grande.fit(X=X_train, y=y_train, X_val=X_valid, y_val=y_valid) preds_grande = model_grande.predict_proba(X_test) ``` -------------------------------- ### Multi-class Classification Workflow Source: https://context7.com/s-marton/grande/llms.txt Demonstrates the complete pipeline for multi-class classification using GRANDE. Includes dataset loading, parameter configuration with 'multiclass' problem type, and evaluation using macro-averaged metrics. ```python import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, f1_score, roc_auc_score import pandas as pd from GRANDE import GRANDE # Load multi-class dataset iris = load_iris() X = pd.DataFrame(iris.data, columns=iris.feature_names) y = pd.Series(iris.target) # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) # Configure for multi-class params = { 'depth': 4, 'n_estimators': 512, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'dropout': 0.2, 'selected_variables': 0.8, 'optimizer': 'adam', 'epochs': 200, 'batch_size': 32, 'early_stopping_epochs': 30, 'es_metric': True, # Use validation loss for multiclass 'problem_type': 'multiclass', # Multi-class classification 'random_seed': 42, 'verbose': 1, } model = GRANDE(params=params) model.fit(X=X_train, y=y_train, X_val=X_val, y_val=y_val) # Get predictions preds = model.predict_proba(X_test) # Shape: (n_samples, n_classes) pred_labels = np.argmax(preds, axis=1) # Evaluate accuracy = accuracy_score(y_test, pred_labels) f1 = f1_score(y_test, pred_labels, average='macro') roc_auc = roc_auc_score(y_test, preds, multi_class='ovo', average='macro') print(f"Multi-class Results:") print(f"Accuracy: {accuracy:.4f}") print(f"F1 Score (macro): {f1:.4f}") print(f"ROC AUC (ovo): {roc_auc:.4f}") ``` -------------------------------- ### Initialize and Train GRANDE Model Source: https://github.com/s-marton/grande/blob/master/README.md Initializes the GRANDE model with specified parameters and trains it using provided training and validation data. Ensure X_train, y_train, X_valid, and y_valid are defined before execution. ```python from GRANDE import GRANDE params = { 'depth': 5, 'n_estimators': 1024, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'learning_rate_embedding': 0.02, # used if embeddings are enabled # Embeddings (set True to enable) 'use_category_embeddings': False, # True to enable 'embedding_dim_cat': 8, 'use_numeric_embeddings': False, # True to enable 'embedding_dim_num': 8, 'embedding_threshold': 1, # low-cardinality split for categorical embeddings 'loo_cardinality': 10, # high-cardinality split for encoders 'dropout': 0.2, 'selected_variables': 0.8, 'data_subset_fraction': 1.0, 'bootstrap': False, 'missing_values': False, 'optimizer': 'adam', # options: nadam, radam, adamw, adam 'cosine_decay_restarts': False, 'reduce_on_plateau_scheduler': True, 'label_smoothing': 0.0, 'use_class_weights': False, 'focal_loss': False, 'swa': False, 'es_metric': True, # AUC for binary, MSE for regression, val_loss for multiclass 'epochs': 250, 'batch_size': 256, 'early_stopping_epochs': 50, 'use_freq_enc': False, 'use_robust_scale_smoothing': False, # Important: use problem_type, not objective 'problem_type': 'binary', # {'binary', 'multiclass', 'regression'} 'random_seed': 42, 'verbose': 2, } model_grande = GRANDE(params=params) model_grande.fit(X=X_train, y=y_train, X_val=X_valid, y_val=y_valid) ``` -------------------------------- ### GRANDE Model Configuration Parameters Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_BINARY.ipynb Defines a dictionary of parameters for configuring the GRANDE model. This includes settings for tree structure, learning rates, embedding strategies, dropout, data subsetting, and optimization. Ensure the GRANDE library is imported. ```python from GRANDE import GRANDE params = { 'depth': 5, 'n_estimators': 1024, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'learning_rate_embedding': 0.02, 'use_category_embeddings': False, 'embedding_dim_cat': 8, 'use_numeric_embeddings': False, 'embedding_dim_num': 8, 'embedding_threshold': 1, 'loo_cardinality': 10, 'dropout': 0.2, 'selected_variables': 0.8, 'data_subset_fraction': 1.0, 'bootstrap': False, 'missing_values': False, 'optimizer': 'adam', #nadam, radam, adamw, adam 'cosine_decay_restarts': False, 'reduce_on_plateau_scheduler': True, 'label_smoothing': 0.0, 'use_class_weights': False, 'focal_loss': False, 'swa': False, 'es_metric': True, # if True use AUC for binary, MSE for regression, val_loss for multiclass 'epochs': 250, 'batch_size': 256, } ``` -------------------------------- ### Binary Classification Workflow Source: https://context7.com/s-marton/grande/llms.txt Configures and trains a GRANDE model for binary classification tasks. Requires pre-split data and specific parameter settings for binary problem types. ```python # Split data: 64% train, 16% validation, 20% test X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_val, y_train, y_val = train_test_split(X_temp, y_temp, test_size=0.2, random_state=42) print(f"Training: {len(X_train)}, Validation: {len(X_val)}, Test: {len(X_test)}") # Configure GRANDE for binary classification params = { 'depth': 5, 'n_estimators': 1024, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'dropout': 0.2, 'selected_variables': 0.8, 'optimizer': 'adam', 'reduce_on_plateau_scheduler': True, 'epochs': 250, 'batch_size': 256, 'early_stopping_epochs': 50, 'es_metric': True, # Use AUC for early stopping 'problem_type': 'binary', 'random_seed': 42, 'verbose': 1, } # Train model model = GRANDE(params=params) model.fit(X=X_train, y=y_train, X_val=X_val, y_val=y_val) # Evaluate preds = model.predict_proba(X_test) y_test_np = y_test.values.codes.astype(np.float64) accuracy = accuracy_score(y_test_np, np.round(preds[:, 1])) f1 = f1_score(y_test_np, np.round(preds[:, 1]), average='macro') roc_auc = roc_auc_score(y_test_np, preds[:, 1]) print(f"\nTest Results:") print(f"Accuracy: {accuracy:.4f}") print(f"F1 Score (macro): {f1:.4f}") print(f"ROC AUC: {roc_auc:.4f}") ``` -------------------------------- ### Initialize GRANDE Model Source: https://context7.com/s-marton/grande/llms.txt Initialize a GRANDE model with a comprehensive parameter dictionary. Configure model architecture, learning rates, embedding options, regularization, training settings, loss functions, scheduling, preprocessing, and task type. ```python from GRANDE import GRANDE # Initialize GRANDE model with configuration parameters params = { # Model architecture 'depth': 5, # Tree depth (default: 5) 'n_estimators': 1024, # Number of trees in ensemble # Learning rates for different components 'learning_rate_weights': 0.001, # LR for estimator weights 'learning_rate_index': 0.01, # LR for split feature selection 'learning_rate_values': 0.05, # LR for split threshold values 'learning_rate_leaf': 0.05, # LR for leaf node values 'learning_rate_embedding': 0.02, # LR for embeddings (if enabled) # Embedding options 'use_category_embeddings': False, # Enable categorical embeddings 'embedding_dim_cat': 8, # Categorical embedding dimension 'use_numeric_embeddings': False, # Enable numeric embeddings 'embedding_dim_num': 8, # Numeric embedding dimension 'embedding_threshold': 1, # Cardinality threshold for cat embeddings 'loo_cardinality': 10, # High-cardinality threshold for LOO encoding # Regularization 'dropout': 0.2, # Dropout rate for estimator weights 'selected_variables': 0.8, # Fraction of features per estimator 'data_subset_fraction': 1.0, # Data fraction per estimator 'bootstrap': False, # Bootstrap sampling # Training configuration 'optimizer': 'adam', # Options: adam, nadam, radam, adamw 'epochs': 250, # Maximum training epochs 'batch_size': 256, # Training batch size 'early_stopping_epochs': 50, # Patience for early stopping # Loss and scheduling 'cosine_decay_restarts': False, # Cosine annealing with restarts 'reduce_on_plateau_scheduler': True, # Reduce LR on plateau 'label_smoothing': 0.0, # Label smoothing factor 'use_class_weights': False, # Balance class weights 'focal_loss': False, # Use focal loss for imbalanced data 'swa': False, # Stochastic weight averaging 'es_metric': True, # Use task-specific metric for early stopping # Preprocessing 'use_freq_enc': False, # Frequency encoding for categoricals 'use_robust_scale_smoothing': False, # Robust scaling with smooth clipping 'missing_values': False, # Handle missing values natively # Task type and misc 'problem_type': 'binary', # Options: binary, multiclass, regression 'random_seed': 42, # Random seed for reproducibility 'verbose': 2, # Verbosity level (0, 1, 2) } model = GRANDE(params=params) ``` -------------------------------- ### Load Dataset for Binary Classification Source: https://context7.com/s-marton/grande/llms.txt Demonstrates loading a dataset from OpenML for a complete classification workflow. ```python import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Use GPU if available import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, f1_score, roc_auc_score import openml from GRANDE import GRANDE # Load dataset from OpenML (churn prediction) dataset = openml.datasets.get_dataset(46915) X, y, categorical_indicator, attribute_names = dataset.get_data( target=dataset.default_target_attribute ) ``` -------------------------------- ### Configure and Train GRANDE Model Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_REG.ipynb Defines hyperparameters for the GRANDE model and executes the training process on the prepared dataset. ```python from GRANDE import GRANDE params = { 'depth': 5, 'n_estimators': 1024, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.15, 'learning_rate_embedding': 0.01, 'use_category_embeddings': True, 'embedding_dim_cat': 8, 'use_numeric_embeddings': False, 'embedding_dim_num': 8, 'embedding_threshold': 1, 'loo_cardinality': 10, 'dropout': 0.2, 'selected_variables': 0.8, 'data_subset_fraction': 1.0, 'bootstrap': False, 'missing_values': False, 'optimizer': 'adam', #nadam, radam, adamw, adam 'cosine_decay_restarts': False, 'reduce_on_plateau_scheduler': True, 'label_smoothing': 0.0, 'use_class_weights': False, 'focal_loss': False, 'swa': False, 'es_metric': True, # if True use AUC for binary, MSE for regression, val_loss for multiclass 'epochs': 250, 'batch_size': 256, 'early_stopping_epochs': 50, 'use_freq_enc': False, 'use_robust_scale_smoothing': False, 'problem_type': 'regression', 'random_seed': 42, 'verbose': 2, } model_grande = GRANDE(params=params) model_grande.fit(X=X_train, y=y_train, X_val=X_valid, y_val=y_valid) preds_grande = model_grande.predict_proba(X_test) ``` -------------------------------- ### Import Project Libraries Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_REG.ipynb Imports essential libraries for machine learning tasks, including data splitting, data loading, categorical encoding, and numerical operations. ```python from sklearn.model_selection import train_test_split import openml import category_encoders as ce import numpy as np import sklearn ``` -------------------------------- ### Load and Prepare Healthcare Insurance Dataset Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_REG.ipynb Fetches the healthcare insurance dataset from OpenML and splits it into training, validation, and test sets. ```python dataset = openml.datasets.get_dataset(46931, download_data=True, download_qualities=True, download_features_meta_data=True) X, y, categorical_indicator, attribute_names = dataset.get_data(target=dataset.default_target_attribute) categorical_feature_indices = [idx for idx, idx_bool in enumerate(categorical_indicator) if idx_bool] X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_valid, y_train, y_valid = train_test_split(X_temp, y_temp, test_size=0.2, random_state=42) print("Training set size:", len(X_train)) print("Validation set size:", len(X_valid)) print("Test set size:", len(X_test)) X_train.head() ``` -------------------------------- ### Display Training Data Head Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_BINARY.ipynb Displays the first few rows of the training data (X_train) to give a preview of the dataset's structure. This is useful for a quick inspection after data loading and splitting. ```python X_train.head() ``` -------------------------------- ### Evaluate Model Performance Metrics Source: https://github.com/s-marton/grande/blob/master/GRANDE_minimal_example_with_comparison_REG.ipynb Calculates and prints performance metrics for GRANDE, XGBoost, and CatBoost models based on the specified problem type. ```python if params['problem_type'] == 'binary': accuracy = sklearn.metrics.accuracy_score(y_test, np.round(preds_grande[:,1])) f1_score = sklearn.metrics.f1_score(y_test, np.round(preds_grande[:,1]), average='macro') roc_auc = sklearn.metrics.roc_auc_score(y_test, preds_grande[:,1], average='macro', multi_class='ovo') print('Accuracy GRANDE:', accuracy) print('F1 Score GRANDE:', f1_score) print('ROC AUC GRANDE:', roc_auc) print('\n') accuracy = sklearn.metrics.accuracy_score(y_test, np.round(preds_xgb[:,1])) f1_score = sklearn.metrics.f1_score(y_test, np.round(preds_xgb[:,1]), average='macro') roc_auc = sklearn.metrics.roc_auc_score(y_test, preds_xgb[:,1], average='macro', multi_class='ovo') print('Accuracy XGB:', accuracy) print('F1 Score XGB:', f1_score) print('ROC AUC XGB:', roc_auc) print('\n') accuracy = sklearn.metrics.accuracy_score(y_test, np.round(preds_catboost[:,1])) f1_score = sklearn.metrics.f1_score(y_test, np.round(preds_catboost[:,1]), average='macro') roc_auc = sklearn.metrics.roc_auc_score(y_test, preds_catboost[:,1], average='macro', multi_class='ovo') print('Accuracy CatBoost:', accuracy) print('F1 Score CatBoost:', f1_score) print('ROC AUC CatBoost:', roc_auc) print('\n') elif params['problem_type'] == 'multiclass': accuracy = sklearn.metrics.accuracy_score(y_test, np.argmax(preds_grande, axis=1)) f1_score = sklearn.metrics.f1_score(y_test, np.argmax(preds_grande, axis=1), average='macro') roc_auc = sklearn.metrics.roc_auc_score(y_test, preds_grande, average='macro', multi_class='ovo', labels=[i for i in range(preds_grande.shape[1])]) print('Accuracy GRANDE:', accuracy) print('F1 Score GRANDE:', f1_score) print('ROC AUC GRANDE:', roc_auc) print('\n') accuracy = sklearn.metrics.accuracy_score(y_test, np.argmax(preds_xgb, axis=1)) f1_score = sklearn.metrics.f1_score(y_test, np.argmax(preds_xgb, axis=1), average='macro') roc_auc = sklearn.metrics.roc_auc_score(y_test, preds_xgb, average='macro', multi_class='ovo', labels=[i for i in range(preds_grande.shape[1])]) print('Accuracy XGB:', accuracy) print('F1 Score XGB:', f1_score) print('ROC AUC XGB:', roc_auc) print('\n') accuracy = sklearn.metrics.accuracy_score(y_test, np.argmax(preds_catboost, axis=1)) f1_score = sklearn.metrics.f1_score(y_test, np.argmax(preds_catboost, axis=1), average='macro') roc_auc = sklearn.metrics.roc_auc_score(y_test, preds_catboost, average='macro', multi_class='ovo', labels=[i for i in range(preds_grande.shape[1])]) print('Accuracy CatBoost:', accuracy) print('F1 Score CatBoost:', f1_score) print('ROC AUC CatBoost:', roc_auc) print('\n') else: mean_absolute_error = sklearn.metrics.mean_absolute_error(y_test, preds_grande) root_mean_squared_error = np.sqrt(((y_test - preds_grande) ** 2).mean()) r2_score = sklearn.metrics.r2_score(y_test, preds_grande) print('MAE GRANDE:', mean_absolute_error) print('RMSE GRANDE:', root_mean_squared_error) print('R2 Score GRANDE:', r2_score) print('\n') mean_absolute_error = sklearn.metrics.mean_absolute_error(y_test, preds_xgb) root_mean_squared_error = np.sqrt(((y_test - preds_xgb) ** 2).mean()) r2_score = sklearn.metrics.r2_score(y_test, preds_xgb) print('MAE XGB:', mean_absolute_error) print('RMSE XGB:', root_mean_squared_error) print('R2 Score XGB:', r2_score) print('\n') mean_absolute_error = sklearn.metrics.mean_absolute_error(y_test, preds_catboost) root_mean_squared_error = np.sqrt(((y_test - preds_catboost) ** 2).mean()) r2_score = sklearn.metrics.r2_score(y_test, preds_catboost) print('MAE CatBoost:', mean_absolute_error) print('RMSE CatBoost:', root_mean_squared_error) print('R2 Score CatBoost:', r2_score) print('\n') ``` -------------------------------- ### Evaluate regression metrics Source: https://context7.com/s-marton/grande/llms.txt Calculate and print standard regression performance metrics including MSE, RMSE, MAE, and R2 score. ```python mse = mean_squared_error(y_test, preds) rmse = np.sqrt(mse) mae = mean_absolute_error(y_test, preds) r2 = r2_score(y_test, preds) print(f"Regression Results:") print(f"MSE: {mse:.4f}") print(f"RMSE: {rmse:.4f}") print(f"MAE: {mae:.4f}") print(f"R2 Score: {r2:.4f}") ``` -------------------------------- ### Regression Workflow Source: https://context7.com/s-marton/grande/llms.txt Implements a regression model using GRANDE. Note that for regression, predict_proba returns continuous values directly. ```python import numpy as np from sklearn.datasets import fetch_california_housing from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score import pandas as pd from GRANDE import GRANDE # Load regression dataset housing = fetch_california_housing() X = pd.DataFrame(housing.data, columns=housing.feature_names) y = pd.Series(housing.target) # Use subset for faster training X = X.iloc[:5000] y = y.iloc[:5000] # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) # Configure for regression params = { 'depth': 5, 'n_estimators': 1024, 'learning_rate_weights': 0.001, 'learning_rate_index': 0.01, 'learning_rate_values': 0.05, 'learning_rate_leaf': 0.05, 'dropout': 0.2, 'selected_variables': 0.8, 'optimizer': 'adam', 'epochs': 200, 'batch_size': 256, 'early_stopping_epochs': 30, 'es_metric': True, # Use MSE for early stopping 'problem_type': 'regression', # Regression task 'random_seed': 42, 'verbose': 1, } model = GRANDE(params=params) model.fit(X=X_train, y=y_train, X_val=X_val, y_val=y_val) # Get predictions (returns continuous values) preds = model.predict_proba(X_test) # For regression, returns values directly ``` -------------------------------- ### Train GRANDE Model with fit() Source: https://context7.com/s-marton/grande/llms.txt Train the GRANDE model using the fit method on tabular data. The method handles preprocessing, including encoding, normalization, and embeddings. Validation data can be provided for early stopping and model selection. ```python import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from GRANDE import GRANDE ```