Documentation for FRegressionFilterPValue
Feature Selection for Regression - p-value based.
Fit f_regression and select all columns when p_value of column < p_threshold.
Source code in photonai/modelwrapper/feature_selection.py
class FRegressionFilterPValue(BaseEstimator, TransformerMixin):
"""Feature Selection for Regression - p-value based.
Fit f_regression and select all columns
when p_value of column < p_threshold.
"""
_estimator_type = "transformer"
def __init__(self, p_threshold: float = .05):
"""
Initialize the object.
Parameters:
p_threshold:
Upper bound for p_values.
"""
self.p_threshold = p_threshold
self.selected_indices = []
self.n_original_features = None
def fit(self, X: np.ndarray, y: np.ndarray):
"""Calculation of the important columns.
Apply f_regression on input X, y to generate p_values.
selected_indices = all p_value(columns) < p_threshold.
Parameters:
X:
The input samples of shape [n_samples, n_original_features]
y:
The input targets of shape [n_samples, 1]
"""
self.n_original_features = X.shape[1]
_, p_values = f_regression(X, y)
self.selected_indices = np.where(p_values < self.p_threshold)[0]
return self
def transform(self, X: np.ndarray) -> np.ndarray:
"""Reduced input X to selected_columns.
Parameters:
X
The input samples of shape [n_samples, n_original_features]
Returns:
Column-filtered array of shape [n_samples, n_selected_features].
"""
return X[:, self.selected_indices]
def inverse_transform(self, X: np.ndarray) -> np.ndarray:
"""Reverse to original dimension.
Parameters:
X:
The input samples of shape [n_samples, n_selected_features].
Raises:
ValueError: If input X has a different shape than during fitting.
Returns:
Array of shape [n_samples, n_original_features]
with columns of zeros inserted where features would have
been removed.
"""
if X.shape[1] != len(self.selected_indices):
msg = "X has a different shape than during fitting."
logger.error(msg)
raise ValueError(msg)
Xt = np.zeros((X.shape[0], self.n_original_features))
Xt[:, self.selected_indices] = X
return Xt
__init__(self, p_threshold=0.05)
special
Initialize the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p_threshold |
float |
Upper bound for p_values. |
0.05 |
Source code in photonai/modelwrapper/feature_selection.py
def __init__(self, p_threshold: float = .05):
"""
Initialize the object.
Parameters:
p_threshold:
Upper bound for p_values.
"""
self.p_threshold = p_threshold
self.selected_indices = []
self.n_original_features = None
fit(self, X, y)
Calculation of the important columns.
Apply f_regression on input X, y to generate p_values. selected_indices = all p_value(columns) < p_threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray |
The input samples of shape [n_samples, n_original_features] |
required |
y |
ndarray |
The input targets of shape [n_samples, 1] |
required |
Source code in photonai/modelwrapper/feature_selection.py
def fit(self, X: np.ndarray, y: np.ndarray):
"""Calculation of the important columns.
Apply f_regression on input X, y to generate p_values.
selected_indices = all p_value(columns) < p_threshold.
Parameters:
X:
The input samples of shape [n_samples, n_original_features]
y:
The input targets of shape [n_samples, 1]
"""
self.n_original_features = X.shape[1]
_, p_values = f_regression(X, y)
self.selected_indices = np.where(p_values < self.p_threshold)[0]
return self
inverse_transform(self, X)
Reverse to original dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray |
The input samples of shape [n_samples, n_selected_features]. |
required |
Exceptions:
Type | Description |
---|---|
ValueError |
If input X has a different shape than during fitting. |
Returns:
Type | Description |
---|---|
ndarray |
Array of shape [n_samples, n_original_features] with columns of zeros inserted where features would have been removed. |
Source code in photonai/modelwrapper/feature_selection.py
def inverse_transform(self, X: np.ndarray) -> np.ndarray:
"""Reverse to original dimension.
Parameters:
X:
The input samples of shape [n_samples, n_selected_features].
Raises:
ValueError: If input X has a different shape than during fitting.
Returns:
Array of shape [n_samples, n_original_features]
with columns of zeros inserted where features would have
been removed.
"""
if X.shape[1] != len(self.selected_indices):
msg = "X has a different shape than during fitting."
logger.error(msg)
raise ValueError(msg)
Xt = np.zeros((X.shape[0], self.n_original_features))
Xt[:, self.selected_indices] = X
return Xt
transform(self, X)
Reduced input X to selected_columns.
Returns:
Type | Description |
---|---|
ndarray |
Column-filtered array of shape [n_samples, n_selected_features]. |
Source code in photonai/modelwrapper/feature_selection.py
def transform(self, X: np.ndarray) -> np.ndarray:
"""Reduced input X to selected_columns.
Parameters:
X
The input samples of shape [n_samples, n_original_features]
Returns:
Column-filtered array of shape [n_samples, n_selected_features].
"""
return X[:, self.selected_indices]