Я настроил трансформатор, как показано ниже:
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
Как правильно передать параметры?