Kaggle competition: Abstraction and Reasoning Challenge
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<categorify>
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
from pathlib import Path
def fill_age_random(df, age_column):

 if age_column+"_filled" not in df:
df.insert(( df.columns.get_loc(age_column)+1), age_column+"_filled", df[age_column])
 keys = []
values = []
for key, value in df[age_column].value_counts().items() :
keys.append(key)
 values.append(value)
 indices = [idx for idx, b in enumerate(df[age_column].isna())if b == True]
for idx, series in df.iterrows() :
if idx in indices:
df.loc[idx, age_column+"_filled"] = random.choices(keys, values)
 return df<prepare_x_and_y>
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'
training_tasks = sorted(os.listdir(training_path))
evaluation_tasks = sorted(os.listdir(evaluation_path))
test_tasks = sorted(os.listdir(test_path))
print(len(training_tasks), len(evaluation_tasks), len(test_tasks))
y_train = train_data["Survived"]
train_data["Sex_cat"] = train_data["Sex"].astype("category")
 test_data["Sex_cat"] = test_data["Sex"].astype("category")
 features = ["Pclass", "Sex_cat", "Age_filled"]
X_train = train_data[features].copy()
X_test = test_data[features].copy()
X_train["Sex_cat"] = train_data["Sex_cat"].cat.codes
X_test["Sex_cat"] = test_data["Sex_cat"].cat.codes
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission is succesfully saved!" )<import_modules>
task_file = str(training_path / '00d62c1b.json')
 with open(task_file, 'r')as f:
task = json.load(f)
 print(task.keys() )
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd<load_from_csv>
n_train_pairs = len(task['train'])
 n_test_pairs = len(task['test'])
 print(f'task contains {n_train_pairs} training pairs')
 print(f'task contains {n_test_pairs} test pairs' )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 train = train_data.copy()
test = test_data.copy() 
<drop_column>
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )
train.drop('PassengerId', axis = 1, inplace = True )<drop_column>
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
test.drop('PassengerId', axis = 1, inplace = True)
 pred = train['Survived']<count_missing_values>
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))
train.isnull().sum()<count_missing_values>
for output_id in submission.index:
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv' )