Skip to content

Commit e013647

Browse files
committed
added intercept and verbosity
1 parent 94aa2fd commit e013647

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

cobra/cobra.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class COBRA(object):
3535
3636
***ATTRIBUTES***
3737
:_partition_dict: Dict with partitioned DFs X/Y train/selection/validation
38+
:_headers_dict: Dict of 4 lists with header names (object, numeric, bool, other)
39+
:_partitioning_settings: Dict with train/sel/valid sets with their size
3840
----------------------------------------------------
3941
__init__: contains variables which are established with the object.
4042
If some of them is changed, then the whole process must be redone(call the class again),
@@ -90,6 +92,9 @@ def transform(self):
9092

9193
df_trans = dtrans.transform()
9294

95+
self._headers_dict = dpc._headers_dict
96+
self._partitioning_settings = dpc._partitioning_settings
97+
9398
return df_trans
9499

95100

@@ -108,7 +113,7 @@ def fit_univariate(self, df_t, preselect_auc=0.53, preselect_overtrain=5):
108113

109114
return df_sel, df_corr
110115

111-
def fit_model(self, df_t, df_us, modeling_nsteps=30, forced_vars=None, excluded_vars=None, name=None, verbose=False):
116+
def fit_model(self, df_t, df_us, modeling_nsteps=30, forced_vars=None, excluded_vars=None, name=None, verbose=False, positive_only=True):
112117
'''
113118
Method fits and finds best model
114119
Returns dataframe with all the info - forward selection, AUC, importance...
@@ -120,9 +125,10 @@ def fit_model(self, df_t, df_us, modeling_nsteps=30, forced_vars=None, excluded_
120125
excluded_vars: list with variables to be excluded in the model
121126
name: name of the model
122127
verbose: whether immediate steps of the procedure should be printed to the console
128+
positive_only: whether only positive coeficients should be considered (recommended to stay so)
123129
----------------------------------------------------
124130
'''
125-
modsel = ms.ModelSelection(verbose=verbose)
131+
modsel = ms.ModelSelection(verbose=verbose, positive_only=positive_only)
126132

127133
df_models = modsel.fit(df_t,
128134
df_us,

cobra/model_selection.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class ModelSelection(object):
2626
***ATTRIBUTES***
2727
:_partition_dict: Dict with partitioned DFs X/Y train/selection/validation
2828
:_optimal_nvars: Optimal number of variables
29+
:positive_only: Whether all coeficients should be positivr
2930
----------------------------------------------------
3031
'''
3132

32-
def __init__(self, verbose):
33+
def __init__(self, verbose, positive_only):
3334
self.verbose = verbose
35+
self.positive_only = positive_only
3436

3537

3638
def fit(self, df_trans, df_unisel, modeling_nsteps, forced_vars, excluded_vars, name):
@@ -93,7 +95,7 @@ def _getTrainSelectValidXY(self, df):
9395

9496
return dict_out
9597

96-
def _forwardSelection(self, df_sel, forced_vars, excluded_vars, positive_only=True):
98+
def _forwardSelection(self, df_sel, forced_vars, excluded_vars):
9799
'''
98100
Method performs forward selection
99101
Returns DF with performance
@@ -121,6 +123,7 @@ def _forwardSelection(self, df_sel, forced_vars, excluded_vars, positive_only=Tr
121123
df_forward_selection = pd.DataFrame(None,columns=[
122124
'step',
123125
'coef',
126+
'intercept',
124127
'all_coefs_positive',
125128
'auc_train',
126129
'auc_selection',
@@ -178,6 +181,7 @@ def _forwardSelection(self, df_sel, forced_vars, excluded_vars, positive_only=Tr
178181
df_forward_selection.loc[row] = [
179182
step, #Step
180183
logit.coef_, #coef
184+
logit.intercept_, #intercept
181185
all_coefs_positive, #all_coefs_positive
182186
AUC_train, #auc_train
183187
AUC_selection, #auc_selection
@@ -194,12 +198,14 @@ def _forwardSelection(self, df_sel, forced_vars, excluded_vars, positive_only=Tr
194198
row +=1
195199

196200
#Only positive coefs
197-
if positive_only:
201+
if self.positive_only:
198202
all_coefs_negative = len(df_forward_selection[(df_forward_selection['all_coefs_positive'] == True) & (df_forward_selection['step'] == step)]) == 0
199203

200204
if all_coefs_negative:
201205
if self.verbose:
202-
print('No models with only positive coefficients, step skipped.')
206+
207+
print('No models with only positive coefficients, following step skipped.')
208+
print(df_forward_selection[(df_forward_selection['all_coefs_positive'] == True) & (df_forward_selection['step'] == step)])
203209
#Skip the next steps and go the next iteration
204210
#The fitted models are not of interest if the user explicitly
205211
# says positive_only=True

0 commit comments

Comments
 (0)