Ejemplo n.º 1
0
    def predict(self, input_data: InputData, verbose=False) -> OutputData:
        if len(self.nodes_from) == 0:
            raise ValueError('')
        parent_results = []
        if verbose:
            print(f'Obtain predictions from all parent nodes: {self.model}')
        for parent in self._nodes_from_with_fixed_order():
            parent_results.append(parent.predict(input_data=input_data))

        target = input_data.target
        secondary_input = Data.from_predictions(outputs=parent_results,
                                                target=target)
        if verbose:
            print(
                f'Obtain prediction in secondary node with model: {self.model}'
            )

        preprocessed_data = copy(secondary_input)
        preprocessed_data.features = self.cache.actual_cached_state.preprocessor.apply(
            preprocessed_data.features)

        evaluation_result = self.model.predict(
            fitted_model=self.cache.actual_cached_state.model,
            data=preprocessed_data)
        return OutputData(idx=input_data.idx,
                          features=input_data.features,
                          predict=evaluation_result,
                          task_type=input_data.task_type)
Ejemplo n.º 2
0
    def fit(self, input_data: InputData, verbose=False) -> OutputData:
        if verbose:
            print(f'Trying to fit primary node with model: {self.model}')

        model_predict = self._fit_using_cache(input_data=input_data, verbose=verbose)

        return OutputData(idx=input_data.idx,
                          features=input_data.features,
                          predict=model_predict, task=input_data.task,
                          data_type=self.model.output_datatype(input_data.data_type))
Ejemplo n.º 3
0
def output_dataset():
    task_type = MachineLearningTasksEnum.classification
    samples = 1000
    x = 10.0 * np.random.rand(samples, ) - 5.0
    x = np.expand_dims(x, axis=1)
    threshold = 0.5
    y = 1.0 / (1.0 + np.exp(np.power(x, -1.0)))
    classes = np.array([0.0 if val <= threshold else 1.0 for val in y])
    classes = np.expand_dims(classes, axis=1)
    data = OutputData(idx=np.arange(0, 100),
                      features=x,
                      predict=classes,
                      task_type=task_type)

    return data
Ejemplo n.º 4
0
    def predict(self, input_data: InputData, verbose=False) -> OutputData:
        if verbose:
            print(f'Predict in primary node by model: {self.model}')
        if not self.cache:
            raise ValueError('Model must be fitted before predict')

        preprocessed_data = transformation_function_for_data(input_data.data_type,
                                                             self.model.metadata.input_types)(input_data)

        preprocessed_data.features = self.cache.actual_cached_state.preprocessor.apply(preprocessed_data.features)

        predict_train = self.model.predict(fitted_model=self.cache.actual_cached_state.model,
                                           data=preprocessed_data)
        return OutputData(idx=input_data.idx,
                          features=input_data.features,
                          predict=predict_train, task=input_data.task,
                          data_type=self.model.output_datatype(input_data.data_type))
Ejemplo n.º 5
0
    def predict(self, input_data: InputData, verbose=False) -> OutputData:
        if len(self.nodes_from) == 0:
            raise ValueError('')
        parent_results = []
        if verbose:
            print(f'Obtain predictions from all parent nodes: {self.model}')
        parent_nodes = self._nodes_from_with_fixed_order()
        target = input_data.target

        # TODO refactor
        if any(['affects_target' in parent_node.model_tags for parent_node in parent_nodes]):
            if len(parent_nodes) == 1:
                # is the previous model is the model that changes target
                parent_result = parent_nodes[0].predict(input_data=input_data)
                target = parent_result.predict

                parent_results.append(parent_result)
            else:
                raise NotImplementedError()

        else:
            for parent in parent_nodes:
                parent_results.append(parent.predict(input_data=input_data))

        secondary_input = Data.from_predictions(outputs=parent_results,
                                                target=target)
        if verbose:
            print(f'Obtain prediction in secondary node with model: {self.model}')

        preprocessed_data = transformation_function_for_data(input_data.data_type,
                                                             self.model.metadata.input_types)(secondary_input)

        evaluation_result = self.model.predict(fitted_model=self.cache.actual_cached_state.model,
                                               data=preprocessed_data)
        return OutputData(idx=input_data.idx,
                          features=input_data.features,
                          predict=evaluation_result,
                          data_type=self.model.output_datatype(input_data.data_type),
                          task=input_data.task)
Ejemplo n.º 6
0
    def fit(self, input_data: InputData, verbose=False) -> OutputData:
        if len(self.nodes_from) == 0:
            raise ValueError()
        parent_results = []

        if verbose:
            print(f'Fit all parent nodes in secondary node with model: {self.model}')

        target = input_data.target
        parent_nodes = self._nodes_from_with_fixed_order()

        are_nodes_affect_target = ['affects_target' in parent_node.model_tags for parent_node in parent_nodes]
        if any(are_nodes_affect_target):
            if len(parent_nodes) == 1:
                # is the previous model is the model that changes target
                parent_result = parent_nodes[0].fit(input_data=input_data)
                target = parent_result.predict

                parent_results.append(parent_result)
            else:
                raise NotImplementedError()

        else:
            for parent in parent_nodes:
                parent_results.append(parent.fit(input_data=input_data))

        secondary_input = Data.from_predictions(outputs=parent_results,
                                                target=target)
        if verbose:
            print(f'Trying to fit secondary node with model: {self.model}')

        model_predict = self._fit_using_cache(input_data=secondary_input, with_preprocessing=False)

        return OutputData(idx=input_data.idx,
                          features=input_data.features,
                          predict=model_predict,
                          task=input_data.task,
                          data_type=self.model.output_datatype(input_data.data_type))
Ejemplo n.º 7
0
    def fit(self, input_data: InputData, verbose=False) -> OutputData:
        if len(self.nodes_from) == 0:
            raise ValueError()
        parent_results = []

        if verbose:
            print(
                f'Fit all parent nodes in secondary node with model: {self.model}'
            )
        for parent in self._nodes_from_with_fixed_order():
            parent_results.append(parent.fit(input_data=input_data))

        target = input_data.target
        secondary_input = Data.from_predictions(outputs=parent_results,
                                                target=target)
        if verbose:
            print(f'Trying to fit secondary node with model: {self.model}')

        model_predict = self._fit_using_cache(input_data=secondary_input)

        return OutputData(idx=input_data.idx,
                          features=input_data.features,
                          predict=model_predict,
                          task_type=input_data.task_type)
Ejemplo n.º 8
0
 def output_from_prediction(self, input_data, prediction):
     return OutputData(idx=input_data.idx,
                       features=input_data.features,
                       predict=prediction, task=input_data.task,
                       data_type=self.model.output_datatype(input_data.data_type))
Ejemplo n.º 9
0
def keras_model_predict(model, input_data: InputData):
    evaluation_result = model.predict(input_data.features)
    return OutputData(idx=input_data.idx,
                      features=input_data.features,
                      predict=evaluation_result,
                      task_type=input_data.task_type)