Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<data_type_conversions>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 datasets = [train, test]
train.tail()
decks = train_data["Cabin"].str[0].unique() [1:]
train_data["Deck"] = train_data["Cabin"].str[0].fillna(random.choice(decks))
test_data["Deck"] = test_data["Cabin"].str[0].fillna(random.choice(decks))
train_data["Deck"] = train_data["Deck"].astype("category")
 test_data["Deck"] = test_data["Deck"].astype("category")
<prepare_x_and_y>
epic_titles = set(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'])
 female_titles = set(['Lady', 'Countess', 'Dona', 'Mlle', 'Ms', 'Mme'])
 deck = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "U": 8}
def mapFare(fare):
if fare <= 7.91:
return 0
elif fare > 7.91 and fare <= 14.454:
return 1
elif fare > 14.454 and fare <= 31:
return 2
elif fare > 31:
return 3
def mapAge(age):
if age <= 16:
return 0
elif age > 16 and age <= 32:
return 1
elif age > 32 and age <= 48:
return 2
elif age > 48 and age <= 64:
return 3
elif age > 64:
return 4
for dataset in datasets:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
dataset.drop([ 'SibSp', 'Parch'], axis=1, inplace=True)
 dataset['Title'] = dataset['Name'].apply(lambda x: x.split(',')[1].split('.')[0].strip())
 dataset['Title'] = dataset['Title'].apply(lambda x: x if x not in epic_titles else 'Epic')
 dataset['Sex'] = dataset['Sex'].fillna(dataset['Title'].apply(lambda x: 'female' if x in female_titles else 'male'))
dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Epic": 5}
dataset['Title'] = dataset['Title'].apply(lambda x: title_mapping[x] if x in title_mapping else 5)
 dataset['Sex'] = dataset['Sex'].map({"female": 0, "male": 1} ).astype(int)
 dataset['Age'] = dataset['Age'].fillna(dataset['Age'].mean())
 dataset['Age'] = dataset['Age'].apply(lambda x: mapAge(x))
dataset['Age'] = dataset['Age'].astype(int)
 dataset['Embarked'] = dataset['Embarked'].fillna('S')
 dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 dataset['Cabin'] = dataset['Cabin'].fillna("U0")
 dataset['Deck'] = dataset['Cabin'].map(lambda x: re.compile("([a-zA-Z]+)" ).search(x ).group())
 dataset['Deck'] = dataset['Deck'].map(deck)
 dataset['Deck'] = dataset['Deck'].fillna(0)
 dataset['Deck'] = dataset['Deck'].astype(int)
 dataset.drop(['Cabin'], axis=1, inplace=True)
 familysizeDummies = pd.get_dummies(dataset['FamilySize'], prefix='familysize')
 dataset = pd.concat([dataset,familysizeDummies],axis=1)
 dataset = dataset.drop(columns=['FamilySize'])
 print(dataset.columns)
y_train = train_data["Survived"]
features = ['age_fill','Pclass', 'Sex','Deck','Fare_new','Embarked_fill','SibSp','Parch', 'relatives_code']
X_test = test_data[features].copy()
X_train = train_data[features].copy() 
<feature_engineering>
train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).agg(['mean', 'count', 'sum'] )
X_train["Sex"] = X_train["Sex"].cat.codes
X_test["Sex"] = X_test["Sex"].cat.codes
X_train["Embarked_fill"] = X_train["Embarked_fill"].cat.codes
X_test["Embarked_fill"] = X_test["Embarked_fill"].cat.codes
X_train["Deck"] = X_train["Deck"].cat.codes
X_test["Deck"] = X_test["Deck"].cat.codes 
<train_model>
y = train[['Survived']]
x = train.drop(columns=['Name', 'Ticket', 'Survived', 'PassengerId'])
 x.columns
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 predictions = model.predict(X_test)
<save_to_csv>
train_x, test_x, train_y, test_y = train_test_split(x, np.ravel(y))
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
logreg_model = LogisticRegression()
logreg_model.fit(train_x, train_y)
 preds = logreg_model.predict(test_x)
 acc = accuracy_score(preds, test_y)
 print(f'Accuracy: {acc}')
import numpy as np
import pandas as pd
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
from scipy import ndimage
import tensorflow as tf
from tensorflow import keras
from keras.layers import Input, LSTM, Embedding, Dense
from keras.models import Model
from tqdm import tqdm
import itertools<load_pretrained>
scaler = MinMaxScaler(feature_range=[0, 1])
 train_x_scaled = scaler.fit_transform(train_x.iloc[1:, 0:8])
 test_x_scaled = scaler.fit_transform(test_x.iloc[1:, 0:len(train_x.columns)])
 train_x_centered = train_x - np.mean(train_x, axis = 0)
 test_x_centered = train_x - np.mean(test_x, axis = 0)
 train_x_centered.describe()
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>
for i in range(1, len(train_x.columns)) :
pca = PCA(n_components=i)
 pca.fit(train_x)
 train_x_pca = pca.transform(train_x)
 test_x_pca = pca.transform(test_x)
 logreg_model.fit(train_x_pca, train_y)
 preds_pca = logreg_model.predict(test_x_pca)
 acc_pca = accuracy_score(preds_pca, test_y)
 print(f'Accuracy with {i} principal components: {acc_pca}' )
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()<set_options>

 warnings.simplefilter(action='ignore', category=FutureWarning)
 def plurality_value(parent_examples):
r = 0
return parent_examples['Survived'].value_counts().index[r]
def allValuesEqual(examples):
return len(examples['Survived'].value_counts())== 1
def partition(examples, column, value):

 true_rows, false_rows = examples.copy() , examples.copy()
for index, row in examples.iterrows() :
if row[column] == value:
false_rows = false_rows.drop([index])
 else:
true_rows = true_rows.drop([index])
 return true_rows, false_rows
def get_entropy(examples):
number_of_ones = 0
for index, row in examples.iterrows() :
if row['Survived'] == 1:
number_of_ones += 1
q = number_of_ones/len(examples)if len(examples)!= 0 else 0
return -(q*math.log2(q)+(1-q)*math.log2(( 1-q)))if(q != 0 and q!= 1)else 0
def get_remainder(one_rows, two_rows, entropy1, entropy2):
total = len(one_rows)+ len(two_rows)
 p, n = len(one_rows), len(two_rows)
 return(p/total)*entropy1 +(n/total)*entropy2
def find_best_split(examples, attributes, current_entropy):

 best_gain = 0
best_attribute = attributes[0]
info_gains = []
for attribute in attributes:
ones, twoes = partition(examples, attribute, 0)
 entropy1, entropy2 = get_entropy(ones), get_entropy(twoes)
 remainder = get_remainder(ones, twoes, entropy1, entropy2)
 info_gain = current_entropy - remainder
info_gains.append(info_gain)
 if info_gain >= best_gain:
best_gain, best_attribute = info_gain, attribute
return(best_attribute, best_gain)if best_gain > 0 else(random.choice(attributes), best_gain)
 def decision_tree_learning(examples, attributes, parent_examples, info_gain):

 if len(examples)== 0:
return plurality_value(parent_examples)
 elif allValuesEqual(examples):
return examples.iloc[0]['Survived']
elif len(attributes)== 0:
return plurality_value(examples)
 elif info_gain < 0.005:
return plurality_value(examples)
 current_entropy = get_entropy(examples)
 best_attribute, info_gain = find_best_split(examples, attributes, current_entropy)[0], find_best_split(examples, attributes, current_entropy)[1]
tree = Tree(best_attribute)
 attr_copy = attributes.copy()
attr_copy = attr_copy.drop(labels=best_attribute)
 for value in train[best_attribute].unique() :
next_examples = examples.loc[examples[best_attribute] == value]
subtree = decision_tree_learning(next_examples, attr_copy, examples, info_gain)
 tree.add_branch(value, subtree)
 return tree
class Tree:
def __init__(self, root, branches=None):
self.root = root
if branches == None:
branches = {}
self.branches = branches
def add_branch(self, label, branch):
self.branches[label] = branch
def predict(tree, example):
value = example[tree.root]
if isinstance(tree.branches[value], Tree):
return predict(tree.branches[value], example)
 else:
return tree.branches[value]
def print_tree(tree, value="", level=0):
print("\t" * level + str(value), end = " -> ")
 attribute = tree.root if isinstance(tree, Tree)else str(tree)
 print(attribute, end = "
")
 if isinstance(tree, Tree):
for label, subtree in tree.branches.items() :
print_tree(subtree, label, level + 1)
 def main() :
print("--------------------------------------------")
 train_new = train.copy()
train_new = train_new.drop(columns=['Name', 'Ticket', 'PassengerId'])
 test_new = test.copy()
y = train_new['Survived']
train_x, test_x, train_y, test_y = train_test_split(train_new, y, test_size=0.25)
 train_x_attributes = train_x.drop(columns=['Survived'] ).columns
tree = decision_tree_learning(train_x, train_x_attributes, train_x, 1)
 print_tree(tree)
 predictions = []
for index, example in test_x.iterrows() :
try:
prediction = predict(tree, example)
 except:
prediction = 0
print("her")
 predictions.append(prediction)
 acc = accuracy_score(test_y, predictions)
 print("Accuracy with my greedy Importance Decision Tree: ", acc)
 real_tree = decision_tree_learning(train, train_x_attributes, train, 1)
 predictions = []
for index, example in test.iterrows() :
try:
prediction = predict(real_tree, example)
 except:
prediction = 0
print("her")
 predictions.append(prediction)
 pred_df = test[['PassengerId']].copy()
pred_df['Survived'] = predictions
pred_df.to_csv('submission_DTC.csv', index=False)
class fund_unit:
def __init__(self,colour,position,ambient = -1):
self.colour = colour
self.position = position
l1 = [x[0] for x in position]
l2 = [x[1] for x in position]
a = min(l1)
 corners = get_corners(position)
 self.top_right = corners['top_right']
self.top_left = corners['top_left']
self.bottom_left = corners['bottom_left']
self.bottom_right = corners['bottom_right']
self.colour = colour
self.ambient = ambient
self.right = corners['right']
self.left = corners['left']
self.bottom = corners['bottom']
self.top = corners['top']<normalization>
def predict_test(model, name):
test.fillna(0, inplace=True)
 model.fit(x, np.ravel(y))
test_data = test.drop(columns=['Name', 'Ticket', 'PassengerId'])
 preds = model.predict(test_data)
 pred_df = test[['PassengerId']].copy()
pred_df['Survived'] = preds
pred_df.to_csv(f'submission_{name}.csv', index=False )
def create(colour,position):
unit = fund_unit(colour,position)
 return(unit )<normalization>
names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process",
"Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
"Naive Bayes", "QDA"]
classifiers = [
KNeighborsClassifier(3),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
GaussianProcessClassifier(1.0 * RBF(1.0)) ,
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=800),
MLPClassifier(alpha=1, max_iter=500),
AdaBoostClassifier() ,
GaussianNB() ,
QuadraticDiscriminantAnalysis()
]
for i, classifier in enumerate(classifiers):
classifier.fit(train_x, train_y)
 preds = classifier.predict(test_x)
 print(f"Predicting with {names[i]}, accuracy: {accuracy_score(preds, test_y)}")
 predict_test(classifier, names[i] )
def min_steps(unit1,unit2,include_end = 0):
l = []
for x in unit1.position:
for y in unit2.position:
a = count_steps(x[0],y[0],include_end)
 b = count_steps(x[1],y[1],include_end)
 l.append(( a,b))
l.sort(key = lambda x : abs(x[0])+ abs(x[1]))
t = l[0]
return(np.array([t[0],t[1]]))<sort_values>
def max_steps(unit1,unit2,include_end = 0):
l = []
for x in unit1.position:
for y in unit2.position:
a = count_steps(x[0],y[0],include_end)
 b = count_steps(x[1],y[1],include_end)
 l.append(( a,b))
l.sort(key = lambda x : abs(x[0])+ abs(x[1]))
t = l[-1]
return(np.array([t[0],t[1]]))<define_variables>
def count_steps(a,b,include_end = 0):
steps = 0
if include_end == 0:
if a < b:
steps = b-a-1
if a > b:
steps = b-a+1
else:
steps = b-a
return(steps )<sort_values>
<train_model><EOS>