module sklapi.sklearn_base_transform_learner#

Inheritance diagram of mlinsights.sklapi.sklearn_base_transform_learner

Short summary#

module mlinsights.sklapi.sklearn_base_transform_learner

Implements a transform which converts a learner into a transform.

source on GitHub

Classes#

class

truncated documentation

SkBaseTransformLearner

A transform which hides a learner, it converts method predict into transform. This way, two learners can …

Methods#

method

truncated documentation

__init__

__repr__

usual

_set_method

Defines the method to use to convert the features into predictions.

fit

Trains a model.

get_params

Returns the parameters mandatory to clone the class.

set_params

Sets parameters.

transform

Predictions, output of the embedded learner.

Documentation#

@file @brief Implements a transform which converts a learner into a transform.

class mlinsights.sklapi.sklearn_base_transform_learner.SkBaseTransformLearner(model=None, method=None, **kwargs)#

Bases: SkBaseTransform

A transform which hides a learner, it converts method predict into transform. This way, two learners can be inserted into the same pipeline. There is another a,d shorter implementation with class @see class TransferTransformer.

Use two learners into a same pipeline

It is impossible to use two learners into a pipeline unless we use a class such as @see cl SkBaseTransformLearner which disguise a learner into a transform.

<<<

from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.pipeline import make_pipeline
from mlinsights.sklapi import SkBaseTransformLearner

data = load_iris()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y)

try:
    pipe = make_pipeline(LogisticRegression(),
                         DecisionTreeClassifier())
except Exception as e:
    print("ERROR:")
    print(e)
    print('.')

pipe = make_pipeline(SkBaseTransformLearner(LogisticRegression()),
                     DecisionTreeClassifier())
pipe.fit(X_train, y_train)
pred = pipe.predict(X_test)
score = accuracy_score(y_test, pred)
print("pipeline avec deux learners :", score)

>>>

    pipeline avec deux learners : 0.9210526315789473

@param model learner instance @param method method to call to transform the feature (see below) @param kwargs parameters

Options for parameter method:

  • 'predict'

  • 'predict_proba'

  • 'decision_function'

  • a function

If method is None, the function tries first predict_proba then predict until one of them is part of the class.

__init__(model=None, method=None, **kwargs)#

@param model learner instance @param method method to call to transform the feature (see below) @param kwargs parameters

Options for parameter method:

  • 'predict'

  • 'predict_proba'

  • 'decision_function'

  • a function

If method is None, the function tries first predict_proba then predict until one of them is part of the class.

__repr__()#

usual

_set_method(method)#

Defines the method to use to convert the features into predictions.

fit(X, y=None, **kwargs)#

Trains a model.

@param X features @param y targets @param kwargs additional parameters @return self

get_params(deep=True)#

Returns the parameters mandatory to clone the class.

@param deep unused here @return dict

set_params(**values)#

Sets parameters.

@param values parameters

transform(X)#

Predictions, output of the embedded learner.

@param X features @return prédictions