|
|
|
|
|
from datetime import datetime |
|
import numpy as np |
|
import pandas as pd |
|
import random |
|
from transformers import BertTokenizer, BertModel |
|
import logging |
|
import matplotlib.pyplot as plt |
|
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased') |
|
import itertools |
|
from sklearn.preprocessing import StandardScaler |
|
from itertools import cycle,islice |
|
from random import sample |
|
import torch |
|
import torch.nn as nn |
|
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler |
|
import torch.nn.functional as F |
|
|
|
|
|
if torch.cuda.is_available(): |
|
device = torch.device("cuda") |
|
print(f'There are {torch.cuda.device_count()} GPU(s) available.') |
|
print('Device name:', torch.cuda.get_device_name(0)) |
|
|
|
else: |
|
print('No GPU available, using the CPU instead.') |
|
device = torch.device("cpu") |
|
|
|
|
|
def load_dictionary(file): |
|
df=pd.read_csv(file).reset_index().rename(columns={"index": 'index_in_dic'}) |
|
df['term2']=df['term'] |
|
df.term=df.term.str.replace("_", " ") |
|
df['len_Bert']=df.apply(lambda x: len(tokenizer.tokenize(x['term'])),axis=1) |
|
|
|
return(df) |
|
|
|
Modifiers =load_dictionary("FullSurveyorInteract_Modifiers.csv") |
|
Behaviors=load_dictionary("FullSurveyorInteract_Behaviors.csv") |
|
Identities=load_dictionary("FullSurveyorInteract_Identities.csv") |
|
|
|
n_Modifiers = Modifiers.copy() |
|
n_Behaviors =Behaviors.copy() |
|
n_Identities = Identities.copy() |
|
|
|
scaler_B = StandardScaler() |
|
scaler_M = StandardScaler() |
|
scaler_I = StandardScaler() |
|
|
|
n_Behaviors[['E','P','A']] = scaler_B.fit_transform(Behaviors[['E','P','A']]) |
|
n_Modifiers[['E','P','A']] = scaler_M.fit_transform(Modifiers[['E','P','A']]) |
|
n_Identities[['E','P','A']] = scaler_I.fit_transform(Identities[['E','P','A']]) |
|
|
|
|
|
|
|
|
|
rnd_st=42 |
|
|
|
|
|
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased', do_lower_case=True) |
|
|
|
|
|
def preprocessing_for_bert(data,MAX_LEN=40): |
|
"""Perform required preprocessing steps for pretrained BERT. |
|
@param data (np.array): Array of texts to be processed. |
|
@return input_ids (torch.Tensor): Tensor of token ids to be fed to a model. |
|
@return attention_masks (torch.Tensor): Tensor of indices specifying which |
|
tokens should be attended to by the model. |
|
""" |
|
|
|
input_ids = [] |
|
attention_masks = [] |
|
|
|
|
|
for sent in data: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encoded_sent = tokenizer.encode_plus( |
|
text=sent, |
|
add_special_tokens=True, |
|
max_length=MAX_LEN, |
|
|
|
padding='max_length', |
|
|
|
return_attention_mask=True |
|
) |
|
|
|
|
|
input_ids.append(encoded_sent.get('input_ids')) |
|
attention_masks.append(encoded_sent.get('attention_mask')) |
|
|
|
|
|
input_ids = torch.tensor(input_ids[0]) |
|
attention_masks = torch.tensor(attention_masks[0]) |
|
|
|
return input_ids, attention_masks |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gnrtr2(Identity,Behavior,Modifier): |
|
ident1=Identity.sample(axis = 0) |
|
ident2=Identity.sample(axis = 0) |
|
behav=Behavior.sample(axis = 0) |
|
modif1=Modifier.sample(axis = 0) |
|
modif2=Modifier.sample(axis = 0) |
|
id1=list(ident1.term) |
|
id2=list(ident2.term) |
|
beh=list(behav.term) |
|
mod1=list(modif1.term) |
|
mod2=list(modif2.term) |
|
sents=' '.join(map(str, (mod1+id1+beh+mod2+id2))) |
|
values=np.concatenate([(modif1[['E','P','A']]).to_numpy(), |
|
(ident1[['E','P','A']]).to_numpy(), |
|
(behav[['E','P','A']]).to_numpy(), |
|
(modif2[['E','P','A']]).to_numpy(), |
|
(ident2[['E','P','A']]).to_numpy()], axis=1)[0] |
|
indexx=torch.tensor([[(modif1['index_in_dic']).to_numpy()][0][0], |
|
[(ident1['index_in_dic']).to_numpy()][0][0], |
|
[(behav['index_in_dic']).to_numpy()][0][0], |
|
[(modif2['index_in_dic']).to_numpy()][0][0], |
|
[(ident2['index_in_dic']).to_numpy()][0][0]]) |
|
ys= torch.tensor(values) |
|
inputs, masks = preprocessing_for_bert([sents]) |
|
|
|
|
|
yield inputs, masks, ys,indexx |
|
|
|
def dta_ldr2(I,B,M,batch_size=32): |
|
dt_ldr= [x for x in DataLoader([next(gnrtr2(I,B,M)) for x in range(batch_size)], batch_size=batch_size)][0] |
|
return(dt_ldr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
def dta_ldr(I,B,M,batch_size=32): |
|
dt_ldr= [x for x in DataLoader([next(gnrtr(I,B,M)) for x in range(batch_size)], batch_size=batch_size)][0] |
|
return(dt_ldr) |
|
class BertRegressor(nn.Module): |
|
"""Bert Model for Regression Tasks. |
|
""" |
|
def __init__(self, freeze_bert=False): |
|
""" |
|
@param bert: a BertModel object |
|
@param classifier: a torch.nn.Module regressor |
|
@param freeze_bert (bool): Set `False` to fine-tune the BERT model |
|
""" |
|
super(BertRegressor, self).__init__() |
|
|
|
D_in, H, D_out = 1024, 120, 15 |
|
|
|
|
|
self.bert = BertModel.from_pretrained('bert-large-uncased') |
|
|
|
|
|
self.regressor = nn.Sequential( |
|
nn.Dropout(0.4), |
|
nn.Linear(D_in, H), |
|
nn.Dropout(0.3), |
|
nn.ReLU(), |
|
nn.Dropout(0.3), |
|
nn.Linear(H, D_out) |
|
) |
|
|
|
|
|
|
|
|
|
|
|
if freeze_bert: |
|
for param in self.bert.parameters(): |
|
param.requires_grad = False |
|
|
|
def forward(self, input_ids, attention_mask): |
|
""" |
|
Feed input to BERT and the classifier to compute logits. |
|
@param input_ids (torch.Tensor): an input tensor with shape (batch_size, |
|
max_length) |
|
@param attention_mask (torch.Tensor): a tensor that hold attention mask |
|
information with shape (batch_size, max_length) |
|
@return logits (torch.Tensor): an output tensor with shape (batch_size, |
|
num_labels) |
|
""" |
|
|
|
outputs = self.bert(input_ids=input_ids, |
|
attention_mask=attention_mask) |
|
|
|
|
|
last_hidden_state_cls = outputs[0][:, 0, :] |
|
|
|
|
|
predictions = self.regressor(last_hidden_state_cls) |
|
|
|
return predictions |
|
from transformers import AdamW, get_linear_schedule_with_warmup |
|
|
|
def initialize_model(epochs=4): |
|
"""Initialize the Bert Classifier, the optimizer and the learning rate scheduler. |
|
""" |
|
|
|
bert_regressor = BertRegressor(freeze_bert=False) |
|
|
|
|
|
bert_regressor.to(device) |
|
|
|
|
|
optimizer = AdamW(bert_regressor.parameters(), |
|
lr=2e-5, |
|
eps=1e-8, |
|
weight_decay =0.001 |
|
) |
|
|
|
|
|
total_steps = 100000 |
|
|
|
|
|
scheduler = get_linear_schedule_with_warmup(optimizer, |
|
num_warmup_steps=0, |
|
num_training_steps=total_steps) |
|
return bert_regressor, optimizer, scheduler |
|
import random |
|
import time |
|
|
|
|
|
loss_fn = nn.MSELoss() |
|
|
|
def set_seed(seed_value=42): |
|
"""Set seed for reproducibility. |
|
""" |
|
random.seed(seed_value) |
|
np.random.seed(seed_value) |
|
torch.manual_seed(seed_value) |
|
torch.cuda.manual_seed_all(seed_value) |
|
|
|
def train(model, I_trn,B_trn,M_trn,I_tst,B_tst,M_tst, |
|
batch_size_tst=32, batch_size=50,batch_epochs=400, evaluation=False,batch_size_trn=32): |
|
"""Train the BertClassifier model. |
|
""" |
|
|
|
|
|
|
|
print("Start training...\n") |
|
|
|
|
|
|
|
|
|
print(f" {'Batch':^5} | {'Train Loss':^12} | {'Val Loss':^10} | {'Elapsed':^9}") |
|
print("-"*50) |
|
|
|
t0_batch = time.time() |
|
|
|
batch_loss, batch_counts = 0, 0 |
|
|
|
model.train() |
|
|
|
for batch in range(batch_epochs): |
|
batch_counts +=1 |
|
if ((batch==(456))):break |
|
|
|
|
|
b_input_ids, b_attn_mask, b_ys,_ = tuple(t.to(device) for t in dta_ldr(I=I_trn,B=B_trn,M=M_trn,batch_size=batch_size_trn)) |
|
|
|
model.zero_grad() |
|
|
|
|
|
preds = model(b_input_ids, b_attn_mask) |
|
|
|
loss = loss_fn(preds.float(), b_ys.float()) |
|
batch_loss += loss.item() |
|
|
|
loss.backward() |
|
|
|
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0) |
|
|
|
optimizer.step() |
|
scheduler.step() |
|
|
|
|
|
if (batch_counts % 50 == 0 and batch_counts != 0) : |
|
|
|
time_elapsed = time.time() - t0_batch |
|
|
|
|
|
val_loss = evaluate(model, Ie=I_tst,Be=B_tst,Me=M_tst,batch_size_e=batch_size_tst) |
|
print(f"{batch+ 1:^7}|{batch_loss / batch_counts:^12.6f} | {val_loss:^10.6f} | {time_elapsed:^9.2f}") |
|
|
|
|
|
print("-"*50) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
batch_loss, batch_counts = 0, 0 |
|
t0_batch = time.time() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if evaluation == True: |
|
|
|
|
|
val_loss = evaluate(model, Ie=I_tst,Be=B_tst,Me=M_tst,batch_size_e=batch_size_tst) |
|
if val_loss<0.32: |
|
print('\n Consider this one with val:', val_loss,' at:',batch,'\n') |
|
print("-"*50) |
|
|
|
|
|
|
|
|
|
|
|
|
|
val_loss = evaluate(model, Ie=I_tst,Be=B_tst,Me=M_tst,batch_size_e=batch_size_tst) |
|
print(f"{batch+ 1:^7}|{batch_loss / batch_counts:^12.6f} | {val_loss:^10.6f} | {time_elapsed:^9.2f}") |
|
print("Training complete!") |
|
|
|
|
|
def evaluate(model, Ie,Be,Me,batch_size_e): |
|
"""After the completion of each training epoch, measure the model's performance |
|
on our validation set. |
|
""" |
|
|
|
|
|
model.eval() |
|
|
|
|
|
val_loss = [] |
|
|
|
|
|
for batch in range(1): |
|
|
|
b_input_ids, b_attn_mask, b_ys,_ = tuple(t.to(device) for t in dta_ldr2(Ie,Be,Me,batch_size_e)) |
|
|
|
|
|
with torch.no_grad(): |
|
preds = model(b_input_ids, b_attn_mask) |
|
|
|
|
|
loss = loss_fn(preds, b_ys) |
|
val_loss.append(loss.item()) |
|
|
|
|
|
|
|
val_loss = np.mean(val_loss) |
|
|
|
return val_loss |
|
|
|
|
|
|
|
bert_regressor = BertRegressor() |
|
bert_regressor.load_state_dict(torch.load("MABMO_product",map_location=torch.device(device))) |
|
bert_regressor.eval() |
|
|
|
|
|
|
|
|
|
def bert_predict(model, test_dataloader): |
|
"""Perform a forward pass on the trained BERT model to predict probabilities |
|
on the test set. |
|
""" |
|
|
|
|
|
model.eval() |
|
all_preds = [] |
|
|
|
for batch in range(1): |
|
|
|
b_input_ids, b_attn_mask = tuple(t.to(device) for t in test_dataloader)[:2] |
|
|
|
|
|
with torch.no_grad(): |
|
preds = model(b_input_ids, b_attn_mask) |
|
all_preds.append(preds) |
|
|
|
|
|
all_preds = torch.cat(all_preds, dim=0) |
|
|
|
return all_preds |
|
def out_df(data,predictions,df_beh=Behaviors,df_ident=Identities,df_mod=Modifiers): |
|
df2=pd.concat([pd.DataFrame(scaler_M.inverse_transform(predictions[:,0:3].cpu())), |
|
pd.DataFrame(scaler_M.inverse_transform(data[2][:,0:3])), |
|
pd.DataFrame(scaler_I.inverse_transform(predictions[:,3:6].cpu())), |
|
pd.DataFrame(scaler_I.inverse_transform(data[2][:,3:6])), |
|
pd.DataFrame(scaler_B.inverse_transform(predictions[:,6:9].cpu())), |
|
pd.DataFrame(scaler_B.inverse_transform(data[2][:,6:9])), |
|
pd.DataFrame(scaler_M.inverse_transform(predictions[:,9:12].cpu())), |
|
pd.DataFrame(scaler_M.inverse_transform(data[2][:,9:12])), |
|
pd.DataFrame(scaler_I.inverse_transform(predictions[:,12:15].cpu())), |
|
pd.DataFrame(scaler_I.inverse_transform(data[2][:,12:15])),pd.DataFrame(np.array(data[3])) |
|
],axis=1).set_axis(['EEMA', 'EPMA', 'EAMA','EM1', 'PM1', 'AM1', |
|
'EEA', 'EPA', 'EAA','EA', 'PA', 'AA', |
|
'EEB', 'EPB', 'EAB','EB', 'PB', 'AB', |
|
'EEMO', 'EPMO', 'EAMO','EM2', 'PM2', 'AM2', |
|
'EEO', 'EPO', 'EAO','EO', 'PO', 'AO', |
|
'idx_ModA','idx_Act','idx_Beh','idx_ModO','idx_Obj'], axis=1, inplace=False) |
|
df2=pd.merge(df2, df_mod[['term','index_in_dic']], left_on= ['idx_ModA'], right_on = ["index_in_dic"], |
|
how='left').rename(columns={"term": 'ModA'}).drop(['index_in_dic'], axis=1) |
|
df2=pd.merge(df2, df_ident[['term','index_in_dic']], left_on= ['idx_Act'], right_on = ["index_in_dic"], |
|
how='left').rename(columns={"term": 'Actor'}).drop(['index_in_dic'], axis=1) |
|
df2=pd.merge(df2, df_beh[['term','index_in_dic']], left_on= ['idx_Beh'], right_on = ["index_in_dic"], |
|
how='left').rename(columns={"term": 'Behavior'}).drop(['index_in_dic'], axis=1) |
|
df2=pd.merge(df2, df_mod[['term','index_in_dic']], left_on= ['idx_ModO'], right_on = ["index_in_dic"], |
|
how='left').rename(columns={"term": 'ModO'}).drop(['index_in_dic'], axis=1) |
|
df2=pd.merge(df2, df_ident[['term','index_in_dic']], left_on= ['idx_Obj'], right_on = ["index_in_dic"], |
|
how='left').rename(columns={"term": 'Object'}).drop(['index_in_dic'], axis=1) |
|
|
|
df2=df2[['EEMA','EPMA', 'EAMA', 'EEA', 'EPA', 'EAA', 'EEB', 'EPB', 'EAB','EEMO', 'EPMO', 'EAMO', 'EEO', 'EPO', 'EAO','EM1', 'PM1', 'AM1','EA', 'PA', 'AA', 'EB', 'PB','AB', 'EM2', 'PM2', 'AM2', 'EO', |
|
'PO', 'AO', 'ModA','Actor','Behavior', 'ModO', 'Object']] |
|
return(df2) |
|
|
|
def get_output(I_b=n_Identities,B_b=n_Behaviors,M_b=n_Modifiers,batch_sz=3000,batch_num=10): |
|
df=pd.DataFrame() |
|
for i in range(batch_num): |
|
q=dta_ldr2(I=I_b,B=B_b,M=M_b,batch_size=batch_sz) |
|
preds = bert_predict(bert_regressor.to(device), q) |
|
df2=out_df(data=q,predictions=preds) |
|
df=pd.concat([df,df2],axis=0) |
|
return(df) |
|
def gen_new(Identity,Behavior,Modifier,n_df,word_type): |
|
if word_type=='identity': |
|
ident1=n_df.sample(axis = 0,random_state=56) |
|
else:ident1=Identity.sample(axis = 0,random_state=6) |
|
ident2=Identity.sample(axis = 0,random_state=6) |
|
if word_type=='behavior': |
|
behav=n_df.sample(axis = 0,random_state=5) |
|
else: behav=Behavior.sample(axis = 0,random_state=5) |
|
if word_type=='modifier': |
|
modif1=n_df.sample(axis = 0,random_state=55) |
|
else: modif1=Modifier.sample(axis = 0) |
|
modif2=Modifier.sample(axis = 0,random_state=96) |
|
id1=list(ident1.term) |
|
id2=list(ident2.term) |
|
beh=list(behav.term) |
|
mod1=list(modif1.term) |
|
mod2=list(modif2.term) |
|
|
|
sents=' '.join(map(str, (mod1+id1+beh+mod2+id2))) |
|
values=np.concatenate([(modif1[['E','P','A']]).to_numpy(), |
|
(ident1[['E','P','A']]).to_numpy(), |
|
(behav[['E','P','A']]).to_numpy(), |
|
(modif2[['E','P','A']]).to_numpy(), |
|
(ident2[['E','P','A']]).to_numpy()], axis=1)[0] |
|
|
|
|
|
indexx=torch.tensor([[(modif1['index_in_dic']).to_numpy()][0][0], |
|
[(ident1['index_in_dic']).to_numpy()][0][0], |
|
[(behav['index_in_dic']).to_numpy()][0][0], |
|
[(modif2['index_in_dic']).to_numpy()][0][0], |
|
[(ident2['index_in_dic']).to_numpy()][0][0]]) |
|
ys= torch.tensor(values) |
|
|
|
|
|
inputs, masks = preprocessing_for_bert([sents]) |
|
|
|
|
|
yield inputs, masks, ys,indexx |
|
def ldr_new(I,B,M,N_df,WT,batch_size=32): |
|
dt_ldr= [x for x in DataLoader([next(gen_new(I,B,M,N_df,WT)) for x in range(batch_size)], batch_size=batch_size)][0] |
|
return(dt_ldr) |
|
|
|
|
|
cols=['EEMA', 'EPMA', 'EAMA', 'EEA', 'EPA', 'EAA', 'EEB', 'EPB', 'EAB', |
|
'EEMO', 'EPMO', 'EAMO', 'EEO', 'EPO', 'EAO', 'ModA', 'Actor', 'Behavior', 'ModO', 'Object'] |
|
def get_output_new(w,wt,I_b=n_Identities,B_b=n_Behaviors,M_b=n_Modifiers,batch_sz=300,batch_num=1,columnss=cols,cus_col=1): |
|
|
|
df=pd.DataFrame() |
|
for i in range(batch_num): |
|
new_df=pd.DataFrame({'index_in_dic':1000,'term':w,'E':10,'P':10,'A':10,'E2':10,'P2':10,'A2':10,'term2':w,'len_Bert':3}, index=[0]) |
|
q=ldr_new(I=I_b,B=B_b,M=M_b,N_df=new_df,WT=wt,batch_size=batch_sz) |
|
preds = bert_predict(bert_regressor.to(device), q) |
|
if wt=='identity': |
|
df_identity=pd.concat([Identities,new_df],axis=0) |
|
df2=out_df(data=q,predictions=preds,df_ident=df_identity) |
|
if cus_col: |
|
columnss=[ 'EEA', 'EPA', 'EAA', 'ModA', 'Actor', 'Behavior', 'ModO', 'Object'] |
|
|
|
|
|
if wt=='behavior': |
|
df_behavior=pd.concat([Behaviors,new_df],axis=0) |
|
df2=out_df(data=q,predictions=preds,df_beh=df_behavior) |
|
if cus_col: |
|
columnss=['EEB', 'EPB', 'EAB', 'ModA', 'Actor', 'Behavior', 'ModO', 'Object'] |
|
if wt=='modifier': |
|
df_modifier=pd.concat([Modifiers,new_df],axis=0) |
|
df2=out_df(data=q,predictions=preds,df_mod=df_modifier) |
|
if cus_col: |
|
columnss=['EEMA', 'EPMA', 'EAMA', 'ModA', 'Actor', 'Behavior', 'ModO', 'Object'] |
|
df=pd.concat([df,df2],axis=0) |
|
return(df[columnss]) |
|
|
|
def gen_new(Identity,Behavior,Modifier,n_df,word_type): |
|
if word_type=='identity': |
|
ident1=n_df.sample(axis = 0) |
|
else:ident1=Identity.sample(axis = 0) |
|
ident2=Identity.sample(axis = 0) |
|
if word_type=='behavior': |
|
behav=n_df.sample(axis = 0) |
|
else: behav=Behavior.sample(axis = 0) |
|
if word_type=='modifier': |
|
modif1=n_df.sample(axis = 0) |
|
else: modif1=Modifier.sample(axis = 0) |
|
modif2=Modifier.sample(axis = 0) |
|
id1=list(ident1.term) |
|
id2=list(ident2.term) |
|
beh=list(behav.term) |
|
mod1=list(modif1.term) |
|
mod2=list(modif2.term) |
|
|
|
sents=' '.join(map(str, (mod1+id1+beh+mod2+id2))) |
|
values=np.concatenate([(modif1[['E','P','A']]).to_numpy(), |
|
(ident1[['E','P','A']]).to_numpy(), |
|
(behav[['E','P','A']]).to_numpy(), |
|
(modif2[['E','P','A']]).to_numpy(), |
|
(ident2[['E','P','A']]).to_numpy()], axis=1)[0] |
|
|
|
|
|
indexx=torch.tensor([[(modif1['index_in_dic']).to_numpy()][0][0], |
|
[(ident1['index_in_dic']).to_numpy()][0][0], |
|
[(behav['index_in_dic']).to_numpy()][0][0], |
|
[(modif2['index_in_dic']).to_numpy()][0][0], |
|
[(ident2['index_in_dic']).to_numpy()][0][0]]) |
|
ys= torch.tensor(values) |
|
inputs, masks = preprocessing_for_bert([sents]) |
|
|
|
|
|
yield inputs, masks, ys,indexx |
|
def ldr_new(I,B,M,N_df,WT,batch_size=32): |
|
dt_ldr= [x for x in DataLoader([next(gen_new(I,B,M,N_df,WT)) for x in range(batch_size)], batch_size=batch_size)][0] |
|
return(dt_ldr) |
|
|
|
|
|
|
|
def sent_gen(sentence): |
|
sents=sentence |
|
indexx=torch.tensor([1,1,1,1,1,1,1,1,1,1,1,1]) |
|
ys= torch.tensor([1,1,1,1,1,1,1,1,1,1,1,1]) |
|
inputs, masks = preprocessing_for_bert([sents]) |
|
yield inputs, masks, ys,indexx |
|
def sent_ldr(sent2,batch_size=1): |
|
dt_ldr= [x for x in DataLoader([next(sent_gen(sent2)) for x in range(batch_size)], batch_size=batch_size)][0] |
|
return(dt_ldr) |
|
def EPA_sents(sent): |
|
q=sent_ldr(sent) |
|
predictions=bert_predict(bert_regressor.to(device), q) |
|
df_out=pd.concat([pd.DataFrame(scaler_M.inverse_transform(predictions[:,0:3].cpu())), |
|
pd.DataFrame(scaler_I.inverse_transform(predictions[:,3:6].cpu())), |
|
pd.DataFrame(scaler_B.inverse_transform(predictions[:,6:9].cpu())), |
|
pd.DataFrame(scaler_M.inverse_transform(predictions[:,9:12].cpu())), |
|
pd.DataFrame(scaler_I.inverse_transform(predictions[:,12:15].cpu())) |
|
],axis=1).set_axis(['EEMA', 'EPMA', 'EAMA', |
|
'EEA', 'EPA', 'EAA', 'EEB', 'EPB', 'EAB', |
|
'EEMO', 'EPMO', 'EAMO','EEO', 'EPO', 'EAO'], axis=1, inplace=False) |
|
return(df_out.round(decimals=2)) |
|
|
|
|
|
|
|
from ipywidgets import interact, IntSlider |
|
from IPython.display import display |
|
|
|
def freeze_header(df, num_rows=30, num_columns=10, step_rows=1, |
|
step_columns=1): |
|
""" |
|
Freeze the headers (column and index names) of a Pandas DataFrame. A widget |
|
enables to slide through the rows and columns. |
|
|
|
Parameters |
|
---------- |
|
df : Pandas DataFrame |
|
DataFrame to display |
|
num_rows : int, optional |
|
Number of rows to display |
|
num_columns : int, optional |
|
Number of columns to display |
|
step_rows : int, optional |
|
Step in the rows |
|
step_columns : int, optional |
|
Step in the columns |
|
|
|
Returns |
|
------- |
|
Displays the DataFrame with the widget |
|
""" |
|
@interact(last_row=IntSlider(min=min(num_rows, df.shape[0]), |
|
max=df.shape[0], |
|
step=step_rows, |
|
description='rows', |
|
readout=False, |
|
disabled=False, |
|
continuous_update=True, |
|
orientation='horizontal', |
|
slider_color='purple'), |
|
last_column=IntSlider(min=min(num_columns, df.shape[1]), |
|
max=df.shape[1], |
|
step=step_columns, |
|
description='columns', |
|
readout=False, |
|
disabled=False, |
|
continuous_update=True, |
|
orientation='horizontal', |
|
slider_color='purple')) |
|
def _freeze_header(last_row, last_column): |
|
display(df.iloc[max(0, last_row-num_rows):last_row, |
|
max(0, last_column-num_columns):last_column]) |
|
|
|
|
|
|
|
|