Pipeline¶
Pipelines, variances to the sklearn.pipeline.Pipeline object.
sklego.pipeline.DebugPipeline
¶
Bases: Pipeline
A pipeline that has a log statement in between each step, useful for debugging purposes.
See sklearn.pipeline.Pipeline
for all other parameters other than log_callback
.
Note
This implementation is a hack on the original sklearn Pipeline. It aims to have the same behaviour as the original sklearn Pipeline, while changing minimal amount of code.
The log statement is added by overwriting the cache method of the memory, such that the function called in the
cache is wrapped with a functions that calls the log callback function (log_callback
).
This hack will break when:
- The sklearn pipeline initialization function is changed.
- The memory is used differently in the fit.
- The
joblib.memory.Memory
changes behaviour of thecache
method. - The
joblib.memory.Memory
starts using a_cache
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_callback
|
Callable | None
|
The callback function that logs information in between each intermediate step.
If set to |
None
|
Examples:
# Set-up for example
import logging
import sys
import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator, TransformerMixin
from sklego.pipeline import DebugPipeline
logging.basicConfig(
format=("[%(funcName)s:%(lineno)d] - %(message)s"),
level=logging.INFO,
stream=sys.stdout,
)
# DebugPipeline set-up
n_samples, n_features = 3, 5
X = np.zeros((n_samples, n_features))
y = np.arange(n_samples)
class Adder(TransformerMixin, BaseEstimator):
def __init__(self, value):
self._value = value
def fit(self, X, y=None):
return self
def transform(self, X):
return X + self._value
def __repr__(self):
return f"Adder(value={self._value})"
steps = [
("add_1", Adder(value=1)),
("add_10", Adder(value=10)),
("add_100", Adder(value=100)),
("add_1000", Adder(value=1000)),
]
# The DebugPipeline behaves the sames as the Sklearn pipeline.
pipe = DebugPipeline(steps)
_ = pipe.fit(X, y=y)
print(pipe.transform(X))
# [[1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]]
# But it has the option to set a `log_callback`, that logs in between each step.
pipe = DebugPipeline(steps, log_callback="default")
_ = pipe.fit(X, y=y)
# [default_log_callback:34] - [Adder(value=1)] shape=(3, 5) time=0s
# [default_log_callback:34] - [Adder(value=10)] shape=(3, 5) time=0s
# [default_log_callback:34] - [Adder(value=100)] shape=(3, 5) time=0s
print(pipe.transform(X))
# [[1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]]
# It is possible to set the `log_callback` later then initialisation.
pipe = DebugPipeline(steps)
pipe.log_callback = "default"
_ = pipe.fit(X, y=y)
# [default_log_callback:34] - [Adder(value=1)] shape=(3, 5) time=0s
# [default_log_callback:34] - [Adder(value=10)] shape=(3, 5) time=0s
# [default_log_callback:34] - [Adder(value=100)] shape=(3, 5) time=0s
print(pipe.transform(X))
# [[1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]]
# It is possible to define your own `log_callback` function.
def log_callback(output, execution_time, **kwargs):
'''My custom `log_callback` function
Parameters
output : tuple[np.ndarray | pd.DataFrame, estimator | transformer]
The output of the step and a step in the pipeline.
execution_time : float
The execution time of the step.
Note
The **kwargs are for arguments that are not used in this callback.
'''
logger = logging.getLogger(__name__)
step_result, step = output
logger.info(
f"[{step}] shape={step_result.shape} "
f"nbytes={step_result.nbytes} time={int(execution_time)}s")
pipe.log_callback = log_callback
_ = pipe.fit(X, y=y)
# [log_callback:20] - [Adder(value=1)] shape=(3, 5) nbytes=120 time=0s
# [log_callback:20] - [Adder(value=10)] shape=(3, 5) nbytes=120 time=0s
# [log_callback:20] - [Adder(value=100)] shape=(3, 5) nbytes=120 time=0s
print(pipe.transform(X))
# [[1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]]
# Remove the `log_callback` when you want to stop logging.
pipe.log_callback = None
_ = pipe.fit(X, y=y)
print(pipe.transform(X))
# [[1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]
# [1111. 1111. 1111. 1111. 1111.]]
# Logging also works with FeatureUnion
from sklearn.pipeline import FeatureUnion
pipe_w_default_log_callback = DebugPipeline(steps, log_callback="default")
pipe_w_custom_log_callback = DebugPipeline(steps, log_callback=log_callback)
pipe_union = DebugPipeline([
("feature_union", FeatureUnion([
("pipe_w_default_log_callback", pipe_w_default_log_callback),
("pipe_w_custom_log_callback", pipe_w_custom_log_callback),
])),
("final_adder", Adder(10000))
], log_callback="default")
_ = pipe_union.fit(X, y=y) # doctest:+ELLIPSIS
# [default_log_callback:34] - [Adder(value=1)] shape=(3, 5) time=0s
# [default_log_callback:34] - [Adder(value=10)] shape=(3, 5) time=0s
# [default_log_callback:34] - [Adder(value=100)] shape=(3, 5) time=0s
# [log_callback:20] - [Adder(value=1)] shape=(3, 5) nbytes=120 time=0s
# [log_callback:20] - [Adder(value=10)] shape=(3, 5) nbytes=120 time=0s
# [log_callback:20] - [Adder(value=100)] shape=(3, 5) nbytes=120 time=0s
# [default_log_callback:34] - [FeatureUnion(...)] shape=(3, 10) time=0s
print(pipe_union.transform(X))
# [[11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111.]
# [11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111.]
# [11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111. 11111.]]
Source code in sklego/pipeline.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
sklego.pipeline.make_debug_pipeline(*steps, **kwargs)
¶
Construct a DebugPipeline
from the given estimators.
This is a shorthand for the DebugPipeline
constructor; it does not require, and does not permit, naming the
estimators. Instead, their names will be set to the lowercase of their types automatically.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*steps
|
list
|
List of estimators to be included in the pipeline. |
()
|
**kwargs
|
dict
|
Additional keyword arguments passed to the
|
{}
|
Returns:
Type | Description |
---|---|
DebugPipeline
|
Instance with given steps, |
Examples:
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing import StandardScaler
make_debug_pipeline(StandardScaler(), GaussianNB(priors=None))
# DebugPipeline(steps=[("standardscaler", StandardScaler()),
# ("gaussiannb", GaussianNB())])
See Also
sklego.pipeline.DebugPipeline : Class for creating a pipeline of transforms with a final estimator.
Source code in sklego/pipeline.py
sklego.pipeline.default_log_callback(output, execution_time, **kwargs)
¶
The default log callback which logs the step name, shape of the output and the execution time of the step.
Info
If you write your custom callback function the input is:
Parameter | Type | Description |
---|---|---|
func |
Callable[..., T] | The function to be wrapped |
input_args |
tuple | The input arguments |
input_kwargs |
dict | The input key-word arguments |
output |
T | The output of the function |
execution_time |
float | The execution time of the step |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
tuple[ndarray | DataFrame, estimator | transformer]
|
The output of the step and a step in the pipeline. |
required |
execution_time
|
float
|
The execution time of the step. |
required |