Prepare: Feature Cleaning
ChemML provides utilities to remove highly correlated and invariant features.
[1]:
import numpy as np
import pandas as pd
from chemml.preprocessing import RemoveCorrFeatures, RemoveInvFeatures
[2]:
df_corr = pd.DataFrame()
df_corr['feat_a'] = np.arange(0, 10)
df_corr['feat_b'] = np.arange(0, 10)
df_corr['feat_c'] = np.array([0, 1] * 5)
df_corr['feat_d'] = -np.arange(0, 10)
df_corr
[2]:
| feat_a | feat_b | feat_c | feat_d | |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | -1 |
| 2 | 2 | 2 | 0 | -2 |
| 3 | 3 | 3 | 1 | -3 |
| 4 | 4 | 4 | 0 | -4 |
| 5 | 5 | 5 | 1 | -5 |
| 6 | 6 | 6 | 0 | -6 |
| 7 | 7 | 7 | 1 | -7 |
| 8 | 8 | 8 | 0 | -8 |
| 9 | 9 | 9 | 1 | -9 |
Removing Invariant and Low-Variance Features
[5]:
df_inv_clean = RemoveInvFeatures(
df_inv,
sanitize_threshold=0.9,
sanitize_nonbinary=True,
use_variance_filtering=True,
variance_threshold=0.01,
keep_filtered_columns=False
)
df_inv_clean
[5]:
| feat_keep_1 | feat_keep_2 | |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 2 | 0 | 2 |
| 3 | 1 | 3 |
| 4 | 0 | 4 |
| 5 | 1 | 5 |
| 6 | 0 | 6 |
| 7 | 1 | 7 |
| 8 | 0 | 8 |
| 9 | 1 | 9 |
| 10 | 0 | 10 |
| 11 | 1 | 11 |
| 12 | 0 | 12 |
| 13 | 1 | 13 |
| 14 | 0 | 14 |
| 15 | 1 | 15 |
| 16 | 0 | 16 |
| 17 | 1 | 17 |
| 18 | 0 | 18 |
| 19 | 1 | 19 |
Keeping Removed Columns in a Separate DataFrame
[6]:
df_inv_clean, df_removed = RemoveInvFeatures(
df_inv,
sanitize_threshold=0.9,
sanitize_nonbinary=True,
use_variance_filtering=True,
variance_threshold=0.01,
keep_filtered_columns=True
)
df_inv_clean
[6]:
| feat_keep_1 | feat_keep_2 | |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| 2 | 0 | 2 |
| 3 | 1 | 3 |
| 4 | 0 | 4 |
| 5 | 1 | 5 |
| 6 | 0 | 6 |
| 7 | 1 | 7 |
| 8 | 0 | 8 |
| 9 | 1 | 9 |
| 10 | 0 | 10 |
| 11 | 1 | 11 |
| 12 | 0 | 12 |
| 13 | 1 | 13 |
| 14 | 0 | 14 |
| 15 | 1 | 15 |
| 16 | 0 | 16 |
| 17 | 1 | 17 |
| 18 | 0 | 18 |
| 19 | 1 | 19 |
[7]:
df_removed
[7]:
| feat_const | feat_binary_dominant | feat_nonbinary_dominant | feat_low_variance | |
|---|---|---|---|---|
| 0 | 1.0 | 0 | 5 | 0.000000 |
| 1 | 1.0 | 0 | 5 | 0.000021 |
| 2 | 1.0 | 0 | 5 | 0.000042 |
| 3 | 1.0 | 0 | 5 | 0.000063 |
| 4 | 1.0 | 0 | 5 | 0.000084 |
| 5 | 1.0 | 0 | 5 | 0.000105 |
| 6 | 1.0 | 0 | 5 | 0.000126 |
| 7 | 1.0 | 0 | 5 | 0.000147 |
| 8 | 1.0 | 0 | 5 | 0.000168 |
| 9 | 1.0 | 0 | 5 | 0.000189 |
| 10 | 1.0 | 0 | 5 | 0.000211 |
| 11 | 1.0 | 0 | 5 | 0.000232 |
| 12 | 1.0 | 0 | 5 | 0.000253 |
| 13 | 1.0 | 0 | 5 | 0.000274 |
| 14 | 1.0 | 0 | 5 | 0.000295 |
| 15 | 1.0 | 0 | 5 | 0.000316 |
| 16 | 1.0 | 0 | 5 | 0.000337 |
| 17 | 1.0 | 0 | 5 | 0.000358 |
| 18 | 1.0 | 0 | 2 | 0.000379 |
| 19 | 1.0 | 1 | 3 | 0.000400 |