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)[source]

Bases: mlinsights.sklapi.sklearn_base_transform.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)

>>>

    ERROR:
    All intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' 'LogisticRegression()' (type <class 'sklearn.linear_model._logistic.LogisticRegression'>) doesn't
    .
    /usr/local/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py:764: ConvergenceWarning: lbfgs failed to converge (status=1):
    STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
    
    Increase the number of iterations (max_iter) or scale the data as shown in:
        https://scikit-learn.org/stable/modules/preprocessing.html
    Please also refer to the documentation for alternative solver options:
        https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
      extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)
    pipeline avec deux learners : 0.9736842105263158

@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)[source]

@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__()[source]

usual

_set_method(method)[source]

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

fit(X, y=None, **kwargs)[source]

Trains a model.

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

get_params(deep=True)[source]

Returns the parameters mandatory to clone the class.

@param deep unused here @return dict

set_params(**values)[source]

Sets parameters.

@param values parameters

transform(X)[source]

Predictions, output of the embedded learner.

@param X features @return prédictions