ipss 1.1.13__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ipss-1.1.13 → ipss-1.2.0}/PKG-INFO +2 -1
- {ipss-1.1.13 → ipss-1.2.0}/README.md +1 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss/base_selectors/gb.py +3 -1
- {ipss-1.1.13 → ipss-1.2.0}/ipss/base_selectors/rf.py +9 -5
- {ipss-1.1.13 → ipss-1.2.0}/ipss/main.py +37 -19
- {ipss-1.1.13 → ipss-1.2.0}/ipss/preselection.py +7 -5
- {ipss-1.1.13 → ipss-1.2.0}/ipss/processing.py +6 -2
- {ipss-1.1.13 → ipss-1.2.0}/ipss.egg-info/PKG-INFO +2 -1
- {ipss-1.1.13 → ipss-1.2.0}/pyproject.toml +1 -1
- {ipss-1.1.13 → ipss-1.2.0}/LICENSE +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss/__init__.py +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss/base_selectors/__init__.py +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss/base_selectors/linear.py +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss/base_selectors/ufi.py +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss/helpers.py +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss.egg-info/SOURCES.txt +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss.egg-info/dependency_links.txt +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss.egg-info/requires.txt +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/ipss.egg-info/top_level.txt +0 -0
- {ipss-1.1.13 → ipss-1.2.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ipss
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Python implementation of integrated path stability selection (IPSS)
|
|
5
5
|
Author-email: Omar Melikechi <omar.melikechi@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -148,6 +148,7 @@ The [examples](https://github.com/omelikechi/ipss/tree/main/examples) folder inc
|
|
|
148
148
|
- `standardize_X`: Scale features to have mean 0, standard deviation 1 (bool; default `None`, which standardizes for `'l1'` and `'adaptive_lasso'` only).
|
|
149
149
|
- `center_y`: Center response to have mean 0 (bool; default `None`, which centers for `'l1'` and `'adaptive_lasso'` only). Binary responses are never centered.
|
|
150
150
|
- `n_jobs`: Number of jobs to run in parallel (int; default `1`).
|
|
151
|
+
- `random_state`: Seed for preselection, subsampling, and the `'gb'`, `'rf'`, and `'ufi'` selectors (int or `None`; default `None`, which uses NumPy's global random state). Each selector fit gets its own seed; `selector_args['random_state']` is ignored.
|
|
151
152
|
|
|
152
153
|
### General observations/recommendations:
|
|
153
154
|
- `selector = 'gb'` often best for capturing nonlinear relationships.
|
|
@@ -127,6 +127,7 @@ The [examples](https://github.com/omelikechi/ipss/tree/main/examples) folder inc
|
|
|
127
127
|
- `standardize_X`: Scale features to have mean 0, standard deviation 1 (bool; default `None`, which standardizes for `'l1'` and `'adaptive_lasso'` only).
|
|
128
128
|
- `center_y`: Center response to have mean 0 (bool; default `None`, which centers for `'l1'` and `'adaptive_lasso'` only). Binary responses are never centered.
|
|
129
129
|
- `n_jobs`: Number of jobs to run in parallel (int; default `1`).
|
|
130
|
+
- `random_state`: Seed for preselection, subsampling, and the `'gb'`, `'rf'`, and `'ufi'` selectors (int or `None`; default `None`, which uses NumPy's global random state). Each selector fit gets its own seed; `selector_args['random_state']` is ignored.
|
|
130
131
|
|
|
131
132
|
### General observations/recommendations:
|
|
132
133
|
- `selector = 'gb'` often best for capturing nonlinear relationships.
|
|
@@ -5,6 +5,7 @@ import xgboost as xgb
|
|
|
5
5
|
|
|
6
6
|
# gradient boosting classifier
|
|
7
7
|
def fit_gb_classifier(X, y, **kwargs):
|
|
8
|
+
kwargs.setdefault('random_state', np.random.randint(np.iinfo(np.int32).max))
|
|
8
9
|
model = xgb.XGBClassifier(**kwargs)
|
|
9
10
|
model.fit(X,y)
|
|
10
11
|
feature_importances = model.feature_importances_
|
|
@@ -12,7 +13,8 @@ def fit_gb_classifier(X, y, **kwargs):
|
|
|
12
13
|
|
|
13
14
|
# gradient boosting regressor
|
|
14
15
|
def fit_gb_regressor(X, y, **kwargs):
|
|
16
|
+
kwargs.setdefault('random_state', np.random.randint(np.iinfo(np.int32).max))
|
|
15
17
|
model = xgb.XGBRegressor(**kwargs)
|
|
16
18
|
model.fit(X,y)
|
|
17
19
|
feature_importances = model.feature_importances_
|
|
18
|
-
return feature_importances
|
|
20
|
+
return feature_importances
|
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
|
4
4
|
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
|
5
5
|
from sklearn.inspection import permutation_importance
|
|
6
6
|
from sklearn.model_selection import train_test_split
|
|
7
|
+
from sklearn.utils import check_random_state
|
|
7
8
|
|
|
8
9
|
# random forest classifier
|
|
9
10
|
def fit_rf_classifier(X, y, **kwargs):
|
|
@@ -13,8 +14,9 @@ def fit_rf_classifier(X, y, **kwargs):
|
|
|
13
14
|
if importance_type == 'mdi':
|
|
14
15
|
feature_importances = model.feature_importances_
|
|
15
16
|
elif importance_type == 'permutation':
|
|
16
|
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
|
|
17
|
-
|
|
17
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=kwargs.get('random_state'))
|
|
18
|
+
model.fit(X_train, y_train)
|
|
19
|
+
perm_importance = permutation_importance(model, X_test, y_test, n_repeats=1, random_state=kwargs.get('random_state'))
|
|
18
20
|
feature_importances = perm_importance.importances_mean
|
|
19
21
|
else:
|
|
20
22
|
raise ValueError("importance_type must be either 'mdi' or 'permutation'")
|
|
@@ -28,14 +30,16 @@ def fit_rf_regressor(X, y, **kwargs):
|
|
|
28
30
|
if importance_type == 'mdi':
|
|
29
31
|
feature_importances = model.feature_importances_
|
|
30
32
|
elif importance_type == 'permutation':
|
|
31
|
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
|
32
|
-
|
|
33
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=kwargs.get('random_state'))
|
|
34
|
+
model.fit(X_train, y_train)
|
|
35
|
+
perm_importance = permutation_importance(model, X_test, y_test, n_repeats=1, random_state=kwargs.get('random_state'))
|
|
33
36
|
feature_importances = perm_importance.importances_mean
|
|
34
37
|
elif importance_type == 'shadow':
|
|
35
38
|
n, p = X.shape
|
|
36
39
|
X_shadow = X.copy()
|
|
40
|
+
rng = check_random_state(kwargs.get('random_state'))
|
|
37
41
|
for i in range(p):
|
|
38
|
-
|
|
42
|
+
rng.shuffle(X_shadow[:,i])
|
|
39
43
|
X_combined = np.hstack((X, X_shadow))
|
|
40
44
|
model.fit(X_combined, y)
|
|
41
45
|
importances_combined = model.feature_importances_
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
# Integrated path stability selection
|
|
2
2
|
|
|
3
3
|
import time
|
|
4
|
+
import warnings
|
|
4
5
|
|
|
5
6
|
from joblib import Parallel, delayed
|
|
6
7
|
import numpy as np
|
|
8
|
+
from sklearn.utils import check_random_state
|
|
7
9
|
|
|
10
|
+
from .base_selectors import (fit_gb_classifier, fit_gb_regressor, fit_rf_classifier, fit_rf_regressor,
|
|
11
|
+
fit_ufi_classifier, fit_ufi_regressor)
|
|
8
12
|
from .helpers import return_null_result
|
|
9
13
|
from .processing import postprocess_ipss, preprocess_ipss
|
|
10
14
|
|
|
15
|
+
# built-in selectors that take a random_state; each of their fits gets its own seed
|
|
16
|
+
seeded_selectors = (fit_gb_classifier, fit_gb_regressor, fit_rf_classifier, fit_rf_regressor,
|
|
17
|
+
fit_ufi_classifier, fit_ufi_regressor)
|
|
18
|
+
|
|
11
19
|
#----------------------------------------------------------------
|
|
12
20
|
# IPSS
|
|
13
21
|
#----------------------------------------------------------------
|
|
@@ -20,7 +28,7 @@ Inputs:
|
|
|
20
28
|
|
|
21
29
|
Optional
|
|
22
30
|
----------------
|
|
23
|
-
selector: gradient boosting ('gb'), l1 regularization ('l1'), random forest ('rf'
|
|
31
|
+
selector: gradient boosting ('gb'), l1 regularization ('l1'), random forest ('rf'), or a custom selector
|
|
24
32
|
selector_args: arguments for selector
|
|
25
33
|
target_fp: target number of false positives
|
|
26
34
|
target_fdr: target false discovery rate
|
|
@@ -39,6 +47,8 @@ Inputs:
|
|
|
39
47
|
center_y: whether to center the response to have mean 0 (None: only for 'l1' and 'adaptive_lasso'; binary responses are never centered)
|
|
40
48
|
n_jobs: number of jobs to run in parallel
|
|
41
49
|
force_regression: force ipss to use regression (if False, classification is used if response is binary)
|
|
50
|
+
random_state: seed (int, RandomState, or None) for preselection, subsampling, and the 'gb', 'rf', and 'ufi'
|
|
51
|
+
selectors, each of whose fits gets its own seed. None uses numpy's global random state
|
|
42
52
|
|
|
43
53
|
Outputs:
|
|
44
54
|
efp_scores: efp (expected false positive) score for each feature
|
|
@@ -50,14 +60,16 @@ Outputs:
|
|
|
50
60
|
def ipss(X, y, selector='gb', selector_args=None, preselect=True, preselector=None, preselector_args=None,
|
|
51
61
|
B=None, n_alphas=None, ipss_function=None, cutoff=0.05, delta=None, subsample_size=None,
|
|
52
62
|
target_fp=None, target_fdr=None, standardize_X=None, center_y=None, n_jobs=1,
|
|
53
|
-
force_regression=False, _return_details=False):
|
|
63
|
+
force_regression=False, random_state=None, _return_details=False):
|
|
54
64
|
|
|
55
65
|
# start timer
|
|
56
66
|
start = time.time()
|
|
57
67
|
|
|
68
|
+
rng = check_random_state(random_state)
|
|
69
|
+
|
|
58
70
|
# prepare ipss arguments and data prior to estimating selection probabilities
|
|
59
71
|
prep = preprocess_ipss(X, y, selector, selector_args, preselect, preselector, preselector_args,
|
|
60
|
-
B, n_alphas, ipss_function, delta, standardize_X, center_y, force_regression)
|
|
72
|
+
B, n_alphas, ipss_function, delta, standardize_X, center_y, force_regression, rng)
|
|
61
73
|
|
|
62
74
|
if prep is None:
|
|
63
75
|
runtime = time.time() - start
|
|
@@ -69,15 +81,22 @@ def ipss(X, y, selector='gb', selector_args=None, preselect=True, preselector=No
|
|
|
69
81
|
p_full, preselect_indices = prep['p_full'], prep['preselect_indices']
|
|
70
82
|
ipss_function = prep['ipss_function']
|
|
71
83
|
|
|
84
|
+
if selector_function in seeded_selectors and 'random_state' in selector_args:
|
|
85
|
+
warnings.warn("selector_args['random_state'] is ignored; use random_state instead.", UserWarning)
|
|
86
|
+
selector_args = {key: value for key, value in selector_args.items() if key != 'random_state'}
|
|
87
|
+
|
|
72
88
|
n, p = X.shape
|
|
73
89
|
|
|
74
90
|
# subsample size
|
|
75
91
|
if subsample_size is None or subsample_size > n // 2:
|
|
76
92
|
subsample_size = n // 2
|
|
77
93
|
|
|
94
|
+
# one seed per subsampling step
|
|
95
|
+
seeds = rng.randint(np.iinfo(np.int32).max, size=B)
|
|
96
|
+
|
|
78
97
|
# estimate selection probabilities
|
|
79
98
|
results = np.array(Parallel(n_jobs=n_jobs)(delayed(selection)(X, y, alphas, subsample_size,
|
|
80
|
-
selector_function, **selector_args) for
|
|
99
|
+
selector_function, seeds[b], **selector_args) for b in range(B)))
|
|
81
100
|
|
|
82
101
|
# process the estimated selection probabilities and compute final results (efp scores, q-values, etc.)
|
|
83
102
|
ipss_output = postprocess_ipss(results, alphas, n_alphas, p, p_full, preselect_indices, B, cutoff, delta,
|
|
@@ -89,25 +108,24 @@ def ipss(X, y, selector='gb', selector_args=None, preselect=True, preselector=No
|
|
|
89
108
|
|
|
90
109
|
return ipss_output
|
|
91
110
|
|
|
111
|
+
|
|
92
112
|
# subsampler for estimating selection probabilities
|
|
93
|
-
def selection(X, y, alphas, subsample_size, selector_function, **kwargs):
|
|
113
|
+
def selection(X, y, alphas, subsample_size, selector_function, seed=None, **kwargs):
|
|
94
114
|
n, p = X.shape
|
|
95
|
-
|
|
96
|
-
|
|
115
|
+
rng = check_random_state(seed)
|
|
116
|
+
indices = rng.permutation(n)
|
|
97
117
|
|
|
98
118
|
# take two disjoint subsamples
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
indicators =
|
|
109
|
-
indicators[:, 0, :] = np.array(selector_function(X[idx1, :], y[idx1], alphas, **kwargs))
|
|
110
|
-
indicators[:, 1, :] = np.array(selector_function(X[idx2, :], y[idx2], alphas, **kwargs))
|
|
119
|
+
halves = [indices[:subsample_size], indices[subsample_size:2*subsample_size]]
|
|
120
|
+
alpha_args = () if alphas is None else (alphas,)
|
|
121
|
+
|
|
122
|
+
# built-in tree selectors get a new seed for each fit unless one is given
|
|
123
|
+
seed_fits = selector_function in seeded_selectors and 'random_state' not in kwargs
|
|
124
|
+
|
|
125
|
+
indicators = np.empty((2, p)) if alphas is None else np.empty((len(alphas), 2, p))
|
|
126
|
+
for i, idx in enumerate(halves):
|
|
127
|
+
fit_args = {**kwargs, 'random_state': rng.randint(np.iinfo(np.int32).max)} if seed_fits else kwargs
|
|
128
|
+
indicators[..., i, :] = selector_function(X[idx, :], y[idx], *alpha_args, **fit_args)
|
|
111
129
|
|
|
112
130
|
return indicators
|
|
113
131
|
|
|
@@ -5,6 +5,7 @@ import warnings
|
|
|
5
5
|
import numpy as np
|
|
6
6
|
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
|
7
7
|
from sklearn.linear_model import Lasso, LogisticRegression
|
|
8
|
+
from sklearn.utils import check_random_state
|
|
8
9
|
import xgboost as xgb
|
|
9
10
|
|
|
10
11
|
# squared distance correlation between response and each feature
|
|
@@ -27,8 +28,9 @@ def compute_dcor_scores(X, y):
|
|
|
27
28
|
scores[j] = dcov_sqr / np.sqrt(denom) if denom > 0 else 0.0
|
|
28
29
|
return scores
|
|
29
30
|
|
|
30
|
-
def preselection(X, y, preselector, preselector_args=None):
|
|
31
|
+
def preselection(X, y, preselector, preselector_args=None, random_state=None):
|
|
31
32
|
n, p = X.shape
|
|
33
|
+
rng = check_random_state(random_state)
|
|
32
34
|
|
|
33
35
|
if preselector_args is None:
|
|
34
36
|
preselector_args = {}
|
|
@@ -63,7 +65,7 @@ def preselection(X, y, preselector, preselector_args=None):
|
|
|
63
65
|
with warnings.catch_warnings():
|
|
64
66
|
warnings.simplefilter('ignore')
|
|
65
67
|
for _ in range(n_runs):
|
|
66
|
-
indices =
|
|
68
|
+
indices = rng.choice(n, size=n, replace=True)
|
|
67
69
|
X_sampled, y_sampled = X[indices], y[indices]
|
|
68
70
|
model.fit(X_sampled, y_sampled)
|
|
69
71
|
feature_importances += np.abs(model.coef_).ravel()
|
|
@@ -77,7 +79,7 @@ def preselection(X, y, preselector, preselector_args=None):
|
|
|
77
79
|
model = model_class(**preselector_args_local)
|
|
78
80
|
feature_importances = np.zeros(p)
|
|
79
81
|
for _ in range(n_runs):
|
|
80
|
-
model.set_params(random_state=
|
|
82
|
+
model.set_params(random_state=rng.randint(1e5))
|
|
81
83
|
model.fit(X,y)
|
|
82
84
|
feature_importances += model.feature_importances_
|
|
83
85
|
preselect_indices = np.argsort(feature_importances)[::-1][:n_keep]
|
|
@@ -91,7 +93,7 @@ def preselection(X, y, preselector, preselector_args=None):
|
|
|
91
93
|
model = model_class(**preselector_args_local)
|
|
92
94
|
feature_importances = np.zeros(p)
|
|
93
95
|
for i in range(n_runs):
|
|
94
|
-
model.set_params(random_state=
|
|
96
|
+
model.set_params(random_state=rng.randint(1e5))
|
|
95
97
|
model.fit(X, y)
|
|
96
98
|
feature_importances += model.feature_importances_
|
|
97
99
|
nonzero_mask = feature_importances > 0
|
|
@@ -104,7 +106,7 @@ def preselection(X, y, preselector, preselector_args=None):
|
|
|
104
106
|
else:
|
|
105
107
|
n_extra = n_keep - len(nonzero_indices)
|
|
106
108
|
zero_indices = np.where(~nonzero_mask)[0]
|
|
107
|
-
extra_indices =
|
|
109
|
+
extra_indices = rng.choice(zero_indices, size=n_extra, replace=False)
|
|
108
110
|
preselect_indices = np.concatenate([nonzero_indices, extra_indices])
|
|
109
111
|
|
|
110
112
|
elif preselector == 'dcor':
|
|
@@ -13,7 +13,7 @@ from .preselection import preselection
|
|
|
13
13
|
|
|
14
14
|
# prepare ipss arguments and data
|
|
15
15
|
def preprocess_ipss(X, y, selector, selector_args, preselect, preselector, preselector_args,
|
|
16
|
-
B, n_alphas, ipss_function, delta, standardize_X, center_y, force_regression=False):
|
|
16
|
+
B, n_alphas, ipss_function, delta, standardize_X, center_y, force_regression=False, random_state=None):
|
|
17
17
|
|
|
18
18
|
# cast to float64 (float32 alpha grids underflow/overflow in integrate)
|
|
19
19
|
X = X.astype(np.float64) if issparse(X) else np.asarray(X, dtype=np.float64)
|
|
@@ -45,6 +45,10 @@ def preprocess_ipss(X, y, selector, selector_args, preselect, preselector, prese
|
|
|
45
45
|
else:
|
|
46
46
|
binary_response, selector = check_response_type(y, selector)
|
|
47
47
|
|
|
48
|
+
# code binary responses as 0/1 (required by XGBoost)
|
|
49
|
+
if binary_response:
|
|
50
|
+
y = (y == np.unique(y)[1]).astype(np.float64)
|
|
51
|
+
|
|
48
52
|
# ipss function
|
|
49
53
|
if ipss_function is None:
|
|
50
54
|
ipss_function = 'h2' if selector in ['lasso', 'logistic_regression'] else 'h3'
|
|
@@ -67,7 +71,7 @@ def preprocess_ipss(X, y, selector, selector_args, preselect, preselector, prese
|
|
|
67
71
|
p_full = X.shape[1]
|
|
68
72
|
if preselect:
|
|
69
73
|
preselector = resolve_preselector(preselector, selector, binary_response)
|
|
70
|
-
X, preselect_indices = preselection(X, y, preselector, preselector_args)
|
|
74
|
+
X, preselect_indices = preselection(X, y, preselector, preselector_args, random_state)
|
|
71
75
|
if preselect_indices.size == 0:
|
|
72
76
|
warnings.warn('Preselection step removed all features. Returning null result.', UserWarning)
|
|
73
77
|
return None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ipss
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Python implementation of integrated path stability selection (IPSS)
|
|
5
5
|
Author-email: Omar Melikechi <omar.melikechi@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -148,6 +148,7 @@ The [examples](https://github.com/omelikechi/ipss/tree/main/examples) folder inc
|
|
|
148
148
|
- `standardize_X`: Scale features to have mean 0, standard deviation 1 (bool; default `None`, which standardizes for `'l1'` and `'adaptive_lasso'` only).
|
|
149
149
|
- `center_y`: Center response to have mean 0 (bool; default `None`, which centers for `'l1'` and `'adaptive_lasso'` only). Binary responses are never centered.
|
|
150
150
|
- `n_jobs`: Number of jobs to run in parallel (int; default `1`).
|
|
151
|
+
- `random_state`: Seed for preselection, subsampling, and the `'gb'`, `'rf'`, and `'ufi'` selectors (int or `None`; default `None`, which uses NumPy's global random state). Each selector fit gets its own seed; `selector_args['random_state']` is ignored.
|
|
151
152
|
|
|
152
153
|
### General observations/recommendations:
|
|
153
154
|
- `selector = 'gb'` often best for capturing nonlinear relationships.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|