Передача параметров на заказной трансформатор - PullRequest
0 голосов
/ 13 февраля 2019

Я настроил трансформатор, как показано ниже:

import numpy as np
import pandas as pd

from sklearn.preprocessing import MinMaxScaler, QuantileTransformer
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.base import TransformerMixin 

class CorrWeight(TransformerMixin):
    """Calculate the weights of metrics based on their correlations, and transform to scores based on the resulting weights."""

    def fit(self, X, y, **fit_params):
        self.offset = offset
        self.max_weight = max_weight
        corr_mat = pd.DataFrame(X).corr()
        # Sum over all correlations to get an overall correlation score for each metric
        metric_summary = corr_mat.sum().to_frame().rename(columns={0: 'overall correlation'})
        # Assign a weight to each metric that is inversely related to the overall correlation
        # so that the higher the overall correlation, the lower the weight
        if self.max_weight > 1:
            metric_summary['weight'] = 1 / MinMaxScaler((1 / self.max_weight, 1)).fit_transform(
                metric_summary[['overall correlation']])
        elif self.max_weight == 1:
            metric_summary['weight'] = 1
        else:
            print("Max_weight has to be no smaller than 1!")
            metric_summary['weight'] = 1
        self.metric_summary = metric_summary
        return self

    def transform(self, X, **fit_params):
        # Multiply the rescaled metrics together (with the weight being the exponent for each metric)
        score = np.exp(pd.DataFrame(np.log(X + self.offset) * np.array(self.metric_summary['weight'])).sum(axis=1))
        return pd.DataFrame(score)

    def fit_transform(self, X, y, **fit_params):
        self.fit(X, y, **fit_params)
        return self.transform(X)

И использовал его в конвейере:

pipeline = Pipeline([
    ('transformations', QuantileTransformer()),
    ('rescale_metrics', MinMaxScaler()),
    ('weighting', CorrWeight()),
    ('rescale_score', MinMaxScaler())
])

Однако, когда я пытаюсь передать параметры настроенному трансформатору:

params = {'weighting__offset': 2,
          'weighting__max_weight': 5}

pipeline.fit(metrics, [], **params)

Я получил сообщение об ошибке, говорящее

NameError: name 'offset' is not defined

Как правильно передать параметры?

1 Ответ

0 голосов
/ 13 февраля 2019

Вы должны установить параметры трансформатора в самой функции конструктора.Даже если вы хотите настроить эти гиперпараметры, лучше всего использовать их в конструкторе.

Попробуйте это!

from sklearn.preprocessing import MinMaxScaler, QuantileTransformer
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.base import TransformerMixin 

class CorrWeight(TransformerMixin):
    """Calculate the weights of metrics based on their correlations, and transform to scores based on the resulting weights."""
    def __init__(self,offset,max_weight):
        self.offset = offset
        self.max_weight = max_weight

    def fit(self, X, y):
         #remove the fit_params here
        corr_mat = pd.DataFrame(X).corr()
        # Sum over all correlations to get an overall correlation score for each metric
        metric_summary = corr_mat.sum().to_frame().rename(columns={0: 'overall correlation'})
        # Assign a weight to each metric that is inversely related to the overall correlation
        # so that the higher the overall correlation, the lower the weight
        if self.max_weight > 1:
            metric_summary['weight'] = 1 / MinMaxScaler((1 / self.max_weight, 1)).fit_transform(
                metric_summary[['overall correlation']])
        elif self.max_weight == 1:
            metric_summary['weight'] = 1
        else:
            print("Max_weight has to be no smaller than 1!")
            metric_summary['weight'] = 1
        self.metric_summary = metric_summary
        return self

    def transform(self, X, **fit_params):
        # Multiply the rescaled metrics together (with the weight being the exponent for each metric)
        score = np.exp(pd.DataFrame(np.log(X + self.offset) * np.array(self.metric_summary['weight'])).sum(axis=1))
        return pd.DataFrame(score)

    def fit_transform(self, X, y, **fit_params):
        self.fit(X, y, **fit_params)
        return self.transform(X)

pipeline = Pipeline([
    ('transformations', QuantileTransformer()),
    ('rescale_metrics', MinMaxScaler()),
    ('weighting', CorrWeight(offset=2,max_weight=5)), 
    #feed the params value when you define the transformer
    ('rescale_score', MinMaxScaler())
])

pipeline.fit(np.random.rand(10,10), []) # you can remove the params here

Может быть, если вы строго хотите использовать его как fit_params, тогда определите функцию подбора следующим образом:

def fit(self, X, y, offset=None,max_weight=None):
    self.offset = offset
    self.max_weight = max_weight
    ...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...