Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
warnings.filterwarnings('ignore')
 train = pd.read_csv(".. /input/train.csv")
 train_shape = train.shape
print(train_shape )
train_df.drop('FareBand', axis=1, inplace=True )<sort_values>
test = pd.read_csv(".. /input/test.csv")
 test_shape = test.shape
print(test_shape )
for df in all_df:
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
train_df.groupby('FamilySize', as_index=False)['Survived'].mean().sort_values('Survived', ascending=False )<sort_values>
def process_age(df, cut_points, label_names):
df["Age"] = df["Age"].fillna(-0.5)
 df["Age_categories"] = pd.cut(df["Age"], cut_points, labels=label_names)
 return df
cut_points = [-1, 0, 5, 12, 18, 35, 60, 100]
label_names = ["Missing", "Infant", "Child", "Teenager", "Young Adult", "Adult", "Senior"]
train = process_age(train, cut_points, label_names)
 test = process_age(test, cut_points, label_names )
for df in all_df:
df['Family'] = 0
df.loc[(df['FamilySize'] >= 5), 'Family'] = 1
df.loc[(df['FamilySize'] > 1)&(df['FamilySize'] < 5), 'Family'] = 2
train_df.groupby('Family', as_index=False)['Survived'].mean().sort_values('Survived', ascending=False )<drop_column>
train["Pclass"].value_counts()
train_df.drop('FamilySize', axis=1, inplace=True)
 test_df.drop('FamilySize', axis=1, inplace=True )<feature_engineering>
def create_dummies(df, column_name):
dummies = pd.get_dummies(df[column_name], prefix=column_name)
 df = pd.concat([df,dummies], axis=1)
 return df
train = create_dummies(train, "Pclass")
 test = create_dummies(test, "Pclass")
 train.head()
for df in all_df:
title = df['Name'].apply(lambda x:x.split(',')[1].split('.')[0] ).copy()
df['Title'] = title.str.strip()
train_df.groupby(['Title', 'Survived'])['PassengerId'].count().sort_values(ascending=False )<categorify>
train = create_dummies(train, "Sex")
 test = create_dummies(test, "Sex")
 train = create_dummies(train, "Age_categories")
 test = create_dummies(test, "Age_categories")
 train.head()
title_map = {"Mr": 0, "Miss": 1, "Mrs": 2, "Master": 3, "Rare": 4}
for df in all_df:
df['Title'] = df['Title'].map(title_map)
 df['Title'] = df['Title'].fillna(0)
 df['Title'] = df['Title'].astype(int)
 all_df = [train_df, test_df]
train_df.head()<drop_column>
train["SibSp_scaled"] = minmax_scale(train[["SibSp"]])
 train["Parch_scaled"] = minmax_scale(train[["Parch"]])
 train["Fare_scaled"] = minmax_scale(train[["Fare"]])
 train.head()
train_df.drop(['Name', 'Ticket'], axis=1, inplace=True)
 test_df.drop(['Name', 'Ticket'], axis=1, inplace=True )<prepare_x_and_y>
test["Fare"] = test["Fare"].fillna(train["Fare"].mean())
 test["Fare"].count()
y_train = train_df['Survived']
features = ['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', \
'Family', 'SibSp', 'Parch', 'Title', 'Cabin']
X_train = train_df[features]
X_test = test_df[features]<compute_test_metric>
test["SibSp_scaled"] = minmax_scale(test[["SibSp"]])
 test["Parch_scaled"] = minmax_scale(test[["Parch"]])
 test["Fare_scaled"] = minmax_scale(test[["Fare"]])
 test.head()
forest = RandomForestClassifier(n_estimators=153, max_depth=5, random_state=1)
 forest.fit(X_train, y_train)
 y_pred = forest.predict(X_train)
 print('Accuracy Score: ', forest.score(X_train, y_train))
print('
Confusion Matric: ', confusion_matrix(y_train, y_pred))
print('
Classification Report: ', classification_report(y_train, y_pred))<save_to_csv>
train = create_dummies(train, "Embarked")
 test = create_dummies(test, "Embarked")
 train.head()
predict = forest.predict(X_test)
 report = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predict})
 report.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
lrColumns = ['Age_categories_Missing','Age_categories_Infant',
'Age_categories_Child', 'Age_categories_Teenager',
'Age_categories_Young Adult', 'Age_categories_Adult',
'Age_categories_Senior', 'Pclass_1', 'Pclass_2', 'Pclass_3',
'Sex_female', 'Sex_male', 'Embarked_C', 'Embarked_Q', 'Embarked_S',
'SibSp_scaled', 'Parch_scaled', 'Fare_scaled']
print(lrColumns )
T = torch.Tensor 
<define_variables>
logisticRegression = LogisticRegression()
logisticRegression.fit(train[lrColumns], train["Survived"])
 coefficients = logisticRegression.coef_
print(coefficients )
SIZE = 1000
EPOCHS = 50
CONV_OUT_1 = 50
CONV_OUT_2 = 100
BATCH_SIZE = 128
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>
predictors = ['Age_categories_Infant', 'SibSp_scaled', 'Sex_female', 'Sex_male',
'Pclass_1', 'Pclass_3', 'Age_categories_Senior', 'Parch_scaled']
lr = LogisticRegression()
lr.fit(train[predictors], train["Survived"])
 predictions = lr.predict(test[predictors])
 print(predictions )
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>
scores = cross_val_score(lr, train[predictors], train["Survived"], cv=10)
 print(scores )
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>
accuracy = scores.mean()
print(accuracy )
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>
fare_count = Counter(train["Fare"])
 print(fare_count )
heights = [np.shape(matrix)[0] for matrix in matrices]
widths = [np.shape(matrix)[1] for matrix in matrices]<categorify>
process_fare(test, fare_cut_points, fare_label_names)
 print(test["Fare_categories"] )
def replace_values(a, d):
return np.array([d.get(i, -1)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<prepare_x_and_y>
train = create_dummies(train, "Fare_categories")
 train.head()
class ARCDataset(Dataset):
def __init__(self, X, y, stage="train"):
self.X = get_new_matrix(X)
 self.X = repeat_matrix(self.X)
 self.stage = stage
if self.stage == "train":
self.y = get_new_matrix(y)
 self.y = repeat_matrix(self.y)
 def __len__(self):
return SIZE
def __getitem__(self, idx):
inp = self.X[idx]
if self.stage == "train":
outp = self.y[idx]
if idx != 0:
rep = np.arange(10)
 orig = np.arange(10)
 np.random.shuffle(rep)
 dictionary = dict(zip(orig, rep))
inp = replace_values(inp, dictionary)
 if self.stage == "train":
outp, outp_probs_len, outp_matrix_dims = get_outp(outp, dictionary)
 if idx == 0:
if self.stage == "train":
outp, outp_probs_len, outp_matrix_dims = get_outp(outp, None, False)
 return inp, outp, outp_probs_len, outp_matrix_dims, self.y<choose_model_class>
test = create_dummies(test, "Fare_categories")
 test.head()
class BasicCNNModel(nn.Module):
def __init__(self, inp_dim=(10, 10), outp_dim=(10, 10)) :
super(BasicCNNModel, self ).__init__()
CONV_IN = 3
KERNEL_SIZE = 3
DENSE_IN = CONV_OUT_2
self.relu = nn.ReLU()
self.softmax = nn.Softmax(dim=1)
 self.dense_1 = nn.Linear(DENSE_IN, outp_dim[0]*outp_dim[1]*10)
 if inp_dim[0] < 5 or inp_dim[1] < 5:
KERNEL_SIZE = 1
self.conv2d_1 = nn.Conv2d(CONV_IN, CONV_OUT_1, kernel_size=KERNEL_SIZE)
 self.conv2d_2 = nn.Conv2d(CONV_OUT_1, CONV_OUT_2, kernel_size=KERNEL_SIZE)
 def forward(self, x, outp_dim):
x = torch.cat([x.unsqueeze(0)]*3)
 x = x.permute(( 1, 0, 2, 3)).float()
self.conv2d_1.in_features = x.shape[1]
conv_1_out = self.relu(self.conv2d_1(x))
self.conv2d_2.in_features = conv_1_out.shape[1]
conv_2_out = self.relu(self.conv2d_2(conv_1_out))
self.dense_1.out_features = outp_dim
feature_vector, _ = torch.max(conv_2_out, 2)
 feature_vector, _ = torch.max(feature_vector, 2)
 logit_outputs = self.dense_1(feature_vector)
 out = []
for idx in range(logit_outputs.shape[1]//10):
out.append(self.softmax(logit_outputs[:, idx*10:(idx+1)*10]))
return torch.cat(out, axis=1 )<categorify>
train["Cabin_type"] = train["Cabin"].str[0]
train["Cabin_type"] = train["Cabin_type"].fillna("Unknown")
 train["Cabin_type"].head()
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))<define_variables>
test["Cabin_type"] = test["Cabin"].str[0]
test["Cabin_type"] = test["Cabin_type"].fillna("Unknown")
 test["Cabin_type"].head()
idx = 0
start = time.time()
test_predictions = []
for X_train, y_train in zip(Xs_train, ys_train):
print("TASK " + str(idx + 1))
train_set = ARCDataset(X_train, y_train, stage="train")
 train_loader = DataLoader(train_set, batch_size=BATCH_SIZE, shuffle=True)
 inp_dim = np.array(X_train[0] ).shape
outp_dim = np.array(y_train[0] ).shape
network = BasicCNNModel(inp_dim, outp_dim ).cuda()
optimizer = Adam(network.parameters() , lr=0.01)
 for epoch in range(EPOCHS):
for train_batch in train_loader:
train_X, train_y, out_d, d, out = train_batch
train_preds = network.forward(train_X.cuda() , out_d.cuda())
 train_loss = nn.MSELoss()(train_preds, train_y.cuda())
 optimizer.zero_grad()
train_loss.backward()
optimizer.step()
end = time.time()
print("Train loss: " + str(np.round(train_loss.item() , 3)) + " " +\
"Total time: " + str(np.round(end - start, 1)) + " s" + "
")
 X_test = np.array([resize(flt(X), np.shape(X), inp_dim)for X in Xs_test[idx-1]])
 for X in X_test:
test_dim = np.array(T(X)).shape
test_preds = npy(network.forward(T(X ).unsqueeze(0 ).cuda() , out_d.cuda()))
test_preds = np.argmax(test_preds.reshape(( 10, *outp_dim)) , axis=0)
 test_predictions.append(itg(resize(test_preds, np.shape(test_preds),
tuple(itg(transform_dim(inp_dim,
outp_dim,
test_dim))))))
idx += 1<categorify>
titles = {
"Mr" : "Mr",
"Mme": "Mrs",
"Ms": "Mrs",
"Mrs" : "Mrs",
"Master" : "Master",
"Mlle": "Miss",
"Miss" : "Miss",
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Dr": "Officer",
"Rev": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Countess": "Royalty",
"Dona": "Royalty",
"Lady" : "Royalty"
}
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<load_from_csv>
extracted_titles = train["Name"].str.extract('([A-Za-z]+)\.',expand=False)
 train["Title"] = extracted_titles.map(titles)
 train["Title"].head()
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>
extracted_titles = test["Name"].str.extract('([A-Za-z]+)\.',expand=False)
 test["Title"] = extracted_titles.map(titles)
 test["Title"].head()
submission.to_csv("submission.csv", index=False )<save_to_csv>
for column in ["Title","Cabin_type"]:
train = create_dummies(train,column)
 test = create_dummies(test,column)
 train.head()
submission.to_csv("submission.csv", index=False )<import_modules>
predictor_columns = ['Age_categories_Missing', 'Age_categories_Infant',
'Age_categories_Child', 'Age_categories_Young Adult',
'Age_categories_Adult', 'Age_categories_Senior', 'Pclass_1', 'Pclass_3',
'Embarked_C', 'Embarked_Q', 'Embarked_S', 'SibSp_scaled',
'Parch_scaled', 'Fare_categories_0-12', 'Fare_categories_50-100',
'Fare_categories_100+', 'Title_Miss', 'Title_Mr', 'Title_Mrs',
'Title_Officer', 'Title_Royalty', 'Cabin_type_B', 'Cabin_type_C',
'Cabin_type_D', 'Cabin_type_E', 'Cabin_type_F', 'Cabin_type_G',
'Cabin_type_T', 'Cabin_type_Unknown']
all_X = train[predictor_columns]
all_y = train["Survived"]
lr = LogisticRegression()
selector = RFECV(lr, cv = 10)
 selector.fit(all_X, all_y)
 optimized_predictors = all_X.columns[selector.support_]
print(optimized_predictors )
import numpy as np
import pandas as pd
from tqdm import tqdm<load_pretrained>
all_X = train[optimized_predictors]
all_y = train["Survived"]
lr = LogisticRegression()
lr.fit(all_X, all_y)
 scores = cross_val_score(lr,all_X,all_y, cv=10)
 print(scores )
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<load_pretrained>
accuracy = scores.mean()
print(accuracy )
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()<train_on_grid>
predictions = lr.predict(test[optimized_predictors] )
<compute_test_metric><EOS> 
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": predictions
})
 submission.to_csv('submission1.csv', index=False )