Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<train_on_grid>
import numpy as np
import pandas as pd
from tqdm import tqdm
GBC = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, scoring="accuracy", n_jobs= -1, verbose = 1)
 gsGBC.fit(X_train,Y_train)
 GBC_best = gsGBC.best_estimator_
print("Best Score: ",gsGBC.best_score_ )<train_on_grid>
def load_data(path):
tasks = pd.Series()
for file_path in os.listdir(path):
task_file = path_join(path, file_path)
 with open(task_file, 'r')as f:
task = json.load(f)
 tasks[file_path[:-5]] = task
return tasks
RFC = RandomForestClassifier()
rf_param_grid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
gsRFC = GridSearchCV(RFC,param_grid = rf_param_grid,scoring="accuracy", n_jobs= -1, verbose = 1)
 gsRFC.fit(X_train,Y_train)
 RFC_best = gsRFC.best_estimator_
print("Best Score: ",gsRFC.best_score_ )<train_model>
train_tasks = load_data('.. /input/abstraction-and-reasoning-challenge/training/')
 evaluation_tasks = load_data('.. /input/abstraction-and-reasoning-challenge/evaluation/')
 test_tasks = load_data('.. /input/abstraction-and-reasoning-challenge/test/')
 train_tasks.head()
votingC = VotingClassifier(estimators=[('RandomForest', RFC_best),('SVC', SVMC_best),('GradientBoosting',GBC_best)], voting='soft', n_jobs=-1)
 votingC = votingC.fit(X_train, Y_train )<predict_on_test>
class CAModel(nn.Module):
def __init__(self, num_states):
super(CAModel, self ).__init__()
self.simple_conv = nn.Sequential(
 nn.Conv2d(num_states, 32, kernel_size=3, padding=1),
nn.ReLU() 
)
 self.conv1 = nn.Conv2d(32, num_states, kernel_size=1)
 self.conv2 = nn.Conv2d(32, num_states+1, kernel_size=1)
 self.soft = nn.Softmax(dim=1)
 def forward(self, x, steps = 1):
for step in range(steps):
x = self.simple_conv(x)
 y1 = self.conv1(x)
 y2 = self.conv2(x)
 x = self.soft(y1)
 return y1, y2
predictions = pd.Series(votingC.predict(test),name="Survived")
 results = pd.concat([test_ids,predictions],axis=1)
 results = results.astype(int )<save_to_csv>
def inp2img(inp):
inp = np.array(inp)
 img = np.full(( 10, inp.shape[0], inp.shape[1]), 0, dtype=np.uint8)
 for i in range(10):
img[i] =(inp==i)
 return img
def target_maker(samin, samout):
target = np.full(( len(samin), len(samin[0])) , 0, dtype=np.uint8)
 for i in range(len(samin)) :
for j in range(len(samin[0])) :
if samin[i][j]==samout[i][j]:
target[i,j] = 10
else:
target[i,j] = samout[i][j]
return target
def pred_maker(inp, pred, pred_grade):
inp = np.asarray(inp)
 final_pred = np.full(( inp.shape[0], inp.shape[1]), 0, dtype=np.uint8)
 grade_avg = np.mean(pred_grade)
 threshold = 0.66
for i in range(pred.shape[0]):
for j in range(pred.shape[1]):
if pred[i,j] == 10 or pred_grade[i, j]/grade_avg < threshold:
final_pred[i,j] = inp[i,j]
else:
final_pred[i,j] = pred[i,j]
return final_pred
class TaskSolver:
def train(self, task_train, n_epoch=100, max_steps=2):

 self.net = CAModel(10)
 criterion = CrossEntropyLoss()
optimizer = Adam(self.net.parameters() , lr = 0.01)
 for epoch in range(n_epoch):
optimizer.zero_grad()
for sample in task_train:
target = target_maker(sample['input'],sample['output'])
 inputs = FloatTensor(inp2img(sample['input'])).unsqueeze(dim=0)
 labels1 = LongTensor(sample['output'] ).unsqueeze(dim=0)
 labels2 = LongTensor(target ).unsqueeze(dim=0)
 outputs1 = self.net(inputs)[0]
outputs2 = self.net(inputs)[1]
loss = criterion(outputs1, labels1)
 loss.backward()
loss = criterion(outputs2, labels2)
 loss.backward()
optimizer.step()
return self
def predict(self, task_test):
predictions = []
pred_grades = []
with torch.no_grad() :
for sample in task_test:
inputs = FloatTensor(inp2img(sample['input'])).unsqueeze(dim=0)
 outputs = self.net(inputs)[0]
pred = outputs.squeeze(dim=0 ).cpu().numpy().argmax(0)
 final_pred = pred
predictions.append(final_pred)
 return predictions
results.to_csv("submission.csv",index=False )<load_from_csv>
def input_output_shape_is_same(task):
return all([np.array(el['input'] ).shape == np.array(el['output'] ).shape for el in task['train']])
 def calk_score(task_test, predict):
return [int(np.equal(sample['output'], pred ).all())for sample, pred in zip(task_test, predict)]
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )<define_variables>
def evaluate(tasks):
ts = TaskSolver()
result = []
predictions = []
for task in tqdm(tasks):
if input_output_shape_is_same(task):
ts.train(task['train'])
 pred = ts.predict(task['test'])
 score = calk_score(task['test'], pred)
 else:
pred = [el['input'] for el in task['test']]
score = [0]*len(task['test'])
 predictions.append(pred)
 result.append(score)
 return result, predictions
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men)
 print("% of women who survived:", rate_women )<save_to_csv>
train_result, train_predictions = evaluate(train_tasks)
 train_solved = [any(score)for score in train_result]
total = sum([len(score)for score in train_result])
 print(f"solved : {sum(train_solved)} from {total}({sum(train_solved)/total})" )
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output_flat = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output_flat.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<prepare_x_and_y>
evaluation_result, evaluation_predictions = evaluate(evaluation_tasks)
 evaluation_solved = [any(score)for score in evaluation_result]
total = sum([len(score)for score in evaluation_result])
 print(f"solved : {sum(evaluation_solved)} from {total}({sum(evaluation_solved)/total})" )
y = train_data["Survived"]
bins= [0,3,13,18,45,120]
labels = ['Infant','Child','Teen','Adolt','Senior']
train_data['AgeGroup'] = pd.cut(train_data['Age'], bins=bins, labels=labels, right=False)
 test_data['AgeGroup'] = pd.cut(train_data['Age'], bins=bins, labels=labels, right=False)
 features = ["Pclass", "Sex", "SibSp", "Parch", "AgeGroup"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output_age = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output_age.to_csv('my_submission_age.csv', index=False)
 print("Your submission was successfully saved!")
 print("Based on age groups as well:", sum(output_age["Survived"]))
print("Based on four features only:", sum(output_flat["Survived"])) 
<correct_missing_values>
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
def make_pediction(tasks):
ts = TaskSolver()
result = pd.Series()
for idx, task in tqdm(test_tasks.iteritems()):
if input_output_shape_is_same(task):
ts.train(task['train'])
 pred = ts.predict(task['test'])
 else:
pred = [el['input'] for el in task['test']]
for i, p in enumerate(pred):
result[f'{idx}_{i}'] = flattener(np.array(p ).tolist())
 return result
processed_train_data = train_data.dropna(subset=['Cabin'] )<load_from_csv>
submission = make_pediction(test_tasks)
 submission.head()
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
submission = submission.reset_index()
submission.columns = ['output_id', 'output']
submission.to_csv('submission.csv', index=False)
 submission