Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
import numpy as np
import pandas as pd
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
import re
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_absolute_error
from sklearn.ensemble import RandomForestClassifier
from catboost import CatBoostClassifier
from sklearn.preprocessing import OneHotEncoder
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from sklearn.linear_model import LogisticRegression
%matplotlib inline 
<import_modules>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.info()
torch.__version__<import_modules>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.info()
hui = test_data['PassengerId']
from torch.autograd import Variable<set_options>
len(train_data.loc[train_data['Survived']==1])/len(train_data )
use_gpu = torch.cuda.is_available()
use_gpu<load_from_zip>
train_names = train_data[['Name','Age']].dropna().reset_index()
train_names['Name'] = train_names['Name'].str.lower()
new_date = []
for i in train_names['Name']:
d = [" ".join(re.sub(r"[^a-z ]", ' ', str(i)).split())]
new_date.append(d)
 df = pd.DataFrame(new_date, columns = ['Name'])
 df['Age'] = train_names['Age']
features_train = df['Name']
target_train = df['Age']
!tar -zxvf.. /input/cifar10-python/cifar-10-python.tar.gz<set_options>
count_tf_idf = TfidfVectorizer()
tf_idf = count_tf_idf.fit_transform(features_train)
 count_tf_idf_ts = TfidfVectorizer()
tf_id_ts = count_tf_idf.transform(features_train )
!ls .<load_pretrained>
clf_t = RandomForestRegressor(random_state=42,
criterion='mae' ).fit(count_tf_idf.transform(features_train), target_train )
transform = transforms.Compose(
 [transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5)) ])
 trainset = torchvision.datasets.CIFAR10(root='.', train=True,
download=False, transform=transform)
 trainloader = torch.utils.data.DataLoader(trainset, batch_size=32,
shuffle=True, num_workers=2)
 testset = torchvision.datasets.CIFAR10(root='.', train=False,
download=False, transform=transform)
 testloader = torch.utils.data.DataLoader(testset, batch_size=32,
shuffle=False, num_workers=2)
 classes =('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck' )<not_enough_vertices>
ind = train_data.loc[train_data['Age'].isnull() ==True].index
ddf = train_data.loc[ind, ['Name']]
ddf['Name'] = ddf['Name'].str.lower()
new_date = []
for i in ddf['Name']:
d = [" ".join(re.sub(r"[^a-z ]", ' ', str(i)).split())]
new_date.append(d)
 df = pd.DataFrame(new_date, columns = ['Name'])
 df.index=ind
features_train = df['Name']
def imshow(img):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg,(1, 2, 0)))
 dataiter = iter(trainloader)
 images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(32)) )<choose_model_class>
d = round(pd.DataFrame(clf_t.predict(count_tf_idf.transform(features_train)) ,columns=['new_adge']))
d.index=ind
d
class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
 self.pool = nn.MaxPool2d(2, 2)
 self.conv2 = nn.Conv2d(6, 16, 5)
 self.fc1 = nn.Linear(16 * 5 * 5, 120)
 self.fc2 = nn.Linear(120, 84)
 self.fc3 = nn.Linear(84, 10)
 def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
 x = self.pool(F.relu(self.conv2(x)))
 x = x.view(-1, 16 * 5 * 5)
 x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
 return x
net = Net()
if torch.cuda.is_available() :
net.cuda()<choose_model_class>
for i in d.index:
for a in train_data.index:
if i == a:
train_data.loc[i,['Age']] = int(d.loc[i,['new_adge']])
 train_data['Age'] = train_data['Age'].astype('int')
 train_data.info()
criterion = nn.CrossEntropyLoss()
if use_gpu:
criterion = criterion.cuda()
optimizer = optim.SGD(net.parameters() , lr=0.001, momentum=0.9 )<train_model>
ind = test_data.loc[test_data['Age'].isnull() ==True].index
ddf = test_data.loc[ind, ['Name']]
ddf['Name'] = ddf['Name'].str.lower()
new_date = []
for i in ddf['Name']:
d = [" ".join(re.sub(r"[^a-z ]", ' ', str(i)).split())]
new_date.append(d)
 df = pd.DataFrame(new_date, columns = ['Name'])
 df.index=ind
features_test = df['Name']
for epoch in tqdm_notebook(range(10)) :
running_loss = 0.0
for i, data in tqdm_notebook(enumerate(trainloader, 0)) :
inputs, labels = data
if torch.cuda.is_available() :
inputs, labels = Variable(inputs ).cuda() , Variable(labels ).cuda()
optimizer.zero_grad()
outputs = net(inputs)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
running_loss += loss.data[0]
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' % 
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training' )<not_enough_vertices>
d = round(pd.DataFrame(clf_t.predict(count_tf_idf.transform(features_test)) ,columns=['new_adge']))
d.index=ind
for i in d.index:
for a in test_data.index:
if i == a:
test_data.loc[i,['Age']] = int(d.loc[i,['new_adge']])
 test_data['Age'] = test_data['Age'].astype('int')
 test_data.info()
dataiter = iter(testloader)
 images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(32)) )<define_variables>
train_data.loc[5,['Age']]
outputs = net(Variable(images ).cuda())if use_gpu else net(Variable(images))<something_strange>
train_data['Embarked'].value_counts()
_, predicted = torch.max(outputs.data, 1)
 print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
for j in range(32)) )<define_variables>
train_data['Embarked'] = train_data['Embarked'].fillna('S')
 train_data.info()
all_pred = np.empty(( 0, 10), float )<predict_on_test>
train_data['Sex'] = pd.get_dummies(train_data['Sex'])
 train_data
for data in tqdm_notebook(testloader):
images, _ = data
if use_gpu:
images = images.cuda()
outputs = net(Variable(images))
curr_pred = F.softmax(outputs ).data.cpu().numpy()
all_pred = np.vstack([all_pred, curr_pred] )<save_to_csv>
train_data['Sex'] = train_data['Sex'].astype('int')
 train_data.info()
pd.DataFrame(all_pred, columns=classes ).to_csv('baseline.csv', index_label='id' )<set_options>
test_data['Sex'] = pd.get_dummies(test_data['Sex'])
 test_data['Sex'] = test_data['Sex'].astype('int')
 test_data
%matplotlib inline<load_from_csv>
one_hot = pd.get_dummies(train_data['Embarked'],drop_first=True)
 train_data = train_data.drop('Embarked',axis = 1)
 train_data = train_data.join(one_hot)
 train_data
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print('Train info:')
 train.info()
print('
Test info:')
 test.info()<feature_engineering>
one_hot = pd.get_dummies(test_data['Embarked'],drop_first=True)
 test_data = test_data.drop('Embarked',axis = 1)
 test_data = test_data.join(one_hot)
 test_data
train['mind'] = train['type'].str[0]
train['energy'] = train['type'].str[1]
train['nature'] = train['type'].str[2]
train['tactics'] = train['type'].str[3]<feature_engineering>
len(train_data.loc[train_data['Ticket'].duplicated() ==True][['Ticket','Cabin']].sort_values('Ticket'))
train['mind'] = train['mind'].apply(lambda x: 0 if x == 'I' else 1)
 train['energy'] = train['energy'].apply(lambda x: 0 if x == 'S' else 1)
 train['nature'] = train['nature'].apply(lambda x: 0 if x == 'F' else 1)
 train['tactics'] = train['tactics'].apply(lambda x: 0 if x == 'P' else 1)
 train.head()<feature_engineering>
train_data['Name'] = train_data['Name'].str.lower()
def word_replace(df):

 df['posts'] = df['posts'].str.replace(r'http.?://[^\s]+[\s]?', 'url ')
 df['posts'] = df['posts'].str.replace(r'n't', ' not')
 df['posts'] = df['posts'].str.replace(r''s', ' is')
 df['posts'] = df['posts'].str.replace(r''m', ' am')
 df['posts'] = df['posts'].str.replace(r''re', ' are')
 df['posts'] = df['posts'].str.replace(r''ve', ' have')
 df['posts'] = df['posts'].str.replace(r''ll', ' will')
 df['posts'] = df['posts'].str.replace(r''d', ' would')
 df['posts'] = df['posts'].str.replace(r'
df['posts'] = df['posts'].str.replace(r"[',.():|-]", " ")
 df['posts'] = df['posts'].str.lower()
return df<categorify>
train_clean = word_replace(train.copy())
 test_clean = word_replace(test.copy() )<import_modules>
<feature_engineering>
test_data['Name'] = test_data['Name'].str.lower()
get_tokens = TweetTokenizer()
get_lemmas = WordNetLemmatizer()
def tokenize_lemmatize(df):

 df['posts'] = df.apply(lambda row: [get_lemmas.lemmatize(w)for w in get_tokens.tokenize(row['posts'])], axis=1)
 return df 
<categorify>
train_data.loc[(train_data['Ticket']=='S.O.C.14879')]
train_clean = tokenize_lemmatize(train_clean.copy())
 test_clean = tokenize_lemmatize(test_clean.copy() )<categorify>
train_data.loc[train_data['Cabin'].isnull() ==False]
emojies = set(emoji.UNICODE_EMOJI.keys())
 def swap_emoji(word):

 if word in emojies:
return 'emoji'
return word
def emoji_convert(df):

 df['posts'] = df['posts'].apply(lambda row: [swap_emoji(word)for word in row])
 return df 
<categorify>
train_clean = emoji_convert(train_clean.copy())
 test_clean = emoji_convert(test_clean.copy() )<categorify>
stop_words = stopwords.words('english')
 stop_words = stop_words + ['could', 'would']
stop_words = [w for w in stop_words if w not in ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves']]
def remove_words(df):

 df['posts'] = df['posts'].apply(lambda row: [word for word in row if word not in stop_words])
 return df<drop_column>
train_clean = remove_words(train_clean.copy())
 test_clean = remove_words(test_clean.copy() )<count_values>
E = train_clean['mind'][train_clean['mind'] == 1].count() / train_clean['mind'].count()
print('E:I = {} : {}'.format(round(E, 2), 1-round(E, 2)))
 N = train_clean['energy'][train_clean['energy'] == 1].count() / train_clean['energy'].count()
print('N:S = {} : {}'.format(round(N, 2), 1-round(N, 2)))
 T = train_clean['nature'][train_clean['nature'] == 1].count() / train_clean['nature'].count()
print('T:F = {} : {}'.format(round(T, 2), 1-round(T, 2)))
 J = train_clean['tactics'][train_clean['tactics'] == 1].count() / train_clean['tactics'].count()
print('J:P = {} : {}'.format(round(J, 2), 1-round(J, 2)) )<train_model>
train_data.loc[[128,699,715,75]]
tt = TfidfVectorizer(preprocessor=list, tokenizer=list, ngram_range=(1,2), min_df=2, smooth_idf=False)
 def vectorise(train_set, test_set):

 tt.fit(train_set)
 train_vect = tt.transform(train_set)
 test_vect = tt.transform(test_set)
 return train_vect, test_vect 
<import_modules>
train_data = train_data.drop('PassengerId',axis=1)
 dt = train_data
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import log_loss, confusion_matrix, accuracy_score
from sklearn.linear_model import LogisticRegression<prepare_x_and_y>
X_train_vect, X_test_vect = vectorise(train_clean['posts'], test_clean['posts'])
 X_train_vect.shape<train_on_grid>
train_data = train_data.drop('Cabin',axis=1)
 test_data = test_data.drop('Cabin',axis=1 )
cm = LogisticRegression(penalty='l1', solver='liblinear', random_state=11)
 params = {'C': [0.1, 0.5, 1, 5, 10, 50, 100]}
grid = GridSearchCV(cm, param_grid=params, scoring='neg_log_loss', n_jobs=-1, cv=4)
 categories = ['mind', 'energy', 'nature', 'tactics']
for cat in categories:
grid.fit(X_train_vect, train_clean[cat])
 print(cat + ':')
 print('Best score: ', -grid.best_score_)
 print('Best paramaters: ', grid.best_params_)
 print('
' )<split>
train, test = train_test_split(train_data, test_size=0.33, random_state=42)
 features_train = train.drop(['Survived'],axis=1)
 target_train = train['Survived']
features_test = test.drop(['Survived'],axis=1)
 target_test = test['Survived']
X_train, X_test, y_train, y_test = train_test_split(train_clean['posts'], train_clean[['mind', 'energy', 'nature', 'tactics']], random_state=11)
 X_train_vect, X_test_vect = vectorise(X_train, X_test)
 X_train_vect.shape<train_model>
model = CatBoostClassifier(iterations=200,
depth=9,learning_rate=0.05,l2_leaf_reg=10,random_seed=42,
loss_function='Logloss',grow_policy='Lossguide',
max_leaves=39,
verbose=True,eval_metric='Accuracy',nan_mode='Min',cat_features=['Pclass','Sex','Q','S'])
 model.fit(features_train, target_train,text_features=['Ticket','Name'],
plot=True,eval_set=(features_test,target_test),verbose=False)
 print(accuracy_score(model.predict(features_train),target_train))
print(accuracy_score(model.predict(features_test),target_test))
def build_model(X_train_vect_f, X_test_vect_f, y_train_f, y_test_f, model):

 model.fit(X_train_vect_f, y_train_f)
 train_pred = model.predict(X_train_vect_f)
 test_pred = model.predict(X_test_vect_f)
 train_proba = model.predict_proba(X_train_vect_f)
 test_proba = model.predict_proba(X_test_vect_f)
 train_loss = log_loss(y_train_f, train_proba, eps=1e-15)
 test_loss = log_loss(y_test_f, test_proba, eps=1e-15)
 test_accuracy = accuracy_score(y_test_f, test_pred)
 test_matrix = confusion_matrix(y_test_f, test_pred)
 return train_pred, test_pred, train_loss, test_loss, test_accuracy, test_matrix<define_variables>
test_data = test_data.drop('PassengerId',axis=1)
 dt = test_data
test_data
ll_scores = []
ac_scores = []<train_model>
submission=pd.DataFrame(model.predict(test_data),columns=['Survived'])
 submission['PassengerId'] = hui
submission.Survived = submission.Survived.astype(int)
 filename = 'Titanic Predictions1.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )