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("ERREUR:")
    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)

>>>

    ERREUR:
    All intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' 'LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                       intercept_scaling=1, l1_ratio=None, max_iter=100,
                       multi_class='auto', n_jobs=None, penalty='l2',
                       random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
                       warm_start=False)' (type <class 'sklearn.linear_model.logistic.LogisticRegression'>) doesn't
    .
    /usr/local/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:935: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
      "of iterations.", ConvergenceWarning)
    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