Models Module
energy_gnome.models
energy_gnome.models.E3NNRegressor
Bases: BaseRegressor
Source code in energy_gnome/models/e3nn/regressor.py
Python | |
---|---|
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
|
__init__(model_name, target_property, models_dir=MODELS_DIR, figures_dir=FIGURES_DIR)
This class extends BaseRegressor
to implement an equivariant neural network (E3NN) for regression tasks. It sets up the necessary directory structure and configurations for training models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name | str | Name of the model, used to create subdirectories. | required |
target_property | str | The target property the model is trained to predict. | required |
models_dir | Path | str | Directory for storing trained model weights. Defaults to | MODELS_DIR |
figures_dir | Path | str | Directory for saving figures and visualizations. Defaults to | FIGURES_DIR |
Attributes:
Name | Type | Description |
---|---|---|
_model_spec | str | Specification string used for model identification. |
l_max | int | Maximum order of the spherical harmonics used in the E3NN model (default is 2). |
r_max | int | Cutoff radius used in the E3NN model (default is 4). |
conv_layers | int | Number of nonlinearities (number of convolutions = layers + 1, default is 2). |
Source code in energy_gnome/models/e3nn/regressor.py
compile_(num_neighbors, lr=DEFAULT_OPTIM_SETTINGS['lr'], wd=DEFAULT_OPTIM_SETTINGS['wd'], optimizer_class=torch.optim.AdamW, scheduler_class=torch.optim.lr_scheduler.ExponentialLR, scheduler_settings=dict(gamma=0.99), loss_function=torch.nn.L1Loss)
Compile and configure the model for training, setting up necessary components such as the optimizer, learning rate scheduler, and loss function, and then builds the regressors.
This method performs the following steps: 1. Loads the optimizer settings (learning rate and weight decay). 2. Configures the optimizer, learning rate scheduler, and loss function. 3. Builds the regressor models based on the provided number of neighbors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_neighbors | float | The scaling factor based on the typical number of neighbors. | required |
lr | float | The learning rate for the optimizer. Defaults to | DEFAULT_OPTIM_SETTINGS['lr'] |
wd | float | The weight decay for the optimizer. Defaults to | DEFAULT_OPTIM_SETTINGS['wd'] |
optimizer_class | Optimizer | The optimizer class to use. Defaults to | AdamW |
scheduler_class | _LRScheduler | The learning rate scheduler class. Defaults to | ExponentialLR |
scheduler_settings | dict | The settings for the learning rate scheduler. Defaults to | dict(gamma=0.99) |
loss_function | Module | The loss function to use for training. Defaults to | L1Loss |
Raises:
Type | Description |
---|---|
ValueError | If |
Source code in energy_gnome/models/e3nn/regressor.py
create_dataloader(databases, subset=None, shuffle=False, confidence_threshold=0.5)
Format and return a PyTorch DataLoader for training, validation, or testing.
This method prepares the dataloaders for PyTorch training by featurizing the input datasets and handling multiple types of databases. It ensures that shuffling is only applied to the training subset, and it filters out low-confidence samples from GNoMEDatabase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
databases | BaseDatabase | list[BaseDatabase] | A single instance or a list of | required |
subset | str | The specific data subset to use ( | None |
shuffle | bool | Whether to shuffle the data. This is only applicable for the training set. Defaults to | False |
confidence_threshold | float | The threshold for filtering out low-confidence entries in the | 0.5 |
Raises:
Type | Description |
---|---|
ValueError | If shuffling is set to |
Returns:
Type | Description |
---|---|
tuple | A tuple containing: - dataloader_db (DataLoader): The PyTorch DataLoader object containing the processed data. - mean_neighbors (float): The mean number of neighbors (calculated using the featurized data). |
Source code in energy_gnome/models/e3nn/regressor.py
evaluate(dataloader, return_df=False)
Evaluate the performance of the regression model(s) on the provided dataset.
This method runs inference on the given dataset and calculates the loss (L1 loss) for each model in the self.models
list. It returns either a detailed DataFrame with predictions and losses or a dictionary of predictions for each model, depending on the return_df
flag.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader | DataLoader | The DataLoader object containing the dataset to be evaluated. | required |
return_df | bool | Whether to return the results as a DataFrame with predictions and losses ( | False |
Returns:
Type | Description |
---|---|
DataFrame | If |
dict[str, DataFrame] | If |
Source code in energy_gnome/models/e3nn/regressor.py
Python | |
---|---|
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
featurize_db(dataset)
Featurize the given dataset by processing the CIF file paths and extracting structural and chemical information.
This method reads the CIF files specified in the input dataset, extracts chemical information (such as formulae and species), and then generates featurized data suitable for model training. It also preserves the target property if it exists in the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset | DataFrame | Input dataset containing the CIF file paths and optionally the target property. | required |
Returns:
Type | Description |
---|---|
DataFrame | The featurized dataset, including chemical information and features for model training. The dataset includes columns for 'structure', 'species', 'formula', and 'data' (featurized data). |
Source code in energy_gnome/models/e3nn/regressor.py
fit(dataloader_train, dataloader_valid, n_epochs=DEFAULT_TRAINING_SETTINGS['n_epochs'], parallelize=False)
Train the regressor models using the specified training and validation datasets.
This method supports both sequential and parallelized training:
- If
parallelize
isTrue
and multiple GPUs are available, training is executed asynchronously. - If only one GPU is available, a warning is issued, and sequential training is used.
- If no GPUs are available but the model is set to use CUDA, an error is raised.
- Otherwise, models are trained sequentially.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader_train | DataLoader | PyTorch DataLoader containing the training dataset. | required |
dataloader_valid | DataLoader | PyTorch DataLoader containing the validation dataset. | required |
n_epochs | int | Number of training epochs. Defaults to | DEFAULT_TRAINING_SETTINGS['n_epochs'] |
parallelize | bool | Whether to parallelize training across multiple GPUs. Defaults to | False |
Raises:
Type | Description |
---|---|
RuntimeError | If CUDA is selected but no GPU is available. |
Source code in energy_gnome/models/e3nn/regressor.py
get_optimizer_scheduler_loss(optimizer_class=torch.optim.AdamW, scheduler_class=torch.optim.lr_scheduler.ExponentialLR, scheduler_settings=dict(gamma=0.96), loss_function=torch.nn.L1Loss)
Configure the optimizer, learning rate scheduler, and loss function for training.
This method sets up the components required for model training, including the optimizer, learning rate scheduler, and loss function. The optimizer and scheduler are configured based on the provided class arguments, while the loss function is selected based on the callable function provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer_class | Optimizer | The optimizer class to use. Defaults to | AdamW |
scheduler_class | _LRScheduler | The learning rate scheduler class to use. Defaults to | ExponentialLR |
scheduler_settings | dict | A dictionary of settings for the learning rate scheduler. For example, | dict(gamma=0.96) |
loss_function | callable | The loss function to use for training. Defaults to | L1Loss |
Raises:
Type | Description |
---|---|
ValueError | If |
ValueError | If |
ValueError | If |
Source code in energy_gnome/models/e3nn/regressor.py
load_trained_models(state='state_best')
Load trained models from the model directory.
This method searches for trained models by: 1. Loading model settings from .yaml
files matching _model_spec
. 2. Initializing models based on corresponding .json
configuration files. 3. Loading the model weights from .torch
files. 4. Storing the loaded models in self.models
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state | str | The key used to extract model weights from the saved state dictionary (e.g., | 'state_best' |
Returns:
Type | Description |
---|---|
list[str]: A list of |
Source code in energy_gnome/models/e3nn/regressor.py
plot_history()
Plot the training and validation loss history for each trained model.
This method iterates through the models saved in the model directory, loads their training history, and generates a plot comparing training and validation loss over epochs. The plot is saved as both PNG and PDF files in the figures directory.
The plot will include
- X-axis: Epochs (steps)
- Y-axis: Loss values
- Two lines: Training loss and Validation loss
Saves the generated plots as
- model_name_training.png
- model_name_training.pdf
Uses Matplotlib to generate the plots and saves them in the configured figures directory.
Raises:
Type | Description |
---|---|
FileNotFoundError | If no models are found in the specified model directory. |
KeyError | If the model history does not contain expected keys like "history". |
Source code in energy_gnome/models/e3nn/regressor.py
plot_parity(predictions_dict, include_ensemble=True)
Plot a parity plot for model predictions and their comparison with true values.
This method generates a scatter plot where the x-axis represents the true values, and the y-axis represents the predicted values from one or more models. It also includes a reference line (1:1 line) and error histograms as insets to visualize the prediction error distribution. Additionally, it calculates and annotates the R² value for each model's predictions and optionally for the ensemble average of all models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions_dict | dict | A dictionary where keys are model names (e.g., 'model_1', 'model_2') and values are pandas DataFrames containing the | required |
include_ensemble | bool | If | True |
Source code in energy_gnome/models/e3nn/regressor.py
Python | |
---|---|
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 |
|
predict(db, confidence_threshold=0.5, save_final=True)
Predicts the target property for candidate specialized materials using regressor models, after filtering materials based on classifier committee confidence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db | GNoMEDatabase | The database containing the materials and their properties. | required |
confidence_threshold | float | The minimum classifier committee confidence required to keep a material for prediction. Defaults to | 0.5 |
save_final | bool | Whether to save the final database with predictions. Defaults to | True |
Returns:
Type | Description |
---|---|
DataFrame | A DataFrame containing the predictions, along with the true values and classifier committee confidence scores for the screened materials. |
Notes
- The method filters the materials based on the classifier confidence, then uses the regressor models to predict the target property for the remaining materials.
- If
save_final
is set to True, the predictions are saved to the database in thefinal
stage.
Source code in energy_gnome/models/e3nn/regressor.py
set_model_settings(yaml_file=None, **kargs)
Set model settings either from a YAML file or provided keyword arguments.
This method allows setting model settings from multiple sources: 1. If a yaml_file
is provided, it loads the settings from that file. 2. If additional settings are provided as keyword arguments (kargs
), they overwrite the default or loaded settings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_file | (Path, str) | Path to the YAML file containing the model settings. | None |
kargs | dict | Dictionary of model settings to override the default ones. | {} |
Source code in energy_gnome/models/e3nn/regressor.py
set_optimizer_settings(lr, wd)
Set the optimizer settings, including learning rate and weight decay.
This method sets the learning rate and weight decay for the optimizer, which will be used in the training process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lr | float | The learning rate for the optimizer. It should be a positive float. | required |
wd | float | The weight decay (regularization) parameter for the optimizer. It should be a non-negative float. | required |
Source code in energy_gnome/models/e3nn/regressor.py
set_training_settings(n_epochs)
Set the number of epochs for training.
This method sets the number of epochs for the model's training process. It is assumed that the training process will be carried out for the specified number of epochs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_epochs | int | The number of epochs for training. It should be a positive integer. | required |
Source code in energy_gnome/models/e3nn/regressor.py
energy_gnome.models.GBDTClassifier
Bases: BaseClassifier
Source code in energy_gnome/models/gbdt/classifier.py
Python | |
---|---|
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
__init__(model_name, models_dir=MODELS_DIR, figures_dir=FIGURES_DIR)
This class extends BaseClassifier
to implement a gradient boosted decision tree (GBDT) for classification tasks. It sets up the necessary directory structure and configurations for training models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name | str | Name of the model, used to create subdirectories. | required |
models_dir | Path | str | Directory for storing trained model weights. Defaults to MODELS_DIR from config. | MODELS_DIR |
figures_dir | Path | str | Directory for saving figures and visualizations. Defaults to FIGURES_DIR from config. | FIGURES_DIR |
Attributes:
Name | Type | Description |
---|---|---|
_model_spec | str | Specification string used for model identification. |
Source code in energy_gnome/models/gbdt/classifier.py
compile_(n_jobs=1)
Initializes the classifier pipeline and sets up the hyperparameter search.
This method calls the _build_classifier
method to construct a Gradient Boosting Classifier pipeline, including feature scaling, feature selection via Recursive Feature Elimination (RFE), and classification. It then stores the resulting GridSearchCV
object in the self.search
attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_jobs | int | The number of CPU cores to use for parallel processing during the grid search. Default is 1. | 1 |
Returns:
Type | Description |
---|---|
None |
Notes
This method does not return any value but sets the self.search
attribute with the initialized GridSearchCV
object, which contains the classifier pipeline and hyperparameter tuning setup.
Source code in energy_gnome/models/gbdt/classifier.py
evaluate(df, return_df=False)
Evaluate the performance of multiple models on a given dataset.
This method evaluates each model's predictions on the provided dataset (df
) and returns the predictions either as a DataFrame with true values and model predictions or as a dictionary of model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | The dataset containing the features and the target property (last column). The model predictions are based on all columns except the last one (target property). | required |
return_df | bool | If True, returns a DataFrame with true values and predictions from each model. If False, returns a dictionary with model predictions. Defaults to | False |
Returns:
Type | Description |
---|---|
DataFrame | If |
dict[str, DataFrame] | If |
Notes
- The target property in
df
must matchself.target_property
. - Each model's predictions are generated using the
predict_proba
method, which is expected to return probabilities. - The method assumes the target property is in the last column of the input
df
and features are in all other columns.
Source code in energy_gnome/models/gbdt/classifier.py
featurize_db(databases, subset=None, max_dof=None, mute_warnings=True)
Load and featurize the specified databases efficiently.
This method processes the given list of databases and applies a featurization pipeline to each dataset. It handles the loading of raw data from databases, extracts relevant features such as composition and structure, and applies transformations to create numerical features for model training.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
databases | list[BaseDatabase] | A list of databases (or a single database) from which to load and featurize data. | required |
subset | str | The subset of data to load from each database (default is None). | None |
max_dof | int | Maximum degrees of freedom for the feature space. If not provided, it is automatically calculated. | None |
mute_warnings | bool | Whether to suppress warnings during featurization (default is True). | True |
Returns:
Type | Description |
---|---|
DataFrame | A DataFrame containing the featurized data, including numerical features and a column for |
Notes
- If the database contains a
Reduced Formula
orformula_pretty
column, compositions are parsed accordingly. - The resulting DataFrame retains only numerical features, removes NaN rows, and includes an
is_specialized
column. - The method supports batch processing using
tqdm
for progress visualization during featurization.
Source code in energy_gnome/models/gbdt/classifier.py
Python | |
---|---|
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
fit(df)
Train and save multiple models using a GridSearchCV
classifier.
This method iterates through the specified number of committers (n_committers
) and performs the following: 1. Trains a model for each committer using the GridSearchCV
pipeline (self.search
) with the given data (df
). 2. Saves each trained model as a .pkl
file in the specified self.models_dir
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | The input dataset. The last column is assumed to be the target variable, and all other columns are used as features. | required |
Returns:
Type | Description |
---|---|
None |
Notes
- The models are saved as
.pkl
files in theself.models_dir
directory, with filenames following the pattern{self._model_spec}.rep{i}.pkl
, wherei
is the index of the committer. - The
GridSearchCV
pipeline, defined inself.search
, is used for training.
Source code in energy_gnome/models/gbdt/classifier.py
load_trained_models()
Load trained models from the model directory.
This method searches for trained models by: 1. Loading model settings from .yaml
files matching _model_spec
. 2. Loading model weights from .pkl
files. 3. Storing the loaded models in self.models
.
Returns:
Type | Description |
---|---|
list[str] | list[str]: A list of |
Source code in energy_gnome/models/gbdt/classifier.py
plot_performance(predictions_dict, include_ensemble=True)
Plot model performance evaluation curves: ROC, Precision, and Recall.
This method generates a multi-panel plot that visualizes the performance of different models on classification tasks. It includes: - ROC curve with AUC (Area Under the Curve) - Precision-Recall curve - Recall-Threshold curve
The method also supports an optional ensemble model performance evaluation by averaging individual model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions_dict | dict[str, DataFrame] | A dictionary where keys are model names and values are DataFrames containing the | required |
include_ensemble | bool | If | True |
Returns:
Name | Type | Description |
---|---|---|
None | The method generates and saves the performance plots as PNG and PDF files. |
Notes
- The method assumes that the
predictions_dict
contains the model predictions (in theprediction
column) and the true labels (in thetrue_value
column). - The ROC curve is evaluated using the
roc_curve
function, while the Precision and Recall curves are generated usingprecision_recall_curve
. - The final figure is saved in both PNG and PDF formats in the directory defined by
self.figures_dir
.
Source code in energy_gnome/models/gbdt/classifier.py
Python | |
---|---|
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
screen(db, save_processed=True)
Screen the database for specialized materials using classifier predictions.
This method performs the following steps: 1. Featurizes the database using featurize_db
. 2. Evaluates the featurized data using a committee of classifiers to generate predictions. 3. Combines the predictions with the original database and removes rows with missing values or unqualified materials. 4. Optionally saves the processed (screened) database for future use.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db | GNoMEDatabase | A | required |
save_processed | bool | Whether to save the screened data to the database. Defaults to | True |
Returns:
Type | Description |
---|---|
DataFrame | A DataFrame containing the original data combined with classifier predictions, excluding materials that have missing or unqualified values for screening. |
Notes
- The method assumes that
featurize_db
and_evaluate_unknown
methods are defined and function correctly. - The
classifier_mean
column in the returned DataFrame reflects the mean classifier prediction, which is used to screen specialized materials. - The
is_specialized
column is dropped from the screened DataFrame.
Source code in energy_gnome/models/gbdt/classifier.py
set_model_settings(yaml_file=None, **kargs)
Set model settings either from a YAML file or provided keyword arguments.
This method allows setting model settings from multiple sources: 1. If a yaml_file
is provided, it loads the settings from that file. 2. If additional settings are provided as keyword arguments (kargs
), they overwrite the default or loaded settings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yaml_file | (Path, str) | Path to the YAML file containing the model settings. | None |
kargs | dict | Dictionary of model settings to override the default ones. | {} |