Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tpot/objectives/number_of_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@

def number_of_nodes_objective(est):
"""
Calculates the number of leaves (input nodes) in an sklearn pipeline
Counts estimator nodes in a pipeline, excluding passthrough and dropped steps.

Parameters
----------
est: GraphPipeline | Pipeline | FeatureUnion | BaseEstimator
The pipeline to compute the number of nodes from.
"""

if est is None or (isinstance(est, str) and est in ("passthrough", "drop")):
return 0

if isinstance(est, GraphPipeline):
return sum(number_of_nodes_objective(est.graph.nodes[node]["instance"]) for node in est.graph.nodes)
if isinstance(est, Pipeline):
Expand Down
18 changes: 17 additions & 1 deletion tpot/objectives/tests/test_number_of_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.pipeline import FeatureUnion, Pipeline
import networkx as nx
import tpot
from tpot import GraphPipeline
Expand Down Expand Up @@ -113,3 +113,19 @@ def test_number_of_nodes_objective_pipeline_in_pipeline():
pipe2 = Pipeline([("pipe", pipe), ("svc", SVC())])

assert tpot.objectives.number_of_nodes.number_of_nodes_objective(pipe2) == 3


def test_number_of_nodes_objective_ignores_inactive_steps():
features = FeatureUnion([
("scale", StandardScaler()),
("skip", "passthrough"),
("discard", "drop"),
])
pipeline = Pipeline([
("skip_none", None),
("skip_string", "passthrough"),
("features", features),
("classifier", SVC()),
])

assert tpot.objectives.number_of_nodes_objective(pipeline) == 2