# stepwise¶

In [ ]:
```stepwise(estimator,
input_relation: (str, vDataFrame),
X: list,
y: str,
criterion: str = "bic",
direction: str = "backward",
max_steps: int = 100,
criterion_threshold: int = 3,
drop_final_estimator: bool = True,
x_order: str = "pearson",
print_info: bool = True,
show: bool = True,
ax=None,
**style_kwds,)
```

Uses the stepwise algorithm to find the most suitable number of features when fitting the estimator.

### Parameters¶

Name Type Optional Description
estimator
object
Vertica estimator with a fit method. This can be either a regressor or a binary classifier. Multiclass classifiers are not supported.
input_relation
str / vDataFrame
Input Relation.
X
list
List of the predictor columns.
y
str
Response Column.
criterion
str
Criterion used to evaluate the model.
• aic : Akaike’s Information Criterion.
• bic : Bayesian Information Criterion.
direction
str
How to start the stepwise search. Can be done 'backward' or 'forward'.
max_steps
int
The maximum number of steps to be considered.
criterion_threshold
int
Threshold used when comparing the models criterions. If the difference is lesser than the threshold then the current 'best' model is changed.
drop_final_estimator
bool
If set to True, the final estimator will be dropped.
x_order
str
How to preprocess X before using the stepwise algorithm.
• pearson : X is ordered based on the Pearson's correlation coefficient.
• spearman : X is ordered based on the Spearman's correlation coefficient.
• random : Shuffles the vector X before applying the stepwise algorithm.
• none : Does not change X order.
print_info
bool
If set to True, prints the model information at each step.
show
bool
If set to True, the Stepwise graphic will be drawn.
ax
Matplotlib axes object
The axes to plot on.
**style_kwds
any
Any optional parameter to pass to the Matplotlib functions.

### Returns¶

tablesample : An object containing the result. For more information, see utilities.tablesample.

### Example¶

In [4]:
```from verticapy.learn.linear_model import LogisticRegression
model = LogisticRegression(name = "public.LR_titanic",
tol = 1e-4,
max_iter = 100,
solver = 'Newton')

from verticapy.learn.model_selection import stepwise

# backward
stepwise(model,
input_relation = "public.titanic",
X = ["age", "fare", "parch", "pclass",],
y = "survived",)
```
```Starting Stepwise
```
```[Model 0] bic: -1574.6084573061446; Variables: ['"age"', '"parch"', '"fare"', '"pclass"']
[Model 1] bic: -1941.0852092129517; (-) Variable: "age"
[Model 2] bic: -1940.602698869082; (-) Variable: "parch"
[Model 3] bic: -1937.9168392321783; (-) Variable: "fare"

Selected Model

[Model 3] bic: -1937.9168392321783; Variables: ['"pclass"']
```
Out[4]:
 bic change variable importance 0 -1574.6084573061446 [null] [null] 0.0 1 -1941.0852092129517 - "age" 0.0 2 -1940.602698869082 - "parch" 0.3359363423491798 3 -1937.9168392321783 - "fare" 1.8699658441482787 4 -1797.4537281723472 + "pclass" 97.79409781350255
Rows: 1-5 | Columns: 6
In [5]:
```# forward
stepwise(model,
input_relation = "public.titanic",
X = ["age", "fare", "parch", "pclass",],
y = "survived",
direction = "forward",)
```
```Starting Stepwise
```
```[Model 0] bic: -1797.4537281723472; Variables: []
[Model 1] bic: -1937.9168392321783; (+) Variable: "pclass"
[Model 2] bic: -1943.8577653611087; (+) Variable: "parch"

Selected Model

[Model 2] bic: -1943.8577653611087; Variables: ['"pclass"', '"parch"']
```
Out[5]:
 features bic change variable importance 0 [] -1797.4537281723472 [null] [null] 0.0 1 ['pclass'] -1937.9168392321783 + "pclass" 94.21370196806726 2 ['pclass', 'fare'] -1940.602698869082 - "fare" 1.8015034513333006 3 ['pclass', 'parch'] -1943.8577653611087 + "parch" 3.984794580599448 4 ['pclass', 'parch', 'age'] -1580.6419110115492 - "age" 0.0
Rows: 1-5 | Columns: 6