import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    df['SOURCE_NAME'] = df['SOURCE_NAME'].str.rsplit('_', 1).str.get(-1)
    return df

df = g(df.copy())

###END SOLUTION
result = df

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

C, D = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(C, D):
    return pd.concat([C,D]).drop_duplicates('A', keep='last').sort_values(by=['A']).reset_index(drop=True)

result = g(C.copy(),D.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    return df.set_axis([*df.columns[:-1], 'Test'], axis=1, inplace=False)

result = g(df.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

corr = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(corr):
    corr_triu = corr.where(~np.tril(np.ones(corr.shape)).astype(bool))
    corr_triu = corr_triu.stack()
    return corr_triu[corr_triu > 0.3]

result = g(corr.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    total_len = len(df)
    zero_len = (df['Column_x'] == 0).sum()
    idx = df['Column_x'].index[df['Column_x'].isnull()]
    total_nan_len = len(idx)
    first_nan = (total_len // 2) - zero_len
    df.loc[idx[0:first_nan], 'Column_x'] = 0
    df.loc[idx[first_nan:total_nan_len], 'Column_x'] = 1
    return df

df = g(df.copy())

###END SOLUTION
result = df

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    return df.loc[df['Field1'].astype(str).str.isdigit(), 'Field1'].tolist()

df = g(df.copy())

###END SOLUTION
result = df

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    to_delete = ['2020-02-17', '2020-02-18']
    return df[~(df.index.strftime('%Y-%m-%d').isin(to_delete))]

result = g(df.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    return pd.pivot_table(df, values=['D','E'], index=['B'], aggfunc={'D':np.sum, 'E':np.mean})

result = g(df.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    df['bar'] = df['bar'].replace("NULL", 0)
    res = df.groupby(["id1", "id2"])[["foo", "bar"]].mean()
    return res

result = g(df.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    df['TIME'] = pd.to_datetime(df['TIME'])
    df['TIME'] = df['TIME'].dt.strftime('%d-%b-%Y %a %T')
    df['RANK'] = df.groupby('ID')['TIME'].rank(ascending=False)
    return df

df = g(df.copy())

###END SOLUTION
result = df

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

def f(df):
    ### BEGIN SOLUTION
    result = df.loc[~df['Field1'].astype(str).str.isdigit(), 'Field1'].tolist()

    return result

    ### END SOLUTION

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
result = f(df)

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    df[['time', 'number']] = df.duration.str.extract(r'\s*(.*)(\d+)', expand=True)
    for i in df.index:
        df.loc[i, 'time'] = df.loc[i, 'time'].strip()
        df.loc[i, 'number'] = eval(df.loc[i,'number'])
    df['time_days'] = df['time'].replace(['year', 'month', 'week', 'day'], [365, 30, 7, 1], regex=True)
    df['time_days'] *= df['number']
    return df

df = g(df.copy())

###END SOLUTION
result = df

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df, thresh = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df, thresh):
    return (df[lambda x: x['value'] >= thresh] .append(df[lambda x: x['value'] < thresh].sum().rename('X')))

result = g(df.copy(),thresh)

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

dict, df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
import numpy as np
def g(dict, df):
    df["Date"] = df["Member"].apply(lambda x: dict.get(x)).fillna(np.NAN)
    return df

df = g(dict.copy(),df.copy())

###END SOLUTION
result = df

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import pickle

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default=1)
args = parser.parse_args()
import pandas as pd
import numpy as np

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
###BEGIN SOLUTION
def g(df):
    return pd.melt(df)

result = g(df.copy())

###END SOLUTION

with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(softmax_output):
### BEGIN SOLUTION
# def solve(softmax_output):
    y = torch.argmax(softmax_output, dim=1).view(-1, 1)
    # return y
# y = solve(softmax_output)

### END SOLUTION
    return y
y = solve(softmax_output)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_log, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
C = B[:, A_log.bool()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
x_tensor = torch.from_numpy(x_array.astype(float))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(x_tensor, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_not_equal = int(len(A)) - int((A == B).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_not_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import torch

optim = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for param_group in optim.param_groups:
    param_group['lr'] = 0.001

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(optim, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
result = torch.ones((t.shape[0] + 2, t.shape[1] + 2)) * -1
result[1:-1, 1:-1] = t
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

ids, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idx = ids.repeat(1, 114).view(30, 1, 114)
result = torch.gather(x, 1, idx)
result = result.squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch
from gensim.models import Word2Vec
from gensim.test.utils import common_texts

input_Tensor = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
word2vec = Word2Vec(sentences=common_texts, vector_size=100, window=5, min_count=1, workers=4)
def get_embedded_input(input_Tensor):
### BEGIN SOLUTION
# def get_embedded_input(input_Tensor):
    weights = torch.FloatTensor(word2vec.wv.vectors)
    embedding = torch.nn.Embedding.from_pretrained(weights)
    embedded_input = embedding(input_Tensor)
    # return embedded_input
### END SOLUTION
    return embedded_input
embedded_input = get_embedded_input(input_Tensor)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(embedded_input, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

lens = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
max_len = max(lens)
mask = torch.arange(max_len).expand(len(lens), max_len) < lens.unsqueeze(1)
mask = mask.type(torch.LongTensor)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(mask, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Convert(a):
### BEGIN SOLUTION
# def Convert(a):
    ### BEGIN SOLUTION
    t = torch.from_numpy(a.astype(float))
    ### END SOLUTION
    # return t
# x_tensor = Convert(x_array)

### END SOLUTION
    return t
x_tensor = Convert(x_array)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(x_tensor, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
px = pd.DataFrame(x.numpy())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(px, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, :lengths[i_batch], :] = 0
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

lens = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def get_mask(lens):
### BEGIN SOLUTION
# def get_mask(lens):
    ### BEGIN SOLUTION
    max_len = max(lens)
    mask = torch.arange(max_len).expand(len(lens), max_len) < lens.unsqueeze(1)
    mask = mask.type(torch.LongTensor)
    ### END SOLUTION
    # return mask
# mask = get_mask(lens)
### END SOLUTION
    return mask
mask = get_mask(lens)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(mask, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

ids, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idx = ids.repeat(1, 2).view(70, 1, 2)
result = torch.gather(x, 1, idx)
result = result.squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
y = torch.argmin(softmax_output, dim=1).view(-1, 1)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_log, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i in range(len(A_log)):
    if A_log[i] == 1:
        A_log[i] = 0
    else:
        A_log[i] = 1
C = B[:, A_log.bool()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

list_of_tensors = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
tensor_of_tensors = torch.stack((list_of_tensors))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensor_of_tensors, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(softmax_output):
### BEGIN SOLUTION
# def solve(softmax_output):
    ### BEGIN SOLUTION
    y = torch.argmin(softmax_output, dim=1).detach()
    ### END SOLUTION
    # return y
# y = solve(softmax_output)

### END SOLUTION
    return y
y = solve(softmax_output)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t, idx = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idxs = torch.from_numpy(idx).long().unsqueeze(1)
# or   torch.from_numpy(idxs).long().view(-1,1)
result = t.gather(1, idxs).squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import torch

optim = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for param_group in optim.param_groups:
    param_group['lr'] = 0.0005

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(optim, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

mask, clean_input_spectrogram, output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i in range(len(mask[0])):
    if mask[0][i] == 1:
        mask[0][i] = 0
    else:
        mask[0][i] = 1
output[:, mask[0].to(torch.bool), :] = clean_input_spectrogram[:, mask[0].to(torch.bool), :]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(output, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

lens = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
max_len = max(lens)
mask = torch.arange(max_len).expand(len(lens), max_len) > (max_len - lens.unsqueeze(1) - 1)
mask = mask.type(torch.LongTensor)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(mask, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
result = torch.nn.functional.pad(t, (1, 1, 1, 1))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
y = torch.argmax(softmax_output, dim=1).view(-1, 1)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

Tensor_2D = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
Tensor_3D = torch.diag_embed(Tensor_2D)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Tensor_3D, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
chunk_dim=10
### BEGIN SOLUTION
Temp = a.unfold(2, chunk_dim, 1)
tensors_31 = []
for i in range(Temp.shape[2]):
    tensors_31.append(Temp[:, :, i, :, :].view(1, 3, chunk_dim, 10, 1).numpy())
tensors_31 = torch.from_numpy(np.array(tensors_31))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensors_31, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_equal = int((A[int(len(A) / 2):] == B[int(len(A) / 2):]).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
chunk_dim=10
### BEGIN SOLUTION
Temp = a.unfold(3, chunk_dim, 1)
tensors_31 = []
for i in range(Temp.shape[3]):
    tensors_31.append(Temp[:, :, :, i, :].view(1, 3, 10, chunk_dim, 1).numpy())
tensors_31 = torch.from_numpy(np.array(tensors_31))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensors_31, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(x, y):
### BEGIN SOLUTION
# def solve(x, y):
    ### BEGIN SOLUTION
    mins = torch.min(torch.abs(x), torch.abs(y))

    xSigns = (mins == torch.abs(x)) * torch.sign(x)
    ySigns = (mins == torch.abs(y)) * torch.sign(y)
    finalSigns = xSigns.int() | ySigns.int()

    signed_min = mins * finalSigns
    ### END SOLUTION
    # return signed_min
# signed_min = solve(x, y)

### END SOLUTION
    return signed_min
signed_min = solve(x, y)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(signed_min, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
px = pd.DataFrame(x.numpy())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(px, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

mask, clean_input_spectrogram, output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
output[:, mask[0].to(torch.bool), :] = clean_input_spectrogram[:, mask[0].to(torch.bool), :]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(output, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_equal = int((A == B).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, :lengths[i_batch], :] = 2333
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

list = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
new_tensors = torch.stack((list))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_tensors, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

torch.random.manual_seed(42)
hid_dim = 32
data = torch.randn(10, 2, 3, hid_dim)
data = data.view(10, 2 * 3, hid_dim)
W = torch.randn(hid_dim)
### BEGIN SOLUTION
W = W.unsqueeze(0).unsqueeze(0).expand(*data.size())
result = torch.sum(data * W, 2)
result = result.view(10, 2, 3)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

MyNet = torch.nn.Sequential(torch.nn.Linear(4, 15),
                            torch.nn.Sigmoid(),
                            torch.nn.Linear(15, 3),
                            )
MyNet.load_state_dict(torch.load("my_model.pt"))
input = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
'''
training part
'''
# X, Y = load_iris(return_X_y=True)
# lossFunc = torch.nn.CrossEntropyLoss()
# opt = torch.optim.Adam(MyNet.parameters(), lr=0.001)
# for batch in range(0, 50):
#     for i in range(len(X)):
#         x = MyNet(torch.from_numpy(X[i]).float()).reshape(1, 3)
#         y = torch.tensor(Y[i]).long().unsqueeze(0)
#         loss = lossFunc(x, y)
#         loss.backward()
#         opt.step()
#         opt.zero_grad()
#         # print(x.grad)
#         # print(loss)
#     # print(loss)
output = MyNet(input)
probs = torch.nn.functional.softmax(output.reshape(1, 3), dim=1)
confidence_score, classes = torch.max(probs, 1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(confidence_score, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_not_equal = int((A[int(len(A) / 2):] != B[int(len(A) / 2):]).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_not_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Count(A, B):
### BEGIN SOLUTION
# def Count(A, B):
    ### BEGIN SOLUTION
    cnt_equal = int((A == B).sum())
    ### END SOLUTION
    # return cnt_equal
# cnt_equal = Count(A, B)

### END SOLUTION
    return cnt_equal
cnt_equal = Count(A, B)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, lengths[i_batch]:, :] = 0
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t, idx = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idxs = torch.from_numpy(idx).long().unsqueeze(1)
# or   torch.from_numpy(idxs).long().view(-1,1)
result = t.gather(1, idxs).squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

Tensor_2D = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Convert(t):
### BEGIN SOLUTION
# def Convert(t):
    ### BEGIN SOLUTION
    result = torch.diag_embed(t)
    ### END SOLUTION
    # return result
# Tensor_3D = Convert(Tensor_2D)

### END SOLUTION
    return result
Tensor_3D = Convert(Tensor_2D)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Tensor_3D, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_logical, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
C = B[:, A_logical.bool()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(a, b):
### BEGIN SOLUTION
# def solve(a, b):
    ### BEGIN SOLUTION
    ab = torch.cat((a, b), 0)
    ### END SOLUTION
    # return ab
# ab = solve(a, b)

### END SOLUTION
    return ab
ab = solve(a, b)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(ab, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, lengths[i_batch]:, :] = 2333
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_log, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(A_log, B):
### BEGIN SOLUTION
# def solve(A_log, B):
    ### BEGIN SOLUTION
    C = B[:, A_log.bool()]
    ### END SOLUTION
    # return C
### END SOLUTION
    return C
C = solve(A_log, B)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch
from gensim.models import Word2Vec
from gensim.test.utils import common_texts

input_Tensor = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
word2vec = Word2Vec(sentences=common_texts, vector_size=100, window=5, min_count=1, workers=4)
### BEGIN SOLUTION
weights = torch.FloatTensor(word2vec.wv.vectors)
embedding = torch.nn.Embedding.from_pretrained(weights)
embedded_input = embedding(input_Tensor)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(embedded_input, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
ab = torch.cat((a, b), 0)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(ab, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

idx, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
C = B.index_select(1, idx)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import torch

optim = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for param_group in optim.param_groups:
    param_group['lr'] = 0.0005
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(optim, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
c = (a[:, -1:] + b[:, :1]) / 2
result = torch.cat((a[:, :-1], c, b[:, 1:]), dim=1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t, idx = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idx = 1 - idx
idxs = torch.from_numpy(idx).long().unsqueeze(1)
# or   torch.from_numpy(idxs).long().view(-1,1)
result = t.gather(1, idxs).squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

ids, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
ids = torch.argmax(ids, 1, True)
idx = ids.repeat(1, 2).view(70, 1, 2)
result = torch.gather(x, 1, idx)
result = result.squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

list_of_tensors = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Convert(lt):
### BEGIN SOLUTION
# def Convert(lt):
    ### BEGIN SOLUTION
    tt = torch.stack((lt))
    ### END SOLUTION
    # return tt
# tensor_of_tensors = Convert(list_of_tensors)

### END SOLUTION
    return tt
tensor_of_tensors = Convert(list_of_tensors)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensor_of_tensors, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
px = pd.DataFrame(x.numpy())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(px, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(a, b):
### BEGIN SOLUTION
# def solve(a, b):
    ### BEGIN SOLUTION
    c = (a[:, -1:] + b[:, :1]) / 2
    result = torch.cat((a[:, :-1], c, b[:, 1:]), dim=1)
    ### END SOLUTION
    # return result
# result = solve(a, b)

### END SOLUTION
    return result
result = solve(a, b)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
maxs = torch.max(torch.abs(x), torch.abs(y))

xSigns = (maxs == torch.abs(x)) * torch.sign(x)
ySigns = (maxs == torch.abs(y)) * torch.sign(y)
finalSigns = xSigns.int() | ySigns.int()

signed_max = maxs * finalSigns
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(signed_max, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
mins = torch.min(torch.abs(x), torch.abs(y))

xSigns = (mins == torch.abs(x)) * torch.sign(x)
ySigns = (mins == torch.abs(y)) * torch.sign(y)
finalSigns = xSigns.int() | ySigns.int()

signed_min = mins * finalSigns
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(signed_min, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from torch.autograd import Variable

images, labels = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
loss_func = torch.nn.CrossEntropyLoss()
loss = loss_func(images, labels)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(loss, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(softmax_output):
### BEGIN SOLUTION
# def solve(softmax_output):
    y = torch.argmax(softmax_output, dim=1).view(-1, 1)
    # return y
# y = solve(softmax_output)

    return y

### END SOLUTION
y = solve(softmax_output)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_log, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
C = B[:, A_log.bool()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
x_tensor = torch.from_numpy(x_array.astype(float))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(x_tensor, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_not_equal = int(len(A)) - int((A == B).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_not_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import torch

optim = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for param_group in optim.param_groups:
    param_group['lr'] = 0.001

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(optim, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
result = torch.ones((t.shape[0] + 2, t.shape[1] + 2)) * -1
result[1:-1, 1:-1] = t
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

ids, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idx = ids.repeat(1, 114).view(30, 1, 114)
result = torch.gather(x, 1, idx)
result = result.squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch
from gensim.models import Word2Vec
from gensim.test.utils import common_texts

input_Tensor = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
word2vec = Word2Vec(sentences=common_texts, vector_size=100, window=5, min_count=1, workers=4)
def get_embedded_input(input_Tensor):
### BEGIN SOLUTION
# def get_embedded_input(input_Tensor):
    weights = torch.FloatTensor(word2vec.wv.vectors)
    embedding = torch.nn.Embedding.from_pretrained(weights)
    embedded_input = embedding(input_Tensor)
    # return embedded_input
    return embedded_input

### END SOLUTION
embedded_input = get_embedded_input(input_Tensor)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(embedded_input, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

lens = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
max_len = max(lens)
mask = torch.arange(max_len).expand(len(lens), max_len) < lens.unsqueeze(1)
mask = mask.type(torch.LongTensor)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(mask, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Convert(a):
### BEGIN SOLUTION
# def Convert(a):
    ### BEGIN SOLUTION
    t = torch.from_numpy(a.astype(float))
    ### END SOLUTION
    # return t
# x_tensor = Convert(x_array)

    return t

### END SOLUTION
x_tensor = Convert(x_array)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(x_tensor, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, :lengths[i_batch], :] = 0
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

lens = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def get_mask(lens):
### BEGIN SOLUTION
# def get_mask(lens):
    ### BEGIN SOLUTION
    max_len = max(lens)
    mask = torch.arange(max_len).expand(len(lens), max_len) < lens.unsqueeze(1)
    mask = mask.type(torch.LongTensor)
    ### END SOLUTION
    # return mask
# mask = get_mask(lens)
    return mask

### END SOLUTION
mask = get_mask(lens)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(mask, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

ids, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idx = ids.repeat(1, 2).view(70, 1, 2)
result = torch.gather(x, 1, idx)
result = result.squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
y = torch.argmin(softmax_output, dim=1).view(-1, 1)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_log, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i in range(len(A_log)):
    if A_log[i] == 1:
        A_log[i] = 0
    else:
        A_log[i] = 1
C = B[:, A_log.bool()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

list_of_tensors = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
tensor_of_tensors = torch.stack((list_of_tensors))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensor_of_tensors, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(softmax_output):
### BEGIN SOLUTION
# def solve(softmax_output):
    ### BEGIN SOLUTION
    y = torch.argmin(softmax_output, dim=1).detach()
    ### END SOLUTION
    # return y
# y = solve(softmax_output)

### END SOLUTION
    return y
y = solve(softmax_output)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t, idx = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idxs = torch.from_numpy(idx).long().unsqueeze(1)
# or   torch.from_numpy(idxs).long().view(-1,1)
result = t.gather(1, idxs).squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import torch

optim = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for param_group in optim.param_groups:
    param_group['lr'] = 0.0005

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(optim, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

mask, clean_input_spectrogram, output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i in range(len(mask[0])):
    if mask[0][i] == 1:
        mask[0][i] = 0
    else:
        mask[0][i] = 1
output[:, mask[0].to(torch.bool), :] = clean_input_spectrogram[:, mask[0].to(torch.bool), :]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(output, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

lens = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
max_len = max(lens)
mask = torch.arange(max_len).expand(len(lens), max_len) > (max_len - lens.unsqueeze(1) - 1)
mask = mask.type(torch.LongTensor)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(mask, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
result = torch.nn.functional.pad(t, (1, 1, 1, 1))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

softmax_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
y = torch.argmax(softmax_output, dim=1).view(-1, 1)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(y, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

Tensor_2D = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
Tensor_3D = torch.diag_embed(Tensor_2D)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Tensor_3D, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
chunk_dim=10
### BEGIN SOLUTION
Temp = a.unfold(2, chunk_dim, 1)
tensors_31 = []
for i in range(Temp.shape[2]):
    tensors_31.append(Temp[:, :, i, :, :].view(1, 3, chunk_dim, 10, 1).numpy())
tensors_31 = torch.from_numpy(np.array(tensors_31))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensors_31, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_equal = int((A[int(len(A) / 2):] == B[int(len(A) / 2):]).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
chunk_dim=10
### BEGIN SOLUTION
Temp = a.unfold(3, chunk_dim, 1)
tensors_31 = []
for i in range(Temp.shape[3]):
    tensors_31.append(Temp[:, :, :, i, :].view(1, 3, 10, chunk_dim, 1).numpy())
tensors_31 = torch.from_numpy(np.array(tensors_31))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensors_31, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(x, y):
### BEGIN SOLUTION
# def solve(x, y):
    ### BEGIN SOLUTION
    mins = torch.min(torch.abs(x), torch.abs(y))

    xSigns = (mins == torch.abs(x)) * torch.sign(x)
    ySigns = (mins == torch.abs(y)) * torch.sign(y)
    finalSigns = xSigns.int() | ySigns.int()

    signed_min = mins * finalSigns
    ### END SOLUTION
    # return signed_min
# signed_min = solve(x, y)

    return signed_min

### END SOLUTION
signed_min = solve(x, y)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(signed_min, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
px = pd.DataFrame(x.numpy())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(px, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

mask, clean_input_spectrogram, output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
output[:, mask[0].to(torch.bool), :] = clean_input_spectrogram[:, mask[0].to(torch.bool), :]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(output, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_equal = int((A == B).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, :lengths[i_batch], :] = 2333
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

list = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
new_tensors = torch.stack((list))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_tensors, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

torch.random.manual_seed(42)
hid_dim = 32
data = torch.randn(10, 2, 3, hid_dim)
data = data.view(10, 2 * 3, hid_dim)
W = torch.randn(hid_dim)
### BEGIN SOLUTION
W = W.unsqueeze(0).unsqueeze(0).expand(*data.size())
result = torch.sum(data * W, 2)
result = result.view(10, 2, 3)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

MyNet = torch.nn.Sequential(torch.nn.Linear(4, 15),
                            torch.nn.Sigmoid(),
                            torch.nn.Linear(15, 3),
                            )
MyNet.load_state_dict(torch.load("my_model.pt"))
input = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
'''
training part
'''
# X, Y = load_iris(return_X_y=True)
# lossFunc = torch.nn.CrossEntropyLoss()
# opt = torch.optim.Adam(MyNet.parameters(), lr=0.001)
# for batch in range(0, 50):
#     for i in range(len(X)):
#         x = MyNet(torch.from_numpy(X[i]).float()).reshape(1, 3)
#         y = torch.tensor(Y[i]).long().unsqueeze(0)
#         loss = lossFunc(x, y)
#         loss.backward()
#         opt.step()
#         opt.zero_grad()
#         # print(x.grad)
#         # print(loss)
#     # print(loss)
output = MyNet(input)
probs = torch.nn.functional.softmax(output.reshape(1, 3), dim=1)
confidence_score, classes = torch.max(probs, 1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(confidence_score, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
cnt_not_equal = int((A[int(len(A) / 2):] != B[int(len(A) / 2):]).sum())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_not_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Count(A, B):
### BEGIN SOLUTION
# def Count(A, B):
    ### BEGIN SOLUTION
    cnt_equal = int((A == B).sum())
    ### END SOLUTION
    # return cnt_equal
# cnt_equal = Count(A, B)

    return cnt_equal

### END SOLUTION
cnt_equal = Count(A, B)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cnt_equal, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, lengths[i_batch]:, :] = 0
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t, idx = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idxs = torch.from_numpy(idx).long().unsqueeze(1)
# or   torch.from_numpy(idxs).long().view(-1,1)
result = t.gather(1, idxs).squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

Tensor_2D = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Convert(t):
### BEGIN SOLUTION
# def Convert(t):
    ### BEGIN SOLUTION
    result = torch.diag_embed(t)
    ### END SOLUTION
    # return result
# Tensor_3D = Convert(Tensor_2D)

    return result

### END SOLUTION
Tensor_3D = Convert(Tensor_2D)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(Tensor_3D, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_logical, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
C = B[:, A_logical.bool()]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(a, b):
### BEGIN SOLUTION
# def solve(a, b):
    ### BEGIN SOLUTION
    ab = torch.cat((a, b), 0)
    ### END SOLUTION
    # return ab
# ab = solve(a, b)

    return ab

### END SOLUTION
ab = solve(a, b)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(ab, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, lengths = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for i_batch in range(10):
    a[i_batch, lengths[i_batch]:, :] = 2333
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(a, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

A_log, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(A_log, B):
### BEGIN SOLUTION
# def solve(A_log, B):
    ### BEGIN SOLUTION
    C = B[:, A_log.bool()]
    ### END SOLUTION
    # return C
    return C

### END SOLUTION
C = solve(A_log, B)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch
from gensim.models import Word2Vec
from gensim.test.utils import common_texts

input_Tensor = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
word2vec = Word2Vec(sentences=common_texts, vector_size=100, window=5, min_count=1, workers=4)
### BEGIN SOLUTION
weights = torch.FloatTensor(word2vec.wv.vectors)
embedding = torch.nn.Embedding.from_pretrained(weights)
embedded_input = embedding(input_Tensor)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(embedded_input, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
ab = torch.cat((a, b), 0)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(ab, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

idx, B = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
C = B.index_select(1, idx)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(C, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import torch

optim = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
for param_group in optim.param_groups:
    param_group['lr'] = 0.0005
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(optim, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
c = (a[:, -1:] + b[:, :1]) / 2
result = torch.cat((a[:, :-1], c, b[:, 1:]), dim=1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

t, idx = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
idx = 1 - idx
idxs = torch.from_numpy(idx).long().unsqueeze(1)
# or   torch.from_numpy(idxs).long().view(-1,1)
result = t.gather(1, idxs).squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

ids, x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
ids = torch.argmax(ids, 1, True)
idx = ids.repeat(1, 2).view(70, 1, 2)
result = torch.gather(x, 1, idx)
result = result.squeeze(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

list_of_tensors = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Convert(lt):
### BEGIN SOLUTION
# def Convert(lt):
    ### BEGIN SOLUTION
    tt = torch.stack((lt))
    ### END SOLUTION
    # return tt
# tensor_of_tensors = Convert(list_of_tensors)

    return tt

### END SOLUTION
tensor_of_tensors = Convert(list_of_tensors)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tensor_of_tensors, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
px = pd.DataFrame(x.numpy())
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(px, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

a, b = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(a, b):
### BEGIN SOLUTION
# def solve(a, b):
    ### BEGIN SOLUTION
    c = (a[:, -1:] + b[:, :1]) / 2
    result = torch.cat((a[:, :-1], c, b[:, 1:]), dim=1)
    ### END SOLUTION
    # return result
# result = solve(a, b)

    return result

### END SOLUTION
result = solve(a, b)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(result, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
maxs = torch.max(torch.abs(x), torch.abs(y))

xSigns = (maxs == torch.abs(x)) * torch.sign(x)
ySigns = (maxs == torch.abs(y)) * torch.sign(y)
finalSigns = xSigns.int() | ySigns.int()

signed_max = maxs * finalSigns
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(signed_max, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch

x, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
mins = torch.min(torch.abs(x), torch.abs(y))

xSigns = (mins == torch.abs(x)) * torch.sign(x)
ySigns = (mins == torch.abs(y)) * torch.sign(y)
finalSigns = xSigns.int() | ySigns.int()

signed_min = mins * finalSigns
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(signed_min, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from torch.autograd import Variable

images, labels = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
loss_func = torch.nn.CrossEntropyLoss()
loss = loss_func(images, labels)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(loss, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

# corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
corpus = [
    'We are looking for Java developer',
    'Frontend developer with knowledge in SQL and Jscript',
    'And this is the third one.',
    'Is this the first document?',
]
### BEGIN SOLUTION
vectorizer = CountVectorizer(stop_words="english", binary=True, lowercase=False,
                             vocabulary=['Jscript', '.Net', 'TypeScript', 'NodeJS', 'Angular', 'Mongo',
                                         'CSS',
                                         'Python', 'PHP', 'Photoshop', 'Oracle', 'Linux', 'C++', "Java", 'TeamCity',
                                         'Frontend', 'Backend', 'Full stack', 'UI Design', 'Web', 'Integration',
                                         'Database design', 'UX'])
X = vectorizer.fit_transform(corpus).toarray()
feature_names = vectorizer.get_feature_names_out()

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((feature_names, X), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
### BEGIN SOLUTION
km.fit(X)
d = km.transform(X)[:, p]
indexes = np.argsort(d)[::][:50]
closest_50_samples = X[indexes]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_50_samples, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn.svm as suppmach

X, y, x_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
svmmodel=suppmach.LinearSVC(random_state=42)
### BEGIN SOLUTION
from sklearn.calibration import CalibratedClassifierCV

calibrated_svc = CalibratedClassifierCV(svmmodel, cv=5, method='sigmoid')
calibrated_svc.fit(X, y)
proba = calibrated_svc.predict_proba(x_test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="yeo-johnson")
yeo_johnson_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(yeo_johnson_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dim', PCA()), ('poly', PolynomialFeatures()), ('svm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.insert(0, ('reduce_dim', PCA()))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix

df_origin, transform_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df = pd.concat([df_origin, pd.DataFrame(transform_output.toarray())], axis=1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np

dataset = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(dataset.iloc[:, :-1], dataset.iloc[:, -1], test_size=0.2,
                                                    random_state=42)

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((x_train, x_test, y_train, y_test), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

words = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
count = CountVectorizer(lowercase=False, token_pattern='[a-zA-Z0-9$&+:;=@#|<>^*()%-]+')
vocabulary = count.fit_transform([words])
feature_names = count.get_feature_names_out()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC

corpus, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
def solve(corpus, y, vectorizer, X):
### BEGIN SOLUTION
# def solve(corpus, y, vectorizer, X):
    ### BEGIN SOLUTION
    svc = LinearSVC(penalty='l1', dual=False)
    svc.fit(X, y)
    selected_feature_names = np.asarray(vectorizer.get_feature_names_out())[np.flatnonzero(svc.coef_)]
    ### END SOLUTION
    # return selected_feature_names
# selected_feature_names = solve(corpus, y, vectorizer, X)
### END SOLUTION
    return selected_feature_names
selected_feature_names = solve(corpus, y, vectorizer, X)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(selected_feature_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

centered_scaled_data = preprocessing.scale(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(centered_scaled_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.ensemble import GradientBoostingClassifier

X_train, y_train = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
X_train[0] = ['a'] * 40 + ['b'] * 40
### BEGIN SOLUTION
catVar = pd.get_dummies(X_train[0]).to_numpy()
X_train = np.concatenate((X_train.iloc[:, 1:], catVar), axis=1)

### END SOLUTION
clf = GradientBoostingClassifier(learning_rate=0.01, max_depth=8, n_estimators=50).fit(X_train, y_train)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(X_train, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
data1 = pd.DataFrame(data=np.c_[data['data'], data['target']], columns=data['feature_names'] + ['target'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def Transform(a):
### BEGIN SOLUTION
# def Transform(a):
    ### BEGIN SOLUTION
    scaler = MinMaxScaler()
    a_one_column = a.reshape([-1, 1])
    result_one_column = scaler.fit_transform(a_one_column)
    new_a = result_one_column.reshape(a.shape)
    ### END SOLUTION
    # return new_a
# transformed = Transform(np_array)

### END SOLUTION
    return new_a
transformed = Transform(np_array)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import scipy.cluster

data_matrix = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
Z = scipy.cluster.hierarchy.linkage(np.array(data_matrix), 'ward')
cluster_labels = scipy.cluster.hierarchy.cut_tree(Z, n_clusters=2).reshape(-1, ).tolist()
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cluster_labels, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import RidgeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", RidgeClassifier(random_state=24))
])
grid = GridSearchCV(pipe, param_grid={"model__alpha": [2e-4, 3e-3, 4e-2, 5e-1]}, cv=7)
### BEGIN SOLUTION
grid.fit(X, y)
coef = grid.best_estimator_.named_steps['model'].coef_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(coef, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop('Col3')),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier

X = [['dsa', '2'], ['sato', '3']]
clf = DecisionTreeClassifier()
### BEGIN SOLUTION
from sklearn.feature_extraction import DictVectorizer

X = [dict(enumerate(x)) for x in X]
vect = DictVectorizer(sparse=False)
new_X = vect.fit_transform(X)
### END SOLUTION
clf.fit(new_X, ['4', '5'])
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(new_X, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler

np_array = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
scaler = MinMaxScaler()
X_one_column = np_array.reshape([-1, 1])
result_one_column = scaler.fit_transform(X_one_column)
transformed = result_one_column.reshape(np_array.shape)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dIm', PCA()), ('pOly', PolynomialFeatures()), ('svdm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.insert(2, ('t1919810', PCA()))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(str(clf.named_steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_dIm', PCA()), ('pOly', PolynomialFeatures()), ('svdm', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.pop(1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(str(clf.named_steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

estimators = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
# estimators = [('reduce_poly', PolynomialFeatures()), ('dim_svm', PCA()), ('sVm_233', SVC())]
clf = Pipeline(estimators)
### BEGIN SOLUTION
clf.steps.insert(0, ('reduce_dim', PCA()))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(len(clf.steps), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()

df_out = df.join(
    pd.DataFrame(
        mlb.fit_transform(df.pop(df.columns[-1])),
        index=df.index,
        columns=mlb.classes_))
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
from sklearn.pipeline import Pipeline

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
pipe = Pipeline([
    ("tf_idf", TfidfVectorizer()),
    ("nmf", NMF())
])
### BEGIN SOLUTION
pipe.fit_transform(data.test)
tf_idf_out = pipe.named_steps['tf_idf'].transform(data.test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(tf_idf_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
cv = StratifiedKFold(5).split(X, y)
logreg = LogisticRegression(random_state=42)
### BEGIN SOLUTION
from sklearn.model_selection import cross_val_predict

proba = cross_val_predict(logreg, X, y, cv=cv, method='predict_proba')
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import sklearn

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
from sklearn import preprocessing

pt = preprocessing.PowerTransformer(method="box-cox")
box_cox_data = pt.fit_transform(data)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(box_cox_data, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

pipe = Pipeline([
    ("scale", StandardScaler()),
    ("model", SGDClassifier(random_state=42))
])
grid = GridSearchCV(pipe, param_grid={"model__alpha": [1e-3, 1e-2, 1e-1, 1]}, cv=5)
X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
grid.fit(X, y)
coef = grid.best_estimator_.named_steps['model'].coef_

### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(coef, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel

X, y = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
clf = ExtraTreesClassifier(random_state=42)
clf = clf.fit(X, y)
### BEGIN SOLUTION
model = SelectFromModel(clf, prefit=True)
column_names = list(X.columns[model.get_support()])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(column_names, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix

df_origin, transform_output = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df = pd.concat([df_origin, pd.DataFrame(transform_output.toarray())], axis=1)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.ensemble import BaggingClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier

X_train, y_train = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
X_test = X_train
param_grid = {
    'base_estimator__max_depth': [1, 2, 3, 4, 5],
    'max_samples': [0.05, 0.1, 0.2, 0.5]
}
dt = DecisionTreeClassifier(max_depth=1, random_state=42)
bc = BaggingClassifier(dt, n_estimators=20, max_samples=0.5, max_features=0.5, random_state=42)
### BEGIN SOLUTION
clf = GridSearchCV(bc, param_grid)
clf.fit(X_train, y_train)

### END SOLUTION
proba = clf.predict_proba(X_test)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(proba, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
import xgboost.sklearn as xgb
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import TimeSeriesSplit

gridsearch, testX, testY, trainX, trainY = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
fit_params = {"early_stopping_rounds": 42,
              "eval_metric": "mae",
              "eval_set": [[testX, testY]]}
gridsearch.fit(trainX, trainY, **fit_params)
### END SOLUTION
b = gridsearch.score(trainX, trainY)
c = gridsearch.predict(trainX)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((b, c), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
data1 = pd.DataFrame(data=np.c_[data['data'], data['target']], columns=data['feature_names'] + ['target'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

# X, y, X_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
def preprocess(s):
    return s.upper()

tfidf = TfidfVectorizer(preprocessor=preprocess)

### END SOLUTION
try:
    assert preprocess("asdfASDFASDFWEQRqwerASDFAqwerASDFASDF") == "ASDFASDFASDFWEQRQWERASDFAQWERASDFASDF"
    assert preprocess == tfidf.preprocessor
    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("accept", f)
except:
    with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
        pickle.dump("reject", f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans

p, X = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
km = KMeans(n_clusters=8, random_state=42)
### BEGIN SOLUTION
km.fit(X)
d = km.transform(X)[:, p]
indexes = np.argsort(d)[::][:50]
closest_50_samples = X[indexes]
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(closest_50_samples, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import preprocessing

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
df_out = pd.DataFrame(preprocessing.scale(data), index=data.index, columns=data.columns)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(df_out, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.datasets import load_boston

data = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
data1 = pd.DataFrame(data.data, columns=data.feature_names)
data1['target'] = pd.Series(data.target)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(data1, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

features_dataframe = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(features_dataframe):
### BEGIN SOLUTION
# def solve(features_dataframe):
    ### BEGIN SOLUTION
    n = features_dataframe.shape[0]
    train_size = 0.2
    train_dataframe = features_dataframe.iloc[:int(n * train_size)]
    test_dataframe = features_dataframe.iloc[int(n * train_size):]
    ### END SOLUTION
    # return train_dataframe, test_dataframe
# train_dataframe, test_dataframe = solve(features_dataframe)
### END SOLUTION
    return train_dataframe, test_dataframe
train_dataframe, test_dataframe = solve(features_dataframe)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((train_dataframe, test_dataframe), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder

df = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
le = LabelEncoder()
transformed_df = df.copy()
transformed_df['Sex'] = le.fit_transform(df['Sex'])
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(transformed_df, f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn import linear_model
import statsmodels.api as sm

X_train, y_train, X_test, y_test = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
### BEGIN SOLUTION
ElasticNet = linear_model.ElasticNet()
ElasticNet.fit(X_train, y_train)
training_set_score = ElasticNet.score(X_train, y_train)
test_set_score = ElasticNet.score(X_test, y_test)
### END SOLUTION
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump((training_set_score, test_set_score), f)


import argparse
import pickle

parser = argparse.ArgumentParser()
parser.add_argument("--test_case", type=int, default="1")
args = parser.parse_args()
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

queries, documents = pickle.load(open(f"input/input{args.test_case}.pkl", "rb"))
def solve(queries, documents):
    tfidf = TfidfVectorizer()
    tfidf.fit_transform(documents)
### BEGIN SOLUTION
# def solve(queries, documents):
    ### BEGIN SOLUTION
    from sklearn.metrics.pairwise import cosine_similarity

    cosine_similarities_of_queries = []
    for query in queries:
        query_tfidf = tfidf.transform([query])
        cosine_similarities_of_queries.append(cosine_similarity(query_tfidf, tfidf.transform(documents)).flatten())
    ### END SOLUTION
    # return cosine_similarities_of_queries
# cosine_similarities_of_queries = solve(queries, documents)

### END SOLUTION
    return cosine_similarities_of_queries
cosine_similarities_of_queries = solve(queries, documents)
with open('result/result_{}.pkl'.format(args.test_case), 'wb') as f:
    pickle.dump(cosine_similarities_of_queries, f)