Kaggle competition: Mechanisms of Action (MoA) Prediction
submission_cv['prediction'] = pred_test_y
print(submission_cv.head() )<save_to_csv>
X, y, genes, cells, classnames, features, X_test, test_control, submission = load()
splits = sorted(glob.glob(".. /input/moa-pipelines/x*"))
folds, pipes = list(zip(*[joblib.load(split)for split in splits]))
print(splits )
submission_cv.to_csv("submission.csv", index=False )<import_modules>
device =('cuda' if torch.cuda.is_available() else 'cpu')
 pseudo = 1
pseudo_optim =(torch.optim.SGD, dict(lr=1e-5, momentum=0.9, nesterov=True))
early_stopping = -1
lr = 2e-3
wd = 1e-5
top_bias = 0.0
smoothing = 3e-4
clipping = 1e-4
batch_size = 128
dropout = [0.2, 0.2]
lookahead = 10
epochs =(30, 5)
 df = 1000
max_lr = 0.01
ps = 0.15
hidden_size =(1024, 512)
 activation = nn.ReLU
model_params = {"num_features": X.shape[1], "num_targets": len(classnames), "num_units": hidden_size,
"dropout": dropout, "top_bias": top_bias, "activation": activation}
optim_params = {"lr": lr, "weight_decay": wd}
sched_params = {"epochs": epochs[0], "pct_start": ps, "div_factor": df, "max_lr": max_lr}
fitter_params = dict(
 estimator=MLP,
estimator_params=model_params,
optim_params=optim_params,
sched_params=sched_params,
lookahead=lookahead,
pseudo=pseudo,
pseudo_optim=pseudo_optim,
savepath="validation.pth",
device=device,
loss_fn=LabelSmoothingCrossEntropy(smoothing, True, clipping)
)
 fitter = Fitter(**fitter_params)
 train_params = {"epochs": epochs, "batch_size": batch_size, "early_stopping": early_stopping, "verbose": 100}
postprocessor = None
tqdm.pandas()
Activation, Dropout, SpatialDropout1D, Embedding, Concatenate, concatenate, Reshape, Flatten,
CuDNNLSTM, CuDNNGRU, Bidirectional)
 t_start = time.time()<define_variables>
%%time
seeds = np.arange(len(folds))
oof_train, oof_test = mlp_oof(X, y, X_test[~test_control], fitter, train_params, folds, seeds, pipes, postprocessor )
embed_size = 300
max_features = 120000
maxlen = 70
nb_features = 6
EPOCHS = 10<define_variables>
preds_clipping = 1e-5
oof_train = np.clip(oof_train, preds_clipping, 1 - preds_clipping)
 oof_test = np.clip(oof_test, preds_clipping, 1 - preds_clipping )
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…', '~', '•',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]
mispell_dict = {"aren't" : "are not", "can't" : "cannot", "couldn't" : "could not", "didn't" : "did not", "doesn't" : "does not",
"don't" : "do not", "hadn't" : "had not", "hasn't" : "has not", "haven't" : "have not", "he'd" : "he would", "he'll" : "he will",
"he's" : "he is", "i'd" : "I would", "i'd" : "I had", "i'll" : "I will", "i'm" : "I am", "isn't" : "is not", "it's" : "it is",
"it'll":"it will", "i've" : "I have", "let's" : "let us", "mightn't" : "might not", "mustn't" : "must not", "shan't" : "shall not",
"she'd" : "she would", "she'll" : "she will", "she's" : "she is", "shouldn't" : "should not", "that's" : "that is", "there's" : "there is",
"they'd" : "they would", "they'll" : "they will", "they're" : "they are", "they've" : "they have", "we'd" : "we would", "we're" : "we are",
"weren't" : "were not", "we've" : "we have", "what'll" : "what will", "what're" : "what are", "what's" : "what is", "what've" : "what have",
"where's" : "where is", "who'd" : "who would", "who'll" : "who will", "who're" : "who are", "who's" : "who is", "who've" : "who have",
"won't" : "will not", "wouldn't" : "would not", "you'd" : "you would", "you'll" : "you will", "you're" : "you are", "you've" : "you have",
"'re": " are", "wasn't": "was not", "we'll":" will", "didn't": "did not", "tryin'":"trying"}
bad_words = "2 girls 1 cup, 2g1c, 4r5e, 5h1t, 5hit, a$$"
bad_words = [x.strip() for x in bad_words.split(",")]
def clean_text(x):
x = str(x)
 for word in bad_words:
x = x.replace(word, "ZZZZZZ")
 for punct in puncts:
x = x.replace(punct, f' {punct} ')
 return x
def clean_numbers(x):
x = re.sub('[0-9]{10,}', '
x = re.sub('[0-9]{5,10}', '
x = re.sub('[0-9]{4}', '
x = re.sub('[0-9]{3}', '
x = re.sub('[0-9]{2}', '
return x
def _get_mispell(mispell_dict):
mispell_re = re.compile('(%s)' % '|'.join(mispell_dict.keys()))
return mispell_dict, mispell_re
mispellings, mispellings_re = _get_mispell(mispell_dict)
 def replace_typical_misspell(text):
def replace(match):
return mispellings[match.group(0)]
return mispellings_re.sub(replace, text)
 def split_text(x):
x = wordninja.split(x)
 return '-'.join(x)
 def add_features(df):
df['question_text'] = df['question_text'].progress_apply(lambda x:str(x))
df['total_length'] = df['question_text'].progress_apply(len)
 df['capitals'] = df['question_text'].progress_apply(lambda comment: sum(1 for c in comment if c.isupper()))
df['caps_vs_length'] = df.progress_apply(lambda row: float(row['capitals'])/float(row['total_length']),
axis=1)
 df['num_words'] = df.question_text.str.count('\S+')
 df['num_unique_words'] = df['question_text'].progress_apply(lambda comment: len(set(w for w in comment.split())))
 df['words_vs_unique'] = df['num_unique_words'] / df['num_words']
return df
def load_and_prec() :
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 for df in [train_df, test_df]:
df["question_text"] = df["question_text"].str.lower()
df["question_text"] = df["question_text"].progress_apply(lambda x: clean_text(x))
df["question_text"] = df["question_text"].progress_apply(lambda x: clean_numbers(x))
df["question_text"] = df["question_text"].progress_apply(lambda x: replace_typical_misspell(x))
df["question_text"] = df["question_text"].fillna("_
print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape)
 _train = add_features(train_df)
 _test = add_features(test_df)
 ff = ['total_length', 'capitals', 'caps_vs_length', 'num_words', 'num_unique_words', 'words_vs_unique']
features = _train[ff].fillna(0)
 test_features = _test[ff].fillna(0)
 ss = StandardScaler()
ss.fit(np.vstack(( features, test_features)))
 features = ss.transform(features)
 test_features = ss.transform(test_features)
 train_X, valid_X, train_y, val_y = train_test_split(
 list(zip(train_df['question_text'].values, features)) ,
train_df['target'].values,
test_size=0.2, random_state=2018 
)
 test_X = test_df["question_text"].values
train_X, features = zip(*train_X)
 val_X, val_features = zip(*valid_X)
 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)
 return train_X, val_X, test_X, train_y, val_y, features, val_features, test_features, tokenizer.word_index<statistical_test>
oof_result(y, oof_train, oof_test )
<choose_model_class><EOS> 
prediction = join_control_group(oof_test, test_control, submission, classnames)
 submit_preds(prediction, submission, test_control, classnames)
 joblib.dump(oof_train, "oof_train.pkl")
 pd.read_csv("submission.csv" ).iloc[:5, :5]