<jupyter_start><jupyter_text>Recozimento simulado (Simulated Annealing) - Auto Featuring - ExperimentoEste é um componente que usa uma técnica chamada Simulated Annealing para executar a AutoFeaturing. Este notebook apresenta:- como carregar modelos e outros resultados do treinamento.- como utilizar um modelo para fornecer predições em tempo real.  Declaração de Classe para Predições em Tempo RealA tarefa de implantação cria um serviço REST para predições em tempo real.Para isso você deve criar uma classe `Model` que implementa o método `predict`.<jupyter_code>%%writefile Model.py
import joblib
import numpy as np
import pandas as pd


class Transformer:
    def __init__(self):
        # Available transformations
        self.av_transformations = {
            "numeric": ["sin", "cos", "tan", "square"],
            "grouped": ["mean", "median", "std", "sum"],
            "time": ["second", "minute", "hour", "day", "month", "year", "dayofweek"],
        }

    def apply_numeric(self, data, column):

        trans, col = column.split("---")

        np_func = getattr(np, trans)

        return data[col].apply(lambda x: np_func(x))

    def apply_timely(self, data, column):

        trans, col = column.split("---")

        data[col] = data[col].astype(str)
        data[col] = pd.to_datetime(data[col], infer_datetime_format=True)

        if trans == "dayofweek" or trans == "weekday":
            return data[col].dt.dayofweek
        else:
            return data[col].apply(lambda x: getattr(x, trans))

    def is_number_repl_isdigit(self, s):
        """
        Returns True ifstring is a number.
        """
        return s.replace(".", "", 1).isdigit()

    def apply_grouped(self, data, trans_applied, column):

        new_data = data.copy()
        distinct_labels = trans_applied[column]

        new_data["temp_grouping_column"] = 0

        for label in distinct_labels:
            # Convert to original type
            c_label = label
            if self.is_number_repl_isdigit(label):
                if label.isdigit():
                    c_label = int(label)
                else:
                    c_label = float(label)

            new_data.loc[
                new_data[column.split("---")[2]] == c_label, "temp_grouping_column"
            ] = distinct_labels[label]

        # Verify if a label was not in the distinct labels
        if 0 in new_data["temp_grouping_column"].unique():
            new_data["temp_grouping_column"] = new_data[
                new_data["temp_grouping_column"] == 0
            ][column.split("---")[1]]

        return new_data["temp_grouping_column"]

    def transform(self, data, trans_applied, column):

        if trans_applied[column] == "":

            if column.split("---")[0] in self.av_transformations["numeric"]:
                data[column] = self.apply_numeric(data, column)
            else:
                data[column] = self.apply_timely(data, column)

        else:
            data[column] = self.apply_grouped(data, trans_applied, column)

        return data


class Model(object):
    def __init__(self):
        # Carrega artefatos: estimador, etc
        artifacts = joblib.load("/tmp/data/simulated-annealing.joblib")
        self.features = artifacts["features"]
        self.transformations = artifacts["transformations"]
        self.features_names_training = artifacts["columns"]

    def class_names(self):
        return self.features_names_training

    def predict(self, X, feature_names, meta=None):
        data = pd.DataFrame(data=X, columns=self.features_names_training)

        if feature_names:
            # Antes de utilizar o conjunto de dados X no modelo, reordena suas features de acordo com a ordem utilizada no treinamento
            df = pd.DataFrame(X, columns=feature_names)
            data = df[self.features_names_training]

        t = Transformer()
        for column in self.transformations:
            data = t.transform(data, self.transformations, column)

        return data.to_numpy()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10842779694430754, 'fraction_numerical': 0.0017249876786594382, 'mean_word_length': 2.5511811023622046, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '88106', 'license': 'MIT', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1171, 'n_words': 311, 'path': 'tasks/simulated-annealing/Deployment.ipynb', 'repo_name': 'vitoryeso/tasks', 'size': '4058'}