Kaggle competition: Oracle Graph ML Contest at Polimi
to=0.50
print('F1 score :%s
'% f1_score(train.target,pred_train>to))
print('Confusion matrix
%s'%confusion_matrix(train.target,pred_train))<filter>
def hamming_accuracy(prediction, true_values):

 return np.mean(np.sum(np.equal(prediction, true_values)) / float(true_values.size))
def get_score(prediction, true_values):
print("\tHamming accuracy: {:.3f}".format(hamming_accuracy(prediction, true_values)))
 print("\tAccuracy, exact matches: {:.3f}".format(accuracy_score(prediction, true_values)))
 print("\tMacro F1 Score: {:.3f}".format(f1_score(y_true=true_values, y_pred=prediction, average="macro")))
 print("\tMicro F1 Score: {:.3f}".format(f1_score(y_true=true_values, y_pred=prediction, average="micro")))
 def build_dataframe(input_data: pd.DataFrame, col_name: str, preserve_int_col_name=False)-> pd.DataFrame:

 vertices_dict = []
for i, row_i in input_data.iterrows() :
features = [int(float(x)) for x in row_i[f"{col_name}s"].split(";")]
new_v = {"id": i}
for j, f in enumerate(features):
new_v[j if preserve_int_col_name else f"{col_name}_{j}"] = f
vertices_dict += [new_v]
res_df = pd.DataFrame(vertices_dict)
 return res_df.set_index("id")
 def bool_to_int(labels: list)-> list:

 return [i for i, x in enumerate(labels)if x == 1]
train[train.target!=pred_train[:,0]]<string_transform>
graph_name = "ppi"
embeddings_path = ".. /input/embeddings.csv"
embeddings_df = pd.read_csv(embeddings_path, header=None, index_col=0)
 embeddings_df.columns = ["e_" + str(col)for col in embeddings_df.columns]
embeddings_df.head()
sequences_t = tokenizer.texts_to_sequences(test.question_text )<concatenate>
vertices_path = f".. /input/{graph_name}_train.csv"
vertices_train = pd.read_csv(vertices_path, sep=",", index_col="id")
 vertices_train["dataset"] = "train"
vertices_train.head()
X_t= pad_sequences(sequences_t, maxlen=vec_size )<save_to_csv>
vertices_path = f".. /input/{graph_name}_test.csv"
vertices_test = pd.read_csv(vertices_path, sep=",", index_col="id")
 vertices_test["dataset"] = "test"
vertices_test.head()
result=pd.read_csv(".. /input/sample_submission.csv",sep=',')
 pred_res=rnn_model.predict(X_t)>to
result['prediction']=pd.DataFrame(pred_res)
 result.to_csv('submission.csv',index=False )<import_modules>
X_train_df = build_dataframe(vertices_train, "feature")
 X_train_df.head()
import os
import time
import numpy as np
import pandas as pd
from tqdm import tqdm
import math
from sklearn.model_selection import train_test_split
from sklearn import metrics
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, CuDNNLSTM, Embedding, Dropout, Activation, CuDNNGRU, Conv1D
from keras.layers import Bidirectional, GlobalMaxPool1D, GlobalMaxPooling1D, GlobalAveragePooling1D
from keras.layers import Input, Embedding, Dense, Conv2D, MaxPool2D, concatenate
from keras.layers import Reshape, Flatten, Concatenate, Dropout, SpatialDropout1D
from keras.optimizers import Adam
from keras.models import Model
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints, optimizers, layers
from keras.layers import *
from keras.models import *
from keras import initializers, regularizers, constraints, optimizers, layers
from keras.initializers import *
from keras.optimizers import *
import keras.backend as K
from keras.callbacks import *
import tensorflow as tf
import os
import time
import gc
import re
import glob<define_variables>
X_test_df = build_dataframe(vertices_test, "feature")
 X_test_df.head()
max_features = 100000
maxlen = 72
embed_size = 300<define_variables>
y_train_df = build_dataframe(vertices_train, "label", preserve_int_col_name=True)
 y_train_df.head()
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]
def clean_text(x):
x = str(x)
 for punct in puncts:
x = x.replace(punct, f' {punct} ')
 return x
def load_and_prec() :
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape)
 train_df["question_text"] = train_df["question_text"].apply(lambda x: clean_text(x))
test_df["question_text"] = test_df["question_text"].apply(lambda x: clean_text(x))
train_df, val_df = train_test_split(train_df, test_size=0.08, random_state=2018)
 train_X = train_df["question_text"].fillna("_
val_X = val_df["question_text"].fillna("_
test_X = test_df["question_text"].fillna("_

 tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 val_X = tokenizer.texts_to_sequences(val_X)
 test_X = tokenizer.texts_to_sequences(test_X)

train_X = pad_sequences(train_X, maxlen=maxlen)
 val_X = pad_sequences(val_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train_df['target'].values
val_y = val_df['target'].values
np.random.seed(2018)
 trn_idx = np.random.permutation(len(train_X))
val_idx = np.random.permutation(len(val_X))
train_X = train_X[trn_idx]
val_X = val_X[val_idx]
train_y = train_y[trn_idx]
val_y = val_y[val_idx]
return train_X, val_X, test_X, train_y, val_y, tokenizer.word_index<split>
X_train_df = pd.merge(X_train_df, embeddings_df, left_index=True, right_index=True, how="left")
 X_test_df = pd.merge(X_test_df, embeddings_df, left_index=True, right_index=True, how="left")
 X_train_df.head()
train_X, val_X, test_X, train_y, val_y, word_index = load_and_prec()<feature_engineering>
kfolds = KFold(n_splits=10)
 seed = random.randint(0, 2**32)
 sgd = SGDClassifier(loss="log", max_iter=100, tol=1e-3)
 model = OneVsRestClassifier(sgd, n_jobs=1 )
embedding_matrix = load_glove(word_index )<choose_model_class>
scores = cross_val_score(model, X_train_df, y_train_df, cv=kfolds, n_jobs=32, verbose=2, scoring="f1_micro")
 print(f"Mean crossvalidation Micro-F1: {np.mean(scores):.3f}" )
def model_lstm_atten(embedding_matrix):
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 = AttentionWithContext()(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', metrics=['accuracy'])
 return model<compute_test_metric>
model.fit(X_train_df, y_train_df)
 print("Train accuracy")
 y_train_pred = model.predict(X_train_df)
 get_score(y_train_pred, y_train_df.values)
 y_test_pred = model.predict(X_test_df )
def focal_loss(gamma=2, alpha=0.75):
def focal_loss_fixed(y_true, y_pred):
eps = 1e-12
y_pred=K.clip(y_pred,eps,1.-eps)
 pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
return -K.sum(alpha * K.pow(1.- pt_1, gamma)* K.log(pt_1)) -K.sum(( 1-alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0))
return focal_loss_fixed
def train_pred(model, epochs=2):
filepath="weights_best.h5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=2, save_best_only=True, mode='min')
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1, min_lr=0.0001, verbose=2)
 earlystopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=2, verbose=2, mode='auto')
 callbacks = [checkpoint, reduce_lr]
for e in range(epochs):
model.fit(train_X, train_y, batch_size=512, epochs=1, validation_data=(val_X, val_y),callbacks=callbacks)
 model.load_weights(filepath)
 pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 pred_test_y = model.predict([test_X], batch_size=1024, verbose=0)
 return pred_val_y, pred_test_y<predict_on_test>
y_pred = [" ".join([str(y)for y in bool_to_int(x)])for x in y_test_pred]
y_pred_df = pd.DataFrame(y_pred, columns=["labels"], index=X_test_df.index)
 y_pred_df.to_csv(f"prediction_{datetime.datetime.now().strftime('%y_%m_%d_%H_%M_%S')}.csv" )