Skip to main content

A smarter refit to sklearn.GridSearchCV and sklearn.RandomizedSearchCV to reduce over and underfitting

Project description

FitSearchCV— A smarter refit selector for scikit-learn searches

selector-mean is a tiny utility that helps reduce overfitting and underfitting when tuning hyperparameters with scikit-learn’s GridSearchCV or RandomizedSearchCV.

It provides a single function:

  • selector_mean(cv_results_, metric=None, use_abs_gap=True, clip01=True)
    A callable you pass to refit=... that picks the parameter set balancing high test performance and small train–test gap.

Train AccuracyTest Accuracy Poor Generalization Overfitting Train AccuracyTest Accuracy Poor Generalization Overfitting (1)

Best Use cases

  • accuracy_score

  • balanced_accuracy_score

  • precision_score (binary, micro, macro, weighted)

  • recall_score (binary, micro, macro, weighted)

  • f1_score (binary, micro, macro, weighted)

  • roc_auc_score

  • average_precision_score

  • jaccard_score


Why?

Vanilla GridSearchCV usually selects the highest mean test score, which can sometimes favor models with high variance.
Here, test refers to as validation accuracy.
selector_mean instead minimizes: ((|train - test|) + (1 - test))/2
This prevents both underfittnig and overfitting.

|train-test| is for reducing the gap between train and test accuracy thus decreasing overfitting.

(1-test) is for reducing the gap between test accuracy and 1 hence increasing the score thus reducing underfitting.


Want to try?

Just type

pip install fitsearchcv

PyPI link: PyPI


How to Use?

from fitsearchcv.selectors import selector_mean
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression

lr=LogisticRegression()

param_grid = [
    {'penalty': ['l1'], 'C': [0.1, 1, 10], 'solver': ['liblinear', 'saga']},
    
    {'penalty': ['l2'], 'C': [0.1, 1, 10], 'solver': ['liblinear', 'lbfgs', 'saga', 'sag', 'newton-cg']},
    
    {'penalty': ['elasticnet'], 'C': [0.1, 1, 10], 'solver': ['saga'], 'l1_ratio': [0.0, 0.25, 0.5, 0.75, 1.0]},
    
    {'penalty': [None], 'solver': ['lbfgs', 'sag', 'newton-cg', 'saga']}  
]

grid1=GridSearchCV(estimator=lr,
                   param_grid=param_grid,
                   refit=selector_mean, # added line
                   cv=5, 
                   return_train_score=True,
                   n_jobs=-1)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fitsearchcv-1.0.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fitsearchcv-1.0.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file fitsearchcv-1.0.2.tar.gz.

File metadata

  • Download URL: fitsearchcv-1.0.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for fitsearchcv-1.0.2.tar.gz
Algorithm Hash digest
SHA256 da2adc1615cf0741ad0ed92490c1dcc3aec4fab44c181b9e13780fd138a90828
MD5 c04f436921b09612263ca7d5d24fde36
BLAKE2b-256 7a41f9f081ae13a6ef0d184aa50e93a567ab7ee5a0b3e630079d5c8417719808

See more details on using hashes here.

File details

Details for the file fitsearchcv-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: fitsearchcv-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for fitsearchcv-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 87ed71fa86b580a28d2352c81a5fb642e4b2909f01aca8a395d8bb7fbf4b0071
MD5 cdf4acf5773cdbb30b1b14c8bc234825
BLAKE2b-256 b7d0e1b3289e9e86b56df68d6ecd63494e23c014fe42354d4a347790ad673a02

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page