This is an implementation of LOFO for automatic feature selection.
Project description
LOFO
Leave One Feature Out (LOFO) is on of the most powerful techniques for feature selection.
This repository contains the implementation of LOFO in Python and can be used with any model of the followings:
- Any Scikit-Learn model.
- Any TensorFlow/Keras model.
- LightGBM.
- CatBoost.
- XGBoost.
Usage
-
Install the package:
-
Import lofo
from feature_selection_lofo import lofo
- For more details on LOFO parameters refer to lofo.py.
- Import the needed libraries for your model, cross-validation, etc
Scikit-Learn Model Example
import warnings
import numpy as np
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegression
- Define the paramters
# shutdown warning messages
warnings.filterwarnings('ignore')
X = train_df.iloc[:, :-1]
Y = train_df.iloc[:, -1]
model = LogisticRegression()
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
metric = roc_auc_score
direction = 'max'
fit_params = "{'X': x_train, 'y': y_train}"
predict_type = 'predict_proba'
return_bad_feats = True
groups = None
is_keras_model = False
- Define the LOFO object and call it
lofo_object = lofo.LOFO(X, Y, model, cv, metric, direction, fit_params,
predict_type, return_bad_feats, groups, is_keras_model)
clean_X, bad_feats = lofo_object()
clean_X: is the dataset containing the useful features only.
bad_feats: are the harmful or useless features.
LightGBM Model Example
import warnings
import numpy as np
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
import lightgbm as lgbm
- Define the paramters
# shutdown warning messages
warnings.filterwarnings('ignore')
X = train_df.iloc[:, :-1]
Y = train_df.iloc[:, -1]
model= lgbm.LGBMClassifier(
objective='binary',
metric='auc',
subsample=0.7,
learning_rate=0.03,
n_estimators=100,
n_jobs=-1)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
metric = roc_auc_score
direction = 'max'
fit_params = "{'X': x_train, 'y': y_train, 'eval_set': [(x_valid,y_valid)], 'verbose': 0}"
predict_type = 'predict_proba'
return_bad_feats = True
groups = None
is_keras_model = False
- Define the LOFO object and call it
lofo_object = lofo.LOFO(X, Y, model, cv, metric, direction, fit_params,
predict_type, return_bad_feats, groups, is_keras_model)
clean_X, bad_feats = lofo_object()
TensorFlow/Keras Model Example
import warnings
import numpy as np
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold
import tensorflow as tf
from tensorflow.keras import layers
- Construct the model
def nn_model():
inputs = layers.Input(shape=X.shape[-1],)
x = layers.Dense(256, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
output = layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.Model(inputs=inputs, outputs=output)
model.compile(loss='binary_crossentropy',
optimizer='adam',)
return model
- Define the paramters
# shutdown warning messages
warnings.filterwarnings('ignore')
X = train_df.iloc[:, :-1]
Y = train_df.iloc[:, -1]
tf.keras.backend.clear_session()
model = nn_model()
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
metric = roc_auc_score
direction = 'max'
fit_params = "{'x': x_train, 'y': y_train, 'validation_data': (x_valid, y_valid), 'epochs': 10, 'batch_size': 256, 'verbose': 0}"
predict_type = 'predict'
return_bad_feats = True
groups = None
is_keras_model = True
- Define the LOFO object and call it
lofo_object = lofo.LOFO(X, Y, model, cv, metric, direction, fit_params,
predict_type, return_bad_feats, groups, is_keras_model)
clean_X, bad_feats = lofo_object()
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
Built Distribution
Close
Hashes for feature_selection_lofo-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86bfed3c3bf1a757b0942bf6263b55e1ffc330b26b7a8515e3cb087d44288bb6 |
|
MD5 | 8b4bc203c9d54df27db05a45df29e489 |
|
BLAKE2b-256 | 81fb338da1d956dc60cdc6f6a17548b2da0ef9e49448b527c543235d7a52c123 |
Close
Hashes for feature_selection_lofo-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edab259f89fc9d7fb411c2595be5e57e57d5f0656badc634b1ee8c2f681ad3fb |
|
MD5 | 79ff8daaaf42652a02e07373da8e4d72 |
|
BLAKE2b-256 | c73663eadc97da412a7ad7d0457ed38169e9c60efe8cec9fba76ce66b3dd4f3c |