How to do variance thresholding in Python for feature selection?
0

How to do variance thresholding in Python for feature selection?

This recipe helps you do variance thresholding in Python for feature selection
In [1]:
## How to do variance thresholding in Python for feature selection
def Snippet_130():
    print()
    print(format('How to do variance thresholding in Python for feature selection','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import datasets
    from sklearn.feature_selection import VarianceThreshold

    # Load iris data
    iris = datasets.load_iris()

    # Create features and target
    X = iris.data; print(); print(X[0:7])
    y = iris.target; print(); print(y[0:7])

    # Create VarianceThreshold object with a variance with a threshold of 0.5
    thresholder = VarianceThreshold(threshold=.5)

    # Conduct variance thresholding
    X_high_variance = thresholder.fit_transform(X)

    # View first five rows with features with variances above threshold
    print(); print(X_high_variance[0:7])

Snippet_130()
*********How to do variance thresholding in Python for feature selection**********

[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]]

[0 0 0 0 0 0 0]

[[5.1 1.4 0.2]
 [4.9 1.4 0.2]
 [4.7 1.3 0.2]
 [4.6 1.5 0.2]
 [5.  1.4 0.2]
 [5.4 1.7 0.4]
 [4.6 1.4 0.3]]