<jupyter_start><jupyter_text>07 - Denoising Autoencoder  Importação<jupyter_code>import pandas as pd
import numpy as np
%matplotlib inline
from sklearn.metrics import median_absolute_error, mean_squared_error
from sklearn.linear_model import Ridge
from category_encoders import OneHotEncoder
from sklearn.pipeline import make_pipeline<jupyter_output><empty_output><jupyter_text>Carga dos Dados<jupyter_code>train = pd.read_csv("data-processed/train.csv")
val = pd.read_csv("data-processed/val.csv")

train['DATE_TIME'] = pd.to_datetime(train['DATE_TIME'], format='%Y-%m-%d %H:%M:%S')
val['DATE_TIME'] = pd.to_datetime(val['DATE_TIME'], format='%Y-%m-%d %H:%M:%S')
train2 = train.copy()
train2['WEEKDAY'] = train2['DATE_TIME'].dt.weekday
train2['HOUR'] = train2['DATE_TIME'].dt.hour
train2['MINUTE'] = train2['DATE_TIME'].dt.minute

val2 = val.copy()
val2['WEEKDAY'] = val2['DATE_TIME'].dt.weekday
val2['HOUR'] = val2['DATE_TIME'].dt.hour
val2['MINUTE'] = val2['DATE_TIME'].dt.minute<jupyter_output><empty_output><jupyter_text>Denoising Autoencoder<jupyter_code>encoder = OneHotEncoder(cols=cats, use_cat_names=True)
train2_ohe = encoder.fit_transform(train2[cats])
val2_ohe = encoder.transform(val2[cats])

import torch.nn as nn
import torch
import torch.optim as opt
import torch.nn.functional as F

train2_ohe.shape
class DenoisingAE(nn.Module):
    def __init__(self):
        super(DenoisingAE, self).__init__()

        self.hid1 = nn.Linear(57, 100)
        self.hid2 = nn.Linear(100, 20)
        self.hid3 = nn.Linear(20, 100)
        self.out = nn.Linear(100, 57)
    
    def forward(self, x, return_embeddings=False):
        x = F.relu(self.hid1(x))
        x = self.hid2(x)
        
        if return_embeddings:
            return x
        
        x = F.relu(self.hid3(x))
        x = self.out(x)
        

        return x
import numba
from numba import float64
from random import random
@numba.vectorize([float64(float64, float64)], target='parallel')
def flip(x, p=0.5):
    if random() < p:
        if x == 1:
            return 0
        else: 
            return 1
    return x
    
#flip_np = np.vectorize(flip)
flip_np = flip
flip(np.array([1,1,1]), 0.5)
torch.manual_seed(0)
net = DenoisingAE().cuda()
criterion = nn.MSELoss()
optimizer = opt.Adam(net.parameters(), lr=1e-4)


x_true = torch.from_numpy(train2_ohe.values).cuda().float()
for e in range(10000):
    optimizer.zero_grad()
    x_noisy = flip_np(train2_ohe.values, 0.1) #certamente tem uma forma melhor de fazer isso
    x_noisy = torch.from_numpy(x_noisy).cuda().float()
    
    p = net(x_noisy).cuda()
    loss = criterion(p, x_true)
    #print(loss.item())
    
    loss.backward()
    optimizer.step()
    
    p_numpy = p.cpu().detach().numpy()
    if e % 500 == 0:
        print(np.sqrt(mean_squared_error(train2_ohe, p_numpy)))<jupyter_output><empty_output><jupyter_text>Embedding to Tree<jupyter_code>from sklearn.ensemble import RandomForestRegressor
mdl = RandomForestRegressor(n_jobs=-1, random_state=0, n_estimators=100)

x = torch.from_numpy(train2_ohe.values).cuda().float()
Xtr = net(x,return_embeddings=True).detach().cpu().numpy()
ytr = train2['Y4WIN']
mdl.fit(Xtr,ytr)

x = torch.from_numpy(val2_ohe.values).cuda().float()
Xval =  net(x,return_embeddings=True).detach().cpu().numpy()

p = mdl.predict(Xval)
median_absolute_error(val2['Y4'], p)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14742451154529307, 'fraction_numerical': 0.027531083481349913, 'mean_word_length': 4.2962382445141065, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '518671', 'license': 'MIT', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1284, 'n_words': 220, 'path': '07 - Denoising Autoencoder.ipynb', 'repo_name': 'rodrigolampier/feature-engineering-and-time-series', 'size': '3378'}