Kaggle competition: Titanic - Machine Learning from Disaster
T = torch.Tensor 
<define_variables>
df_train = pd.read_csv('.. /input/train.csv')
 print(df_train.columns)
 print(df_train.dtypes)
 df_test = pd.read_csv('.. /input/test.csv')
 print(df_test.columns )
SIZE = 800
EPOCHS = 30
CONV_OUT_1 = 50
CONV_OUT_2 = 100
BATCH_SIZE = 32
TEST_PATH = Path('.. /input/abstraction-and-reasoning-challenge/')
 SUBMISSION_PATH = Path('.. /input/abstraction-and-reasoning-challenge/')
 TEST_PATH = TEST_PATH / 'test'
SUBMISSION_PATH = SUBMISSION_PATH / 'sample_submission.csv'<load_pretrained>
df_all = pd.concat([df_train,df_test],axis=0 ).reset_index(drop=True )
test_task_files = sorted(os.listdir(TEST_PATH))
test_tasks = []
for task_file in test_task_files:
with open(str(TEST_PATH / task_file), 'r')as f:
task = json.load(f)
 test_tasks.append(task )<prepare_x_and_y>
df_all['Title'] = df_all['Name'].map(lambda x: re.compile(", (.*?)\." ).findall(x)[0])
 df_all['Title'][df_all.Title == 'Jonkheer'] = 'Master'
df_all['Title'][df_all.Title.isin(['Ms','Mlle'])] = 'Miss'
df_all['Title'][df_all.Title == 'Mme'] = 'Mrs'
df_all['Title'][df_all.Title.isin(['Capt', 'Don', 'Major', 'Col', 'Sir'])] = 'Sir'
df_all['Title'][df_all.Title.isin(['Dona', 'Lady', 'the Countess'])] = 'Lady'
Xs_test, Xs_train, ys_train = [], [], []
for task in test_tasks:
X_test, X_train, y_train = [], [], []
for pair in task["test"]:
X_test.append(pair["input"])
 for pair in task["train"]:
X_train.append(pair["input"])
 y_train.append(pair["output"])
 Xs_test.append(X_test)
 Xs_train.append(X_train)
 ys_train.append(y_train )<create_dataframe>
print(df_all['Title'].value_counts() )
matrices = []
for X_test in Xs_test:
for X in X_test:
matrices.append(X)
 values = []
for matrix in matrices:
for row in matrix:
for value in row:
values.append(value)
 df = pd.DataFrame(values)
 df.columns = ["values"]<define_variables>
", df_all['Deck'])
 ", df_all['Room'] )
different_y = 0
different_xy = 0
k = []
for i in range(len(training_tasks)) :
task_file = str(training_path / training_tasks[i])
 with open(task_file, 'r')as f:
task = json.load(f)
 outs = [np.array(task['train'][i]['output'])for i in range(len(task['train'])) ]
inps = [np.array(task['train'][i]['input'])for i in range(len(task['train'])) ]
if len(set([x.shape for x in outs])) > 1:
different_y += 1
if any([x.shape != y.shape for x, y in zip(outs, inps)]):
different_xy +=1
k.append(i)
 print(different_y, different_xy)
 print(k )<categorify>
df_all = pd.concat([df_all, df_all['Ticket'].str.split(' ', expand=True)], axis=1 ).drop('Ticket', axis=1)
 df_all.rename(columns={0: 'Ticket_Cls', 1: 'Ticket_Num'}, inplace=True)
 df_all['Ticket_Num'][df_all['Ticket_Num'].isnull() ]=df_all['Ticket_Cls']
df_all['Ticket_Num']=pd.to_numeric(df_all['Ticket_Num'], errors='coerce')
 df_all['Ticket_Cls'][df_all['Ticket_Cls'].str.isnumeric() ]='None'
print("Ticket_Cls
",df_all['Ticket_Cls'])
 print("Ticket_Num
",df_all['Ticket_Num'] )
def replace_values(a, d):
return np.array([d.get(i, i)for i in range(a.min() , a.max() + 1)])[a - a.min() ]
def repeat_matrix(a):
return np.concatenate([a]*(( SIZE // len(a)) + 1)) [:SIZE]
def get_new_matrix(X):
if len(set([np.array(x ).shape for x in X])) > 1:
X = np.array([X[0]])
 return X
def get_outp(outp, dictionary=None, replace=True):
if replace:
outp = replace_values(outp, dictionary)
 outp_matrix_dims = outp.shape
outp_probs_len = outp.shape[0]*outp.shape[1]*10
outp = to_categorical(outp.flatten() ,
num_classes=10 ).flatten()
return outp, outp_probs_len, outp_matrix_dims<categorify>
df_all = df_all.drop(2, axis=1)
 df_all = df_all.drop(['Cabin'], axis=1)
 df_all['Embarked'].fillna(df_all['Embarked'].dropna().mode().values.item() ,inplace=True)
 df_all['Fare'].fillna(df_all['Fare'].median() ,inplace=True)
 df_all['Ticket_Num'].fillna(df_all['Ticket_Num'].median() ,inplace=True )
class Mutator:
def __init__(self):
pass
def mutate(self, X, y=None):
return X, y
class OneOfMutator(Mutator):
def __init__(self, mutators):
self.mutators = mutators
def mutate(self, X, y=None):
return np.random.choice(self.mutators ).mutate(X, y)
 class MutationPipeline(Mutator):
def __init__(self, mutators):
self.mutators = mutators
def mutate(self, X, y=None):
for mutator in self.mutators:
X, y = mutator.mutate(X, y)
 return X, y<normalization>
df_all['Age_Cls']=0
df_all['Age_Cls'][df_all['Age']<=10] = 0
df_all['Age_Cls'][(df_all['Age']>10)&(df_all['Age']<=20)] = 1
df_all['Age_Cls'][(df_all['Age']>20)&(df_all['Age']<=30)] = 2
df_all['Age_Cls'][(df_all['Age']>30)&(df_all['Age']<=40)] = 3
df_all['Age_Cls'][(df_all['Age']>40)&(df_all['Age']<=50)] = 4
df_all['Age_Cls'][(df_all['Age']>50)&(df_all['Age']<=60)] = 5
df_all['Age_Cls'][df_all['Age']>60] = 6
class Shifter(Mutator):

 def __init__(self, sigma):
self.sigma = sigma
def mutate(self, X, y=None):
direction = np.random.randint(8)
 shift = np.random.randint(self.sigma)
 def do_shift(picture):
shifted = np.zeros(picture.shape, dtype=int)
 if shift == 0:
return picture
if direction == 0:
shifted[:,:-shift] = picture[:,shift:]
if direction == 1:
shifted[:,shift:] = picture[:,:-shift]
if direction == 2:
shifted[:-shift,:] = picture[shift:,:]
if direction == 3:
shifted[shift:,:] = picture[:-shift,:]
if direction == 4:
shifted[:-shift,:-shift] = picture[shift:,shift:]
if direction == 5:
shifted[:-shift,shift:] = picture[shift:,:-shift]
if direction == 6:
shifted[shift:,shift:] = picture[:-shift,:-shift]
if direction == 7:
shifted[shift:,:-shift] = picture[:-shift,shift:]
return shifted
return do_shift(X),(None if y is None else do_shift(y))
mutator = Shifter(2)
 fig, ax = plt.subplots(1, 4, figsize=(15,15))
plot_matrix(np.array(Xs_train[0][0]), ax[0])
 plot_matrix(np.array(ys_train[0][0]), ax[1])
 mutated = mutator.mutate(np.array(Xs_train[0][0]), np.array(ys_train[0][0]))
plot_matrix(mutated[0], ax[2])
 plot_matrix(mutated[1], ax[3] )<normalization>
df_all['Fare_Bin'] = pd.qcut(df_all['Fare'], 6, labels=['Fare_G1', 'Fare_G2', 'Fare_G3', 'Fare_G4', 'Fare_G5', 'Fare_G6'] )
class Flipper(Mutator):

 def __init__(self, do_t=False):
self.do_t = do_t
def mutate(self, X, y=None):
direction = np.random.randint(3-self.do_t)
 def do_mirror(picture):
if direction == 0:
return np.flipud(picture)
 if direction == 1:
return np.fliplr(picture)
 if direction == 2:
return picture.T
return picture
return do_mirror(X),(None if y is None else do_mirror(y))
mutator = Flipper(False)
 fig, ax = plt.subplots(1, 4, figsize=(15,15))
plot_matrix(np.array(X_train[0]), ax[0])
 plot_matrix(np.array(y_train[0]), ax[1])
 mutated = mutator.mutate(np.array(X_train[0]), np.array(y_train[0]))
plot_matrix(mutated[0], ax[2])
 plot_matrix(mutated[1], ax[3] )<define_variables>
print(df_all.nunique() /len(df_all))
df_all.drop(['Name'],axis=1,inplace=True )
def get_borders(X):
result = []
max_ = np.max([np.array(x ).shape for x in X])
 for x in X:
x = np.array(x)
 extended = np.zeros(( max_, max_))
diffw = max_ - x.shape[0]
diffh = max_ - x.shape[1]
def get_borders_add(diff):
addl = diff // 2
if addl != diff / 2.0:
addr = addl + 1
else:
addr = addl
return addl, addr
addl, addr = get_borders_add(diffw)
 addt, addb = get_borders_add(diffh)
 result.append(( addl, max_ - addr, addt, max_ - addb))
return result
def extend_matrices_to_max(X):
if len(set([np.array(x ).shape for x in X])) == 1:
return X
result = []
borders = get_borders(X)
 max_ = np.max([np.array(x ).shape for x in X])
 for i, x in enumerate(X):
x = np.array(x)
 extended = np.zeros(( max_, max_))
extended[borders[i][0]:borders[i][1], borders[i][2]:borders[i][3]] = x[:, :]
result.append(extended.astype(int))
return np.array(result)
 def narrow_prediction(prediction, orig_X, orig_y):
different_y_sizes = False
different_with_x_y = False
if len(set([x.shape for x in orig_y])) > 1:
different_y_sizes = True
if any([x.shape != y.shape for x, y in zip(orig_y, orig_X)]):
different_with_x_y = True
if not different_with_x_y:
borders = get_borders(orig_y)
 return np.array([y[borders[i][0]:borders[i][1], borders[i][2]:borders[i][3]] for i,y in enumerate(prediction)])
 result = []
for y in prediction:
for i in range(y.shape[0]):
if np.sum(y[i])> 1e-5:
left = i
for i in reversed(range(y.shape[0])) :
if np.sum(y[i])> 1e-5:
right = i + 1
for i in range(y.shape[1]):
if np.sum(y[:,i])> 1e-5:
bot = i
for i in reversed(range(y.shape[1])) :
if np.sum(y[:,i])> 1e-5:
top = i + 1
result.append(y[left:right, bot:top])
 return np.array(result)
<train_model>
scaler = preprocessing.StandardScaler()
df_all['Fare'] = scaler.fit_transform(df_all['Fare'].values.reshape(-1, 1))
df_all['Age'] = scaler.fit_transform(df_all['Age'].values.reshape(-1, 1))
df_all['Ticket_Num'] = scaler.fit_transform(df_all['Ticket_Num'].values.reshape(-1, 1))
class Extender:
def __init__(self, X_train, X_test, y_train):
self.X_train = np.array(X_train)
 self.X_test = np.array(X_test)
 self.y_train = np.array(y_train)
 def get_max_shape(X):
shapes1 = [[len(x)] for x in X]
shapes2 = [[len(x[0])] for x in X]
return np.max(( np.max(shapes1),np.max(shapes2)))
 max_train = get_max_shape(X_train)
 max_test = get_max_shape(X_test)
 max_ = np.max([max_train, max_test])
 self.X_train_borders = self.get_borders(self.X_train, max_)
 self.X_test_borders = self.get_borders(self.X_test, max_)
 self.different_y_sizes = False
self.different_with_x_y = False
if len(set([np.shape(x)for x in self.y_train])) > 1:
self.different_y_sizes = True
if any([np.shape(x)!= np.shape(y)for x, y in zip(self.y_train, self.X_train)]):
self.different_with_x_y = True
max_y_train = get_max_shape(self.y_train)
 self.y_train_borders = self.get_borders(self.y_train, max_y_train)
 self.extended_X_train = self.extend_matrices_to_max(self.X_train, max_, self.X_train_borders)
 self.extended_X_test = self.extend_matrices_to_max(self.X_test, max_, self.X_test_borders)
 self.extended_y_train = self.extend_matrices_to_max(self.y_train, max_y_train, self.y_train_borders)
 def get_borders(self, X, max_):
result = []
for x in X:
x = np.array([np.array(k)for k in x])
 extended = np.zeros(( max_, max_))
diffw = max_ - x.shape[0]
diffh = max_ - x.shape[1]
def get_borders_add(diff):
addl = diff // 2
if addl != diff / 2.0:
addr = addl + 1
else:
addr = addl
return addl, addr
addl, addr = get_borders_add(diffw)
 addt, addb = get_borders_add(diffh)
 result.append(( addl, max_ - addr, addt, max_ - addb))
return result
def extend_matrices_to_max(self, X, max_, borders):
shapes = [np.array(x ).shape for x in X]
result = []
for i, x in enumerate(X):
x = np.array([np.array(k,dtype=int)for k in x])
 extended = np.zeros(( max_, max_), dtype=int)
 extended[borders[i][0]:borders[i][1], borders[i][2]:borders[i][3]] = x[:, :]
result.append(extended)
 return np.array(result)
 def narrow_prediction(self, prediction, idxs):
if not self.different_with_x_y:
borders = self.y_train_borders
return np.array([y[borders[i][0]:borders[i][1], borders[i][2]:borders[i][3]] for y,i in zip(prediction, idxs)])
 result = []
for y in prediction:
left, bot=0,0
right, top = y.shape
for i in range(y.shape[0]):
if np.sum(y[i])> 1e-5:
left = i
break
for i in reversed(range(y.shape[0])) :
if np.sum(y[i])> 1e-5:
right = i + 1
break
for i in range(y.shape[1]):
if np.sum(y[:,i])> 1e-5:
bot = i
break
for i in reversed(range(y.shape[1])) :
if np.sum(y[:,i])> 1e-5:
top = i + 1
break
result.append(y[left:right, bot:top])
 return np.array(result)
<compute_test_metric>
df_all.drop(['Ticket_Num'],axis=1,inplace=True)
 df_all.drop(['Ticket_Cls'],axis=1,inplace=True)
 df_all['Pclass'] = df_all['Pclass'].astype(object)
 df_all['SibSp'] = df_all['SibSp'].astype(object)
 df_all['Parch'] = df_all['Parch'].astype(object)
 df_all['Age_Cls'] = df_all['Age_Cls'].astype(object)
 df_all = pd.get_dummies(df_all)
 print(df_all.columns )
ext = Extender(X_train, X_test,y_train )<concatenate>
print('Create DataSet', datetime.now() ,)
 df_all.reset_index(drop=True,inplace=True)
 y = df_all['Survived'][df_all['Survived'].isnull() == False]
x_train = df_all[:len(y)].drop(['PassengerId','Survived'],axis=1 ).values
x_test = df_all[len(y):].drop(['PassengerId','Survived'],axis=1 ).values
x_train.shape, x_test.shape, y.shape
class ARCDataset(Dataset):
def __init__(self, X_train, X_test, y_train, mutation=ColorSwitcher()):
self.mutation = mutation
self.extender = Extender(X_train, X_test, y_train)
 self.X_train = repeat_matrix(self.extender.extended_X_train)
 self.y_train = repeat_matrix(self.extender.extended_y_train)
 self.colors = []
for x in X_train:
self.colors += np.unique(x ).tolist()
for x in X_test:
self.colors += np.unique(x ).tolist()
for x in y_train:
self.colors += np.unique(x ).tolist()
def __len__(self):
return SIZE
def get_input_dimension(self):
return self.X_train[0].shape
def get_output_dimension(self):
return self.y_train[0].shape
def get_output_prob_dimension(self):
return self.y_train[0].shape
def narrow_prediction(self, prediction, idxs):
return self.extender.narrow_prediction(prediction, idxs)
 def get_extended_test(self):
return self.extender.extended_X_test
def round_colors(self, prediction):
@np.vectorize
def find_nearest(value):
array = np.array(self.colors)
 idx =(np.abs(array - value)).argmin()
return array[idx]
return find_nearest(prediction)
 def __getitem__(self, idx):
in_, out_ = self.mutation.mutate(self.X_train[idx], self.y_train[idx])
 return torch.FloatTensor(in_.copy()), torch.LongTensor(out_.copy()), self.get_output_prob_dimension()<choose_model_class>
class PerceptronClassification(BaseEstimator,ClassifierMixin):

 def __init__(self, rate = 0.01, number_of_iterations = 100):
self.rate = rate
self.number_of_iterations = number_of_iterations
def fit(self, X, y):

 self.weight_matrix = np.zeros(1 + X.shape[1])
 self.errors_list = []
for _ in range(self.number_of_iterations):
errors = 0
for xi, target in zip(X, y):
update = self.rate *(target - self.predict(xi))
self.weight_matrix[1:] += update * xi
self.weight_matrix[0] += update
errors += int(update != 0.0)
 self.errors_list.append(errors)
 return self
def dot_product(self, X):

 return(np.dot(X, self.weight_matrix[1:])+ self.weight_matrix[0])
 def predict(self, X):

 return np.where(self.dot_product(X)>= 0.0, 1, 0 )
class DoubleConv(nn.Module):

 def __init__(self, in_channels, out_channels, mid_channels=None):
super().__init__()
if not mid_channels:
mid_channels = out_channels
self.double_conv = nn.Sequential(
 nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(mid_channels),
nn.ReLU(inplace=True),
nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
 def forward(self, x):
return self.double_conv(x)
 class Down(nn.Module):

 def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
 nn.MaxPool2d(2),
DoubleConv(in_channels, out_channels)
)
 def forward(self, x):
return self.maxpool_conv(x)
 class Up(nn.Module):

 def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
 self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
 else:
self.up = nn.ConvTranspose2d(in_channels , in_channels // 2, kernel_size=2, stride=2)
 self.conv = DoubleConv(in_channels, out_channels)
 def forward(self, x1, x2):
x1 = self.up(x1)
 diffY = x2.size() [2] - x1.size() [2]
diffX = x2.size() [3] - x1.size() [3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
 x = torch.cat([x2, x1], dim=1)
 return self.conv(x)
 class OutConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(OutConv, self ).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
 def forward(self, x):
return self.conv(x)
 def shape(self):
self.conv.shape<choose_model_class>
print('Define Classification model', datetime.now() ,)
 perceptron_classifiter=PerceptronClassification(number_of_iterations = 3000 )
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, in_d, out_d, bilinear=True):
super(UNet, self ).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
 self.down1 = Down(64, 128)
 factor = 2 if bilinear else 1
self.up4 = Up(192, 64, bilinear)
 self.outc = OutConv(64, n_classes)
 self.conv = nn.Conv2d(n_classes, n_classes, kernel_size=np.abs(in_d[0]-out_d[0])+1, padding=-np.min(( in_d[0]-out_d[0], 0)))
 def forward(self, x):
x1 = self.inc(x)
 x2 = self.down1(x1)
 x = self.up4(x2, x1)
 logits = self.outc(x)
 logits = self.conv(logits)
 return logits<choose_model_class>
LR = LogisticRegression(random_state=10, solver='lbfgs', multi_class='ovr' )
class BasicCNNModel(nn.Module):
def __init__(self, inp_dim=(10, 10), outp_dim=(10, 10)) :
super(BasicCNNModel, self ).__init__()
self.network = UNet(1, 10, inp_dim, outp_dim)
 def forward(self, x):
x_ = self.network.forward(x)
 return x_ 
<choose_model_class>
ADA = AdaBoostClassifier()
a=BasicCNNModel()
a.parameters<categorify>
SVM = svm.LinearSVC()
def transform_dim(inp_dim, outp_dim, test_dim):
return(test_dim[0]*outp_dim[0]/inp_dim[0],
test_dim[1]*outp_dim[1]/inp_dim[1])
 def resize(x, test_dim, inp_dim):
if inp_dim == test_dim:
return x
else:
return cv2.resize(flt(x), inp_dim,
interpolation=cv2.INTER_AREA)
 def flt(x): return np.float32(x)
 def npy(x): return x.cpu().detach().numpy()
def itg(x): return np.int32(np.round(x))<train_model>
param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100]}
grid_search_SVM = GridSearchCV(SVM, param_grid, cv=5 )
def train_model(model, train_loader, dataset, loss, optimizer, num_epochs, scheduler=None):
loss_history = []
train_history = []
val_history = []
for epoch in range(num_epochs):
model.train()
loss_accum = 0
correct_samples = 0
total_samples = 0
for i_step, train_batch in enumerate(train_loader):
x, y, prob_d = train_batch
prediction = model(x.unsqueeze(1))
loss_value = loss(prediction, y)
 optimizer.zero_grad()
loss_value.backward()
loss_accum += loss_value
optimizer.step()
ave_loss = loss_accum /(i_step if i_step else 1)
 loss_history.append(float(ave_loss))
if scheduler is not None:
scheduler.step()
print("Average loss: %f" %(ave_loss))
if len(loss_history)> 5 and np.mean(np.abs(np.array(loss_history[-3:])- np.array(loss_history[-4:-1])))< epoch*1e-5:
break
return loss_history 
<train_on_grid>
rf_parameters = {'n_estimators': 2000, 'max_depth': 30, 'random_state':10}
RF = RandomForestClassifier(**rf_parameters )
idx = 0
start = time.time()
test_predictions = []
class Flattener(nn.Module):
def forward(self, x):
batch_size, *_ = x.shape
return x.view(batch_size, -1)
 def do_train(dataset, do_test=False):
train_loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)
 in_d = dataset.get_input_dimension()
prob_d = dataset.get_output_prob_dimension()
d = dataset.get_output_dimension()
network = BasicCNNModel(in_d, d)
 loss = nn.CrossEntropyLoss()
optimizer = Adam(network.parameters() , lr=1e-2, weight_decay=1e-3)
 step_lr = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.05)
 lh = train_model(network, train_loader, dataset, loss, optimizer, EPOCHS)
 preds = []
if do_test:
test_loader = DataLoader(np.array(dataset.extender.extended_X_test, dtype=np.single), batch_size=1, shuffle=False)
 network.eval()
for i, test_batch in enumerate(test_loader):
test_batch = test_batch
test_preds = network(torch.FloatTensor(test_batch ).unsqueeze(1))
_, indices = torch.max(test_preds, 1)
 test_preds = dataset.narrow_prediction(indices.detach().numpy() , [i])
 preds.append(dataset.round_colors(test_preds))
return lh[-1], preds
for idx,(X_train, y_train)in enumerate(zip(Xs_train, ys_train)) :
X_test = Xs_test[idx]
print("TASK " + str(idx + 1))
mutator_losses = {}
losses = []
EPOCHS = 70
if len(mutators[idx].mutators)!= 0:
dataset = ARCDataset(X_train, X_test, y_train, mutators[idx])
 train_loss, predictions = do_train(dataset, do_test=True)
 test_predictions += predictions
else:
test_predictions += X_test
end = time.time()
print("Train loss: " + str(np.round(train_loss, 3)) + " " +\
"Total time: " + str(np.round(end - start, 1)) + " s" + "
")
 fig, ax = plt.subplots(1, 4, figsize=(15,15))
plot_matrix(np.array(X_train[0]), ax[0])
 plot_matrix(np.array(y_train[0]), ax[1])
 plot_matrix(X_test[0], ax[2])
 plot_matrix(test_predictions[-1][0], ax[3] )<categorify>
NN = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(1000, 2), random_state=100 )
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred<define_variables>
GBR = GradientBoostingClassifier(n_estimators=2000, max_depth=30, max_leaf_nodes=8, min_samples_split=5, random_state =10 )
pp = 0
for k in test_predictions:
pp+=1
pp<load_from_csv>
param_grid = {'n_estimators': [2000], 'max_depth' : [20], 'random_state' : [0, 5,10]}
grid_search = GridSearchCV(RF, param_grid, cv=5 )
test_predictions = [[list(pred)for pred in test_pred] for test_pred in test_predictions]
for idx, pred in enumerate(test_predictions):
test_predictions[idx] = flattener(pred)
 submission = pd.read_csv(SUBMISSION_PATH)
 submission["output"] = test_predictions<save_to_csv>
bagging = BaggingClassifier(n_estimators=2000, max_samples=1.0, max_features=1.0, n_jobs=-1, random_state =10)
submission.to_csv("submission.csv", index=False )<save_to_csv>
Extraforest = ExtraTreesClassifier(n_estimators=2000, random_state=10 )
submission.to_csv("submission.csv", index=False )<set_options>
votemodel = VotingClassifier(estimators=[('LR', LR),('RF', RF),('GBR', GBR),('ADA', ADA),('Extraforest', Extraforest), 
('bagging_Decision tree', bagging)],voting='hard',weights=[1,1,1,1,1,1])
%matplotlib inline
%load_ext autoreload
%autoreload 2 
<define_variables>
submission_bagging=pd.DataFrame({'PassengerId':df_all['PassengerId'][df_all['Survived'].isnull() ], 'Survived':bagging.predict(x_test ).astype(int)})
 submission_votemodel=pd.DataFrame({'PassengerId':df_all['PassengerId'][df_all['Survived'].isnull() ], 'Survived':votemodel.predict(x_test ).astype(int)} )
for dirname, _, filenames in os.walk('/kaggle/input/abstraction-and-reasoning-challenge/'):
print(dirname)
 data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'
working_path = Path('/kaggle/working/')
 training_tasks = sorted(os.listdir(training_path))
evaluation_tasks = sorted(os.listdir(evaluation_path))
test_tasks = sorted(os.listdir(test_path))
print()
print(training_tasks[:5])
 print(evaluation_tasks[:5])
 print(test_tasks[:5])
<define_variables>
submission_bagging = submission_bagging.reset_index(drop=True)
 submission_votemodel = submission_votemodel.reset_index(drop=True )
EM_PATH = test_path
em_task_files = sorted(os.listdir(EM_PATH))
em_tasks = []
for task_file in em_task_files:
with open(str(EM_PATH / task_file), 'r')as f:
task = json.load(f)
 em_tasks.append(task)
 EM_LEN = len(em_tasks)
 str_tasks_prdn = []
str_tasks = []
prdn = []
print(EM_LEN)
 print(task.keys())
 print(em_task_files[:5])
 print()
print(em_tasks[0])
<concatenate>
submission_votemodel.to_csv("submission_v4_vm.csv", index=False)
 submission_bagging.to_csv("submission_v4_bagging.csv", index=False)
 print('Save submission', datetime.now() , )
<load_from_csv><EOS> 
X = x_train
y = y
ada_parameters = {'n_estimators':500, 'learning_rate':0.1}
et_parameters = {'n_jobs': -1, 'n_estimators':500, 'max_depth': 8, 'min_samples_leaf': 2, 'verbose': 0}
gb_parameters = {'n_estimators': 500, 'max_depth': 5, 'min_samples_leaf': 2, 'verbose': 0}
dt_parameters = {'max_depth':8}
knn_parameters = {'n_neighbors':2}
svm_parameters = {'kernel':'linear', 'C':0.025}
gbm_parameters = {'n_estimators': 2000, 'max_depth': 4, 'min_child_weight': 2, 'gamma':0.9, 'subsample':0.8,
'colsample_bytree':0.8, 'objective': 'binary:logistic', 'nthread':-1, 'scale_pos_weight':1}
title = "Learning Curves(RandomForest)"
plot_learning_curve(votemodel, title, X, y, ylim=(0.5, 1.01), cv=stratifiedkfold, n_jobs=4, train_sizes=[200, 300, 400, 500, 600])
 plt.show()