AutoML管道:从输入数据中提取标签,并在Neuraxle或SKLearn管道中采样



我正在从事一个项目,该项目正在寻找一个精简的Python AutoML管道实现。根据项目定义,进入管道的数据采用串行业务对象的格式,例如(人工示例):

property.json:
{
"area": "124",
"swimming_pool": "False",
"rooms" : [
... some information on individual rooms ...
]
}

机器学习目标(例如,基于其他属性预测物业是否有游泳池)存储在业务对象中,而不是在单独的标签向量中传递,并且业务对象可能包含不应用于训练的观察结果。

我在找什么

我需要一个管道引擎,它支持初始(或稍后)管道步骤,I)动态更改机器学习问题中的目标(例如,从输入数据中提取,阈值实值)和ii)对输入数据进行重采样

理想情况下,管道应如下所示(伪代码):

swimming_pool_pipeline = Pipeline([
("label_extractor", SwimmingPoolExtractor()),  # skipped in prediction mode
("sampler", DataSampler()),  # skipped in prediction mode
("featurizer", SomeFeaturization()),
("my_model", FitSomeModel())
])
swimming_pool_pipeline.fit(training_data)  # not passing in any labels
preds = swimming_pool_pipeline.predict(test_data)

管道执行引擎需要满足/允许以下内容:

  • 在模型训练(.fit())期间,SwimmingPoolExtractor从输入训练数据中提取目标标签,并将标签传递到(与自变量一起)
  • 在训练模式中,DataSampler()使用在前一步骤中提取的目标标签对观测值进行采样(例如,可以进行少数上采样或过滤观测值)
  • 在预测模式中,SwimmingPoolExtractor()什么都不做,只传递输入数据
  • 在预测模式中,DataSampler()什么都不做,只传递输入数据

示例

例如,假设数据如下所示:

property.json:
"properties" = [
{ "id_": "1",
"swimming_pool": "False",
..., 
},
{ "id_": "2",
"swimming_pool": "True",
..., 
},
{ "id_": "3",
# swimming_pool key missing
..., 
}
]

SwimmingPoolExtractor()的应用程序将提取如下内容:

"labels": [
{"id_": "1", "label": "0"}, 
{"id_": "2", "label": "1"}, 
{"id_": "3", "label": "-1"}
]

并通过它将这些设置为机器学习流水线的";目标";。

DataSampler()的应用可以例如进一步包括从不包含任何swimming_pool-密钥(label = -1)的整个训练数据集移除任何训练实例的逻辑。

后续步骤应使用修改后的训练数据(经过过滤,不包括id_=3的观察)来拟合模型。如上所述,在预测模式中,DataSamplerSwimmingPoolExtractor将刚好通过输入数据

如何

据我所知,neuraxlesklearn(我相信后者)都没有提供满足所需功能的流水线步骤(从我目前收集的信息来看,neuraxle至少必须支持切片数据,因为它实现了交叉验证元估计量)。

我是不是遗漏了什么,或者有没有办法在任何一个管道模型中实现这样的功能?如果没有,Python生态系统中列出的库是否有相当成熟的替代品,并支持此类用例(不考虑以这种方式设计管道可能产生的问题)?

"我是不是遗漏了什么,或者有没有一种方法可以实现这样的功能;

是的,使用Neuraxle,您想做的一切都可以很容易地完成:

  1. 您错过了转换输出数据的输出处理程序!这样,您就可以将一些x发送到管道内的y中(因此实际上不会像您想要的那样向fit传递任何标签)
  2. 您还错过了TrainOnlyWrapper,它只能在列车时刻转换数据!这对于在测试时(以及在验证时)停用任何管道步骤都很有用。请注意,通过这种方式,在评估验证度量时,它不会进行数据过滤或重新采样
  3. 您也可以使用AutoML对象来执行训练循环

假设您的输入数据传入";"适合";是某个东西的可迭代性(例如:不要一次传递整个json,至少制作一些可以迭代的东西)。最坏的情况是,传递一个ID列表,然后执行一个步骤,使用一个对象将ID转换为其他东西,例如,该对象可以自己使用json对传递的ID执行任何需要的操作。

这是您更新的代码:

from neuraxle.pipeline import Pipeline
class SwimmingPoolExtractor(NonFittableMixin, InputAndOutputTransformerMixin, BaseStep): # Note here: you may need to delete the NonFittableMixin from the list here if you encounter problems, and define "fit" yourself rather than having it provided here by default using the mixin class. 
def transform(self, data_inputs):
# Here, the InputAndOutputTransformerMixin will pass 
# a tuple of (x, y) rather than just x. 
x, _ = data_inputs
# Please note that you should pre-split your json into 
# lists before the pipeline so as to have this assert pass: 
assert hasattr(x, "__iter__"), "input data must be iterable at least."
x, y = self._do_my_extraction(x)  # TODO: implement this as you wish!
# Note that InputAndOutputTransformerMixin expects you 
# to return a (x, y) tuple, not only x.
outputs = (x, y) 
return outputs
class DataSampler(NonFittableMixin, BaseStep):
def transform(self, data_inputs):
# TODO: implement this as you wish!
data_inputs = self._do_my_sampling(data_inputs)
assert hasattr(x, "__iter__"), "data must stay iterable at least."
return data_inputs
swimming_pool_pipeline = Pipeline([
TrainOnlyWrapper(SwimmingPoolExtractor()),  # skipped in `.predict(...)` call
TrainOnlyWrapper(DataSampler()),  # skipped in `.predict(...)` call
SomeFeaturization(),
FitSomeModel()
])
swimming_pool_pipeline.fit(training_data)  # not passing in any labels!
preds = swimming_pool_pipeline.predict(test_data)

请注意,您还可以执行以下操作来替换对fit的调用:

auto_ml = AutoML(
swimming_pool_pipeline,
validation_splitter=ValidationSplitter(0.20),  # You can create your own splitter class if needed to replace this one. Dig in the source code of Neuraxle an see how it's done to create your own replacement. 
refit_trial=True,
n_trials=10,
epochs=1,
cache_folder_when_no_handle=str(tmpdir),
scoring_callback=ScoringCallback(mean_squared_error, higher_score_is_better=False)  # mean_squared_error from sklearn
hyperparams_repository=InMemoryHyperparamsRepository(cache_folder=str(tmpdir))
)
best_swimming_pool_pipeline = auto_ml.fit(training_data).get_best_model()
preds = best_swimming_pool_pipeline.predict(test_data)

如果要使用高级数据缓存功能,请附带说明

如果你想使用缓存,你不应该定义任何transform方法,而是应该定义handle_transform方法(或相关方法),以保持数据的顺序;ID";s重新采样数据时。Neuraxle是用来处理可迭代数据的,这就是为什么我在上面做了一些断言,以确保您的json已经被预处理,使其成为某种列表。

其他有用的代码参考:

  • https://github.com/Neuraxio/Neuraxle/blob/7957be352e564dd5dfc325f7ae23f51e9c4690a2/neuraxle/steps/data.py#L33
  • https://github.com/Neuraxio/Neuraxle/blob/d30abfc5f81b261db7c6717fb939f0e64aca1583/neuraxle/metaopt/auto_ml.py#L586

相关内容

最新更新