Scikit-learn Integrations
Classes for sklearn integration.
Despite similar names, there is a difference between pdpipe PdPipeline and
sklearn.pipeline.Pipeline. PdPipeline can only chain transformers while
scikit-learn Pipeline objects can further include the final estimator to
provide additional methods such as predict
and predict_proba
.
This means that by itself, pdpipe PdPipeline does not integrate well with some of scikit-learn utility classes such as sklearn.model_selection.GridSearchCV compared to sklearn.pipeline.Pipeline.
This module resolves such integration issues. Refer to the notebooks folder of the pdpipe repository for complete examples.
Classes
PdPipelineAndSklearnEstimator
Bases: BaseEstimator
A PdPipeline object chained before an sklearn estimator object.
This kind of object can also be used with sklearn's GridSearchCV.
See the pipeline_and_model.ipynb notebook in the notebooks folder of the pdpipe repository for a tutorial on how to use this class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline |
PdPipeline
|
The preprocssing pipeline to connect. |
required |
estimator |
sklearn.base.BaseEstimator
|
The model to connect to the pipeline. |
required |
Attributes:
Name | Type | Description |
---|---|---|
pipeline |
PdPipeline
|
The preprocssing pipeline composing this pipeline+model object. |
model |
sklearn.base.BaseEstimator
|
The sklearn model composing this pipeline+model object. |
Examples:
>>> import pandas as pd; import pdpipe as pdp;
>>> from pdpipe.skintegrate import PdPipelineAndSklearnEstimator;
>>> from sklearn.linear_model import LogisticRegression;
>>> DF2 = pd.DataFrame(
... data=[['-1',0], ['-1',0], ['1',1], ['1',1]],
... index=[1, 2, 3, 4],
... columns=['feature1', 'target']
... )
>>> all_x = DF2[['feature1']]
>>> all_y = DF2['target']
>>> mp = PdPipelineAndSklearnEstimator(
... pipeline=pdp.ColumnDtypeEnforcer({'feature1': int}),
... estimator=LogisticRegression()
... )
>>> mp.fit(all_x, all_y)
<PdPipeline -> LogisticRegression>
>>> res = mp.predict(all_x)
Source code in pdpipe/skintegrate.py
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
Attributes
pipeline = pipeline
instance-attribute
estimator = estimator
instance-attribute
classes_
property
Class labels.
Only available when the estimator is a classifier.
Functions
score(X, y=None)
Source code in pdpipe/skintegrate.py
fit(X, y)
A reference implementation of a fitting function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame, shape(n_samples, n_features)
|
The training input samples. |
required |
y |
array-like, shape (n_samples,) or (n_samples, n_outputs)
|
The target values (class labels in classification, real numbers in regression). |
required |
Returns:
Name | Type | Description |
---|---|---|
self |
object
|
Returns self. |
Source code in pdpipe/skintegrate.py
predict(X)
A reference implementation of a predicting function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
The training input samples. |
array-like
|
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray, shape(n_samples)
|
Returns an array of ones.
The predicted labels or values for |
Source code in pdpipe/skintegrate.py
predict_proba(X)
Call predict_proba on the estimator with the best found parameters.
Only available if the underlying estimator supports
predict_proba
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
indexable, length n_samples
|
Must fulfill the input assumptions of the underlying estimator. |
required |
Returns:
Name | Type | Description |
---|---|---|
y_pred |
ndarray of shape (n_samples,) or (n_samples, n_classes)
|
Predicted class probabilities for |
Source code in pdpipe/skintegrate.py
predict_log_proba(X)
Call predict_log_proba on the estimator with best found parameters.
Only available if the underlying estimator supports
predict_log_proba
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
indexable, length n_samples
|
Must fulfill the input assumptions of the underlying estimator. |
required |
Returns:
Name | Type | Description |
---|---|---|
y_pred |
ndarray of shape (n_samples,) or (n_samples, n_classes)
|
Predicted class log-probabilities for |
Source code in pdpipe/skintegrate.py
decision_function(X)
Call decision_function on the estimator with best found parameters.
Only available if the underlying estimator supports
decision_function
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
indexable, length n_samples
|
Must fulfill the input assumptions of the underlying estimator. |
required |
Returns:
Name | Type | Description |
---|---|---|
y_score |
ndarray of shape (n_samples,) or (n_samples, n_classes) or (n_samples, n_classes
|
Result of the decision function for |
Source code in pdpipe/skintegrate.py
Functions
available_if(check)
An attribute that is available only if check returns a truthy value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
check |
callable
|
When passed the object with the decorated method, this should return a truthy value if the attribute is available, and either return False or raise an AttributeError if not available. |
required |
Returns:
Type | Description |
---|---|
callable
|
A lambda based attribute. |
Source code in pdpipe/skintegrate.py
pdpipe_scorer_from_sklearn_scorer(scorer)
Converts an sklearn scorer to one that will work with pdpipe.
The returned scorer function can then be used with sklearn's model-evaluation tools using cross-validation (such as model_selection.cross_val_score and model_selection.GridSearchCV), when searching over the hyperparameter space of a PdPipelineAndSklearnEstimator object.
See the pipeline_and_model_with_test_test.ipynb notebook in the notebooks folder of the pdpipe repository for a complete example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scorer |
callable
|
A function with the signature |
required |
Returns:
Name | Type | Description |
---|---|---|
pdpipe_scorer |
callable
|
A scorer that is aware of the fact that PdPipelineAndSklearnEstimator has an inner pipeline object that should be used to transform input X (which is a dataframe when using pdpipe, and not a numpy.ndarray). |