Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
pred_glove_val_y = model.predict([val_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(val_y,(pred_glove_val_y>thresh ).astype(int))))<predict_on_test>
df = pd.read_csv('.. /input/titanic/train.csv')
 df.head()
pred_glove_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<set_options>
df.columns[df.isna().any() ]
del word_index, embeddings_index, all_embs, embedding_matrix, model, inp, x
time.sleep(10 )<statistical_test>
df['Age'].fillna(df['Age'].mean() , inplace=True)
 df['Embarked'].fillna('S', inplace=True)
 df.set_index('PassengerId', inplace=True)
 df['Sex'] = df['Sex'].astype('category')
 df['Sex'] = df['Sex'].cat.codes
df['Embarked'] = df['Embarked'].astype('category')
 df['Embarked'] = df['Embarked'].cat.codes
df.head()
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(64, activation="relu" )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'] )<train_model>
def convertRanges(data):
_, bins = pd.qcut(data, 5, retbins=True)
 for i in range(len(data.values)) :
x = data.values[i]
if(x>=bins[0] and x<bins[1]):
x = 1
elif(x>=bins[1] and x<bins[2]):
x = 2
elif(x>=bins[2] and x<bins[3]):
x = 3
elif(x>=bins[3] and x<bins[4]):
x = 4
elif(x>=bins[4] and x<=bins[5]):
x = 5
data.values[i] = x
return data
model.fit(train_X, train_y, batch_size=512, epochs=3, validation_data=(val_X, val_y))<predict_on_test>
df['Age'] = convertRanges(df['Age'])
 df['Fare'] = convertRanges(df['Fare'])
 df.head()
pred_fasttext_val_y = model.predict([val_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(val_y,(pred_fasttext_val_y>thresh ).astype(int))))<predict_on_test>
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=10, max_features=1),
MLPClassifier(alpha=1, max_iter=1000),
AdaBoostClassifier() ,
GaussianNB() ,
QuadraticDiscriminantAnalysis() ]
modelAccuracy = []
for model in classifiers:
kf = KFold(n_splits=10, shuffle=False)
 X = df.iloc[:,1:].values
y = df.iloc[:,0].values
scores = []
for train_index, test_index in kf.split(df.values):
X_train, X_test, y_train, y_test = X[train_index], X[test_index], y[train_index], y[test_index]
model.fit(X_train, y_train)
 scores.append(model.score(X_test, y_test))
modelAccuracy.append(np.mean(scores))
maxIndex = np.argmax(modelAccuracy)
 model = classifiers[maxIndex]
model.fit(X, y )
pred_fasttext_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<set_options>
df = pd.read_csv('.. /input/titanic/test.csv')
 df.head()
del word_index, embeddings_index, all_embs, embedding_matrix, model, inp, x
time.sleep(10 )<statistical_test>
df.columns[df.isna().any() ]
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(64, activation="relu" )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'] )<train_model>
df['Age'].fillna(df['Age'].mean() , inplace=True)
 df['Fare'].fillna(df['Fare'].mean() , inplace=True)
 df['Age'] = convertRanges(df['Age'])
 df['Fare'] = convertRanges(df['Fare'])
 df['Sex'] = df['Sex'].astype('category')
 df['Sex'] = df['Sex'].cat.codes
df['Embarked'] = df['Embarked'].astype('category')
 df['Embarked'] = df['Embarked'].cat.codes
df.head()
<predict_on_test><EOS> 
X_test = df.iloc[:,1:].values
y_test = model.predict(X_test)
 pred = pd.DataFrame({"PassengerId": df.iloc[:,0].values, "Survived": y_test})
 pred.to_csv('results.csv', index=False, header=True )