Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
token2int = {x:i for i, x in enumerate('().ACGUBEHIMSX')}
target_cols = ['reactivity', 'deg_Mg_pH10', 'deg_pH10', 'deg_Mg_50C', 'deg_50C']<load_pretrained>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv")
 def append_train_test(train,test):
train["IsTrain"]=1
test["IsTrain"]=0
df=train.append(test)
 return df;
full=append_train_test(train,test)
 train.info()
print("------------------------")
 test.info()
print("------------------------")
 full.info()
def read_bpps_sum(df):
bpps_arr = []
for mol_id in df.id.to_list() :
bpps_arr.append(np.load(f".. /input/stanford-covid-vaccine/bpps/{mol_id}.npy" ).sum(axis=1))
return bpps_arr
def read_bpps_max(df):
bpps_arr = []
for mol_id in df.id.to_list() :
bpps_arr.append(np.load(f".. /input/stanford-covid-vaccine/bpps/{mol_id}.npy" ).max(axis=1))
return bpps_arr
def read_bpps_nb(df):
bpps_nb_mean = 0.077522
bpps_nb_std = 0.08914
bpps_arr = []
for mol_id in df.id.to_list() :
bpps = np.load(f".. /input/stanford-covid-vaccine/bpps/{mol_id}.npy")
 bpps_nb =(bpps > 0 ).sum(axis=0)/ bpps.shape[0]
bpps_nb =(bpps_nb - bpps_nb_mean)/ bpps_nb_std
bpps_arr.append(bpps_nb)
 return bpps_arr
os.chdir("/kaggle/working/")
 train_data['bpps_sum'] = read_bpps_sum(train_data)
 test_data['bpps_sum'] = read_bpps_sum(test_data)
 train_data['bpps_max'] = read_bpps_max(train_data)
 test_data['bpps_max'] = read_bpps_max(test_data)
 train_data['bpps_nb'] = read_bpps_nb(train_data)
 test_data['bpps_nb'] = read_bpps_nb(test_data)
 train_data.head()<create_dataframe>
full.loc[(full["Title"].isin(["Rev","Dr","Col","Capt","Major"])) &(full["Sex"]!="male")]
def get_bases(data):
bases = []
for j in range(len(data)) :
counts = dict(count(data.iloc[j]['sequence']))
bases.append((
counts['A'] / 107,
counts['G'] / 107,
counts['C'] / 107,
counts['U'] / 107 
))
bases = pd.DataFrame(bases, columns=['A_percent', 'G_percent', 'C_percent', 'U_percent'])
 return bases<create_dataframe>
full.loc[(full["Title"].isin(["Rev","Capt","Major","Col","Jonkheer","Don","Sir","Dr"])) &(full["Sex"]=="male"),"Title"] = "Mr"
full.loc[(full["Title"].isin(["Countess","Lady","Dona","Mme"])) ,"Title"]="Mrs"
full.loc[(full["Title"].isin(["Mlle","Ms","Dr"])) &(full["Sex"]=="female"),"Title"]="Miss"
full["Title"].value_counts(normalize=True )
def get_pairs_rate(data):
pairs_rate = []
for j in range(len(data)) :
res = dict(count(data.iloc[j]['structure']))
pairs_rate.append(res['('] / 53.5)
 pairs_rate = pd.DataFrame(pairs_rate, columns=['pairs_rate'])
 return pairs_rate<define_variables>
full.isnull().sum()
def get_pairs(data):
pairs = []
all_partners = []
for j in range(len(data)) :
partners = [-1 for i in range(130)]
pairs_dict = {}
queue = []
for i in range(0, len(data.iloc[j]['structure'])) :
if data.iloc[j]['structure'][i] == '(':
queue.append(i)
 if data.iloc[j]['structure'][i] == ')':
first = queue.pop()
try:
pairs_dict[(data.iloc[j]['sequence'][first], data.iloc[j]['sequence'][i])] += 1
except:
pairs_dict[(data.iloc[j]['sequence'][first], data.iloc[j]['sequence'][i])] = 1
partners[first] = i
partners[i] = first
all_partners.append(partners)
 pairs_num = 0
pairs_unique = [('U', 'G'),('C', 'G'),('U', 'A'),('G', 'C'),('A', 'U'),('G', 'U')]
for item in pairs_dict:
pairs_num += pairs_dict[item]
add_tuple = list()
for item in pairs_unique:
try:
add_tuple.append(pairs_dict[item]/pairs_num)
 except:
add_tuple.append(0)
 pairs.append(add_tuple)
 pairs = pd.DataFrame(pairs, columns=['U-G', 'C-G', 'U-A', 'G-C', 'A-U', 'G-U'])
 return pairs<create_dataframe>
full[full.Fare.isnull() ]
def get_loops(data):
loops = []
for j in range(len(data)) :
counts = dict(count(data.iloc[j]['predicted_loop_type']))
available = ['E', 'S', 'H', 'B', 'X', 'I', 'M']
row = []
for item in available:
try:
row.append(counts[item] / 107)
 except:
row.append(0)
 loops.append(row)
 loops = pd.DataFrame(loops, columns=available)
 return loops<categorify>
full.loc[(full.Fare.isnull()),"Fare"]=8.05
def get_structure_adj(train):
Ss = []
for i in tqdm(range(len(train))):
seq_length = train["seq_length"].iloc[i]
structure = train["structure"].iloc[i]
sequence = train["sequence"].iloc[i]
cue = []
a_structures = { 
("A", "U"): np.zeros([seq_length, seq_length]), 
("C", "G"): np.zeros([seq_length, seq_length]), 
("U", "G"): np.zeros([seq_length, seq_length]), 
("U", "A"): np.zeros([seq_length, seq_length]), 
("G", "C"): np.zeros([seq_length, seq_length]), 
("G", "U"): np.zeros([seq_length, seq_length]),
}
a_structure = np.zeros([seq_length, seq_length])
 for j in range(seq_length):
if structure[j] == "(":
cue.append(j)
 elif structure[j] == ")":
start = cue.pop()
a_structures[(sequence[start], sequence[j])][start, j] = 1
a_structures[(sequence[j], sequence[start])][j, start] = 1
a_strc = np.stack([a for a in a_structures.values() ], axis = 2)
 a_strc = np.sum(a_strc, axis = 2, keepdims = True)
 Ss.append(a_strc)
 Ss = np.array(Ss)
 print(Ss.shape)
 return Ss<load_pretrained>
full[full.Embarked.isnull() ]
As = []
data = train_data[train_data['signal_to_noise'] > 1].copy()
for id in tqdm(data['id']):
a = np.load(f"/kaggle/input/stanford-covid-vaccine/bpps/{id}.npy")
 As.append(a)
 As = np.array(As )<compute_test_metric>
full.loc[(full.Embarked.isnull()),"Embarked"]="C"
def get_distance_matrix(As):
idx = np.arange(As.shape[1])
 Ds = []
for i in range(len(idx)) :
d = np.abs(idx[i] - idx)
 Ds.append(d)
 Ds = np.array(Ds)+ 1
Ds = 1/Ds
Ds = Ds[None, :,:]
Ds = np.repeat(Ds, len(As), axis = 0)
 Dss = []
for i in [1, 2, 4]:
Dss.append(Ds ** i)
 Ds = np.stack(Dss, axis = 3)
 print(Ds.shape)
 return Ds<categorify>
ImpAge=pd.DataFrame({'median' : data.groupby([ "Title", "Pclass"] ).Age.median() } ).reset_index()
ImpAge.head(n=20 )
def preprocess_inputs(df, cols=['sequence', 'structure', 'predicted_loop_type'], seq_length = 107, flag = 'train'):
base_fea = np.transpose(
 np.array(
 df[cols] 
.applymap(lambda seq: [token2int[x] for x in seq])
.values 
.tolist() 
), 
(0, 2, 1)
)
 bpps_sum_fea = np.array(df['bpps_sum'].to_list())[:,:,np.newaxis]
bpps_max_fea = np.array(df['bpps_max'].to_list())[:,:,np.newaxis]
bpps_nb_fea = np.array(df['bpps_nb'].to_list())[:,:,np.newaxis]
Ss = get_structure_adj(df)
 Ss = Ss.sum(axis = 1)
 if flag == 'train':
Ds = get_distance_matrix(As)
 elif flag == 'test_private':
Ds = get_distance_matrix(As_private)
 elif flag == 'test_public':
Ds = get_distance_matrix(As_public)
 Ds = Ds.sum(axis = 1)
 data = np.concatenate([base_fea,bpps_sum_fea,bpps_max_fea,bpps_nb_fea, Ss, Ds], 2)
 array_data =(np.reshape(( [([list(df['A_percent'])[0]] * seq_length)]),(1,seq_length,1)))
 for i in range(1,len(df)) :
array_data_i =(np.reshape(( [([list(df['A_percent'])[i]] * seq_length)]),(1,seq_length,1)))
 array_data = np.concatenate([array_data,array_data_i], axis = 0)
 data = np.concatenate([data, array_data], 2)
 for col in ['G_percent','C_percent','U_percent','U-G','C-G','U-A','G-C','A-U','G-U',
'E','S','H','B','X','I','M','pairs_rate']:
array_data =(np.reshape(( [([list(df[col])[0]] * seq_length)]),(1,seq_length,1)))
 for i in range(1,len(df)) :
arraydom_data_i =(np.reshape(( [([list(df[col])[i]] * seq_length)]),(1,seq_length,1)))
 array_data = np.concatenate([array_data,array_data_i], axis = 0)
 data = np.concatenate([data, array_data], 2)
 return data<categorify>
def imputeAges(df):
classes=[1,2,3]
titles=["Mr","Mrs","Miss","Master"]
for title in titles:
for pclass in classes:
x=ImpAge[(( ImpAge.Title==title)&(ImpAge.Pclass==pclass)) ]["median"].values[0]
df.loc[(( df.Title==title)&(df.Pclass==pclass)&(df.Age.isnull())) ,"Age"]=x
return df
imputeAges(full)
 full.isnull().sum()
bases = get_bases(train_data)
 pairs = get_pairs(train_data)
 loops = get_loops(train_data)
 pairs_rate = get_pairs_rate(train_data)
 train_data = pd.concat([train_data, bases, pairs, loops, pairs_rate], axis=1)
 bases = get_bases(test_data)
 pairs = get_pairs(test_data)
 loops = get_loops(test_data)
 pairs_rate = get_pairs_rate(test_data)
 test_data = pd.concat([test_data, bases, pairs, loops, pairs_rate], axis=1 )<define_variables>
features=["Age","Embarked","Fare","Parch","Pclass","Sex","SibSp","Title","FamilySize","FamilySizeBand"]
target="Survived"
full[features].head()
train_inputs = preprocess_inputs(train_data.loc[train_data['signal_to_noise'] > 1], seq_length = 107, flag = 'train')
 train_labels = np.array(train_data.loc[train_data['signal_to_noise'] > 1][target_cols].values.tolist() ).transpose(( 0, 2, 1))<compute_test_metric>
clf = RandomForestClassifier(n_jobs=2, random_state=0)
 clf = clf.fit(train_features, train_target)
 train_preds=clf.predict(train_features)
 print(clf)
 for i in range(0,10):
print(features[i]," ",clf.feature_importances_[i] )
def root_mean_squared_error(y_true, y_pred):
return tf.sqrt(mean_squared_error(y_true, y_pred))
def MCRMSE(y_true, y_pred):
colwise_mse = tf.reduce_mean(tf.square(y_true - y_pred), axis=1)
 return tf.reduce_mean(tf.sqrt(colwise_mse), axis=1)
 def lstm_layer(hidden_dim, dropout):
return tf.keras.layers.Bidirectional(
 tf.keras.layers.LSTM(hidden_dim,
dropout=dropout,
return_sequences=True,
kernel_initializer = 'orthogonal'))
def gru_layer(hidden_dim, dropout):
return tf.keras.layers.Bidirectional(
 tf.keras.layers.GRU(hidden_dim,
dropout=dropout,
return_sequences=True,
kernel_initializer='orthogonal'))
def build_model(n_layers = 2, seq_len = 107, num_features = 28, embed_dim = 200, sp_dropout = 0.2, hidden_dim = 512, dropout = 0.5, pred_len = 68, gru_flag = False):
inputs = tf.keras.layers.Input(shape=(seq_len, num_features))
categorical_feats = inputs[:, :, :3]
numerical_feats = inputs[:, :, 3:10]
overall_gene_feats = inputs[:, :, 10:]
embed = tf.keras.layers.Embedding(input_dim=len(token2int), output_dim=embed_dim )(categorical_feats)
 reshaped = tf.reshape(embed, shape=(-1, embed.shape[1], embed.shape[2] * embed.shape[3]))
reshaped_1 = tf.keras.layers.concatenate([reshaped, numerical_feats], axis=2)
 normalized_layer_1 = tf.keras.layers.BatchNormalization()(reshaped_1)
 spatial_dropout = tf.keras.layers.SpatialDropout1D(sp_dropout )(normalized_layer_1)
 if gru_flag:
for x in range(n_layers):
normalized_layer_1 = gru_layer(hidden_dim, dropout )(normalized_layer_1)
 normalized_layer_2 = tf.keras.layers.BatchNormalization()(normalized_layer_1)
 else:
for x in range(n_layers):
normalized_layer_1 = lstm_layer(hidden_dim, dropout )(normalized_layer_1)
 normalized_layer_2 = tf.keras.layers.BatchNormalization()(normalized_layer_1)
 concat_layer = tf.keras.layers.concatenate([normalized_layer_2, overall_gene_feats], axis=2)
 dense_layer_1 = tf.keras.layers.Dense(100, activation = 'linear' )(concat_layer)
 normalized_layer_3 = tf.keras.layers.BatchNormalization()(dense_layer_1)
 dropout_layer_1 = tf.keras.layers.SpatialDropout1D(sp_dropout )(normalized_layer_3)
 dense_layer_2 = tf.keras.layers.Dense(100, activation = 'linear' )(dropout_layer_1)
 normalized_layer_4 = tf.keras.layers.BatchNormalization()(dense_layer_2)
 dropout_layer_2 = tf.keras.layers.SpatialDropout1D(sp_dropout )(normalized_layer_4)
 truncated = dropout_layer_2[:, :pred_len]
out = tf.keras.layers.Dense(5, activation='linear' )(truncated)
 model = tf.keras.Model(inputs=inputs, outputs=out)
 adam = tf.optimizers.Adam()
model.compile(optimizer = adam, loss = MCRMSE)
 return model<define_variables>
pd.crosstab(train_target, train_preds, rownames=['Actual Outcome'], colnames=['Predicted Outcome'] )
public_df = test_data.query("seq_length == 107" ).copy()
private_df = test_data.query("seq_length == 130" ).copy()<load_pretrained>
test_preds=clf.predict(test_features ).astype(int)
 test_ids=full[891:]["PassengerId"]
final = pd.DataFrame({
"PassengerId": test_ids,
"Survived": test_preds
})
 final.info()
<load_pretrained><EOS> 
final.to_csv('submission2.csv', index=False )