Kaggle competition: Santander Value Prediction Challenge
def handle_punctuation(x):
x = x.translate(remove_dict)
 x = x.translate(isolate_dict)
 return x
def handle_contractions(x):
x = TreebankWordTokenizer().tokenize(x)
 return x
def fix_quote(x):
x = [x_[1:] if x_.startswith("'")else x_ for x_ in x]
x = ' '.join(x)
 return x
def preprocess(x):
x = handle_punctuation(x)
 x = handle_contractions(x)
 x = fix_quote(x)
 return x<load_pretrained>
def activation(x):
return np.log1p(x)
 def deactivation(x):
return np.expm1(x )
with open(f".. /input/{KERAS_PATH}/saved_tokenizer.pickle", 'rb')as handle:
tokenizer = pickle.load(handle )<data_type_conversions>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
test_df[TEXT_COLUMN] = test_df[TEXT_COLUMN].apply(lambda x:preprocess(x))
x_test = test_df[TEXT_COLUMN].astype(str)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN )<load_pretrained>
_x_train = df_train.drop(['ID', 'target'], axis=1 ).astype('float32')
 _x_test = df_test.drop(['ID'], axis=1 ).astype('float32')
 y_train = activation(df_train['target'] )
json_file = open(f".. /input/{KERAS_PATH}/saved_model.json", 'r')
 model_json = json_file.read()
json_file.close()
model = model_from_json(model_json)
 model.load_weights(f".. /input/{KERAS_PATH}/saved_model.h5" )<choose_model_class>
del df_train
del df_test
gc.collect()
model.compile(loss='binary_crossentropy', optimizer='adam')
 prediction = model.predict(x_test, batch_size=2048)[0].flatten()<save_to_csv>
n_dim = 32
svd_model = TruncatedSVD(n_dim, random_state=0)
 svd_data_train = pd.DataFrame(svd_model.fit_transform(_x_train), columns=[f'svd_{i}' for i in range(n_dim)] ).astype('float32')
 svd_data_test = pd.DataFrame(svd_model.transform(_x_test), columns=[f'svd_{i}' for i in range(n_dim)] ).astype('float32' )
submission = pd.DataFrame.from_dict({
'id': test_df.id,
'prediction': prediction
})
 submission.to_csv('submission.csv', index=False )<create_dataframe>
validation = pd.DataFrame.from_dict({
'id': test_df.id,
'comment_text': test_df.comment_text,
'prediction': prediction
} )<import_modules>
gc.collect()
print(os.listdir(".. /input"))
seed = 42
np.random.seed(seed )<define_variables>
x_train = pd.concat([_x_train,
svd_data_train,
], axis=1, copy=False)
 x_test = pd.concat([_x_test,
svd_data_test,
], axis=1, copy=False )
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = LSTM_UNITS
WORD_EMBED_DIM = 300
EPOCHS = 3
WORD_MAX_LEN = 220
MAX_FEATURES = None<define_variables>
EMBEDDING_FILES = [
'.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec',
'.. /input/glove840b300dtxt/glove.840B.300d.txt'
]<categorify>
gc.collect()
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32')
 def load_embeddings(path):
with open(path)as f:
return dict(get_coefs(*line.strip().split(' ')) for line in f)
 def build_matrix(word_index, path):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+ 1, 300))
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
pass
return embedding_matrix<define_variables>
all_params = {'min_child_weight': [3],
'subsample': [1],
'subsample_freq': [1],
'seed': [114514],
'colsample_bytree': [0.8],
'learning_rate': [0.01],
'max_depth': [-1],
'min_split_gain': [0.01],
'reg_alpha': [0],
'max_bin': [63],
'num_leaves': [255],
'objective': ['regression_l1'],
'scale_pos_weight': [1],
'verbose': [-1],
'boosting_type': ['gbdt'],
'metric': ['rmse'],
}
contraction_mapping = {"ain't": "is not", "aren't": "are not","can't": "cannot", "'cause": "because", "could've": "could have", "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", "how'd": "how did", "how'd'y": "how do you", "how'll": "how will", "how's": "how is", "I'd": "I would", "I'd've": "I would have", "I'll": "I will", "I'll've": "I will have","I'm": "I am", "I've": "I have", "i'd": "i would", "i'd've": "i would have", "i'll": "i will", "i'll've": "i will have","i'm": "i am", "i've": "i have", "isn't": "is not", "it'd": "it would", "it'd've": "it would have", "it'll": "it will", "it'll've": "it will have","it's": "it is", "let's": "let us", "ma'am": "madam", "mayn't": "may not", "might've": "might have","mightn't": "might not","mightn't've": "might not have", "must've": "must have", "mustn't": "must not", "mustn't've": "must not have", "needn't": "need not", "needn't've": "need not have","o'clock": "of the clock", "oughtn't": "ought not", "oughtn't've": "ought not have", "shan't": "shall not", "sha'n't": "shall not", "shan't've": "shall not have", "she'd": "she would", "she'd've": "she would have", "she'll": "she will", "she'll've": "she will have", "she's": "she is", "should've": "should have", "shouldn't": "should not", "shouldn't've": "should not have", "so've": "so have","so's": "so as", "this's": "this is","that'd": "that would", "that'd've": "that would have", "that's": "that is", "there'd": "there would", "there'd've": "there would have", "there's": "there is", "here's": "here is","they'd": "they would", "they'd've": "they would have", "they'll": "they will", "they'll've": "they will have", "they're": "they are", "they've": "they have", "to've": "to have", "wasn't": "was not", "we'd": "we would", "we'd've": "we would have", "we'll": "we will", "we'll've": "we will have", "we're": "we are", "we've": "we have", "weren't": "were not", "what'll": "what will", "what'll've": "what will have", "what're": "what are", "what's": "what is", "what've": "what have", "when's": "when is", "when've": "when have", "where'd": "where did", "where's": "where is", "where've": "where have", "who'll": "who will", "who'll've": "who will have", "who's": "who is", "who've": "who have", "why's": "why is", "why've": "why have", "will've": "will have", "won't": "will not", "won't've": "will not have", "would've": "would have", "wouldn't": "would not", "wouldn't've": "would not have", "y'all": "you all", "y'all'd": "you all would","y'all'd've": "you all would have","y'all're": "you all are","y'all've": "you all have","you'd": "you would", "you'd've": "you would have", "you'll": "you will", "you'll've": "you will have", "you're": "you are", "you've": "you have" }
mispell_dict = {'colour': 'color', 'centre': 'center', 'favourite': 'favorite', 'travelling': 'traveling', 'counselling': 'counseling', 'theatre': 'theater', 'cancelled': 'canceled', 'labour': 'labor', 'organisation': 'organization', 'wwii': 'world war 2', 'citicise': 'criticize', 'youtu ': 'youtube ', 'Qoura': 'Quora', 'sallary': 'salary', 'Whta': 'What', 'narcisist': 'narcissist', 'howdo': 'how do', 'whatare': 'what are', 'howcan': 'how can', 'howmuch': 'how much', 'howmany': 'how many', 'whydo': 'why do', 'doI': 'do I', 'theBest': 'the best', 'howdoes': 'how does', 'mastrubation': 'masturbation', 'mastrubate': 'masturbate', "mastrubating": 'masturbating', 'pennis': 'penis', 'Etherium': 'Ethereum', 'narcissit': 'narcissist', 'bigdata': 'big data', '2k17': '2017', '2k18': '2018', 'qouta': 'quota', 'exboyfriend': 'ex boyfriend', 'airhostess': 'air hostess', "whst": 'what', 'watsapp': 'whatsapp', 'demonitisation': 'demonetization', 'demonitization': 'demonetization', 'demonetisation': 'demonetization'}
punct_mapping = {"‘": "'", "₹": "e", "´": "'", "°": "", "€": "e", "™": "tm", "√": " sqrt ", "×": "x", "²": "2", "—": "-", "–": "-", "’": "'", "_": "-", "`": "'", '“': '"', '”': '"', '“': '"', "£": "e", '∞': 'infinity', 'θ': 'theta', '÷': '/', 'α': 'alpha', '•': '.', 'à': 'a', '−': '-', 'β': 'beta', '∅': '', '³': '3', 'π': 'pi', }
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', '\t', '
']
def clean_words(x):
for dic in [contraction_mapping, mispell_dict]:
for word in dic.keys() :
x = x.replace(word, dic[word])
 return x
def clean_punct_mapping(x):
for word in punct_mapping.keys() :
x = x.replace(word, f' {word} ')
 return x
def clean_puncts(x):
x = str(x)
 for punct in puncts:
x = x.replace(punct, ' ')
 return x
def clean_text(text):

 text = text.lower()
text = clean_words(text)
 text = clean_punct_mapping(text)
 text = clean_puncts(text)
 return text
def preprocess(data):
data = data.astype(str ).apply(lambda x: clean_text(x))
return data<load_from_csv>
min_score = 10000
min_params = None
cv = KFold(n_splits=5, shuffle=True, random_state=114514)
 for params in tqdm(list(ParameterGrid(all_params))):
for test, train in cv.split(x_train, y_train):
trn_x = x_train.iloc[train]
val_x = x_train.iloc[test]
trn_y = y_train[train]
val_y = y_train[test]
train_data = lgb.Dataset(trn_x,
label=trn_y 
)
 test_data = lgb.Dataset(val_x,
label=val_y 
)
 clf = lgb.train(params,
train_data,
100000,
early_stopping_rounds=100,
valid_sets=[test_data],
verbose_eval=30 
)
 params['n_tree'] = clf.best_iteration
pred = clf.predict(val_x)
 score = np.sqrt(mean_squared_error(val_y, pred))
if min_score > score:
min_score = score
min_params = params
print(score, min_score)
 break
train = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv')
 test = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv' )<prepare_x_and_y>
print(min_score, min_params )
x_train = preprocess(train['comment_text'])
 y_train = np.where(train['target'] >= 0.5, 1, 0)
 y_aux_train = train[['target', 'severe_toxicity', 'obscene', 'identity_attack', 'insult', 'threat']]
x_test = preprocess(test['comment_text'] )<train_model>
train_data = lgb.Dataset(x_train,
label=y_train, 
)
 n_tree = int(clf.best_iteration * 1.1)
 clf = lgb.train(min_params,
train_data,
n_tree,
valid_sets=[train_data],
verbose_eval=10
)
<define_variables><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = deactivation(clf.predict(x_test))
sub.to_csv('submit.csv', index=False )