Skip to content

Instantly share code, notes, and snippets.

@finlytics-hub
Created July 6, 2020 05:57
Show Gist options
  • Save finlytics-hub/0e978f2846bc32a570360ea3998f8d2c to your computer and use it in GitHub Desktop.
Save finlytics-hub/0e978f2846bc32a570360ea3998f8d2c to your computer and use it in GitHub Desktop.
Practical demonstration of VarianceThreshold() without string columns
# import the required library
from sklearn.feature_selection import VarianceThreshold
# define the transform
# default threshold parameter is 0, can be set to any float value. All features with variance below this threshold will be removed
selector = VarianceThreshold(threshold = 0)
# fit the defined transform on training dataset
selector.fit(X_train)
# transform the training dataset and save as a new DF (don't forget to transform the test dataset as well)
X_train_clean = pd.DataFrame(selector.transform(X_train), columns = X_train.columns[selector.get_support(indices=False)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment