Kaggle competition: Predicting Molecular Properties
def preprocess_text_for_word_embeddings(text):
for word in text.split() :
if word.lower() in contractions:
text = text.replace(word, contractions[word.lower() ])
 text = re.sub(r'\d+', '', text)
 text = re.sub('[^A-Za-z0-9]+', ' ', text)
 text = text.strip()
return text<categorify>
sub_path = ".. /input/chemistry-models"
all_files = os.listdir(sub_path)
 all_files
full_text = [i for i in train['comment_text']] + [i for i in test['comment_text']]
full_text = [(lambda x: preprocess_text_for_word_embeddings(x))(x)for x in full_text]<feature_engineering>
concat_sub['m_max'] = concat_sub.iloc[:, 1:].max(axis=1)
 concat_sub['m_min'] = concat_sub.iloc[:, 1:].min(axis=1)
 concat_sub['m_median'] = concat_sub.iloc[:, 1:].median(axis=1 )
train['clean_text'] = full_text[:len(train)]
test['clean_text'] = full_text[len(train):]<feature_engineering>
cutoff_lo = 0.8
cutoff_hi = 0.2
def tfidf_transform(clean_text):
vectorizer = TfidfVectorizer(stop_words = set(stopwords.words('english')))
 vectorizer.fit(clean_text)
 x_train_tfidf_vec = vectorizer.transform(clean_text[:len(train)])
 x_test_tfidf_vec = vectorizer.transform(clean_text[len(train):])
 print(x_train_tfidf_vec.shape, x_test_tfidf_vec.shape)
 return x_train_tfidf_vec, x_test_tfidf_vec<feature_engineering>
rank = np.tril(concat_sub.iloc[:,1:ncol].corr().values,-1)
 m_gmean = 0
n = 8
while rank.max() >0:
mx = np.unravel_index(rank.argmax() , rank.shape)
 m_gmean += n*(np.log(concat_sub.iloc[:, mx[0]+1])+ np.log(concat_sub.iloc[:, mx[1]+1])) /2
rank[mx] = 0
n += 1
x_train_tfidf_vec, x_test_tfidf_vec = tfidf_transform(lower_case_text )<train_on_grid>
concat_sub['m_mean'] = np.exp(m_gmean/(n-1)**2 )
clf = LogisticRegression()
grid_values = {'penalty': ['l1', 'l2'],'C':[0.001,.009,0.01,.09,1,5,10,25]}
grid_clf = GridSearchCV(clf, param_grid = grid_values,scoring = 'recall')
 grid_clf.fit(x_train_tfidf_vec, train['toxic'] )<compute_train_metric>
concat_sub['scalar_coupling_constant'] = concat_sub['m_mean']
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )
y_pred_acc = grid_clf.predict_proba(x_test_tfidf_vec)[:,1]
roc_auc_score(test['toxic'], y_pred_acc )<find_best_model_class>
concat_sub['scalar_coupling_constant'] = concat_sub['m_median']
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )
y_prediction = pd.DataFrame(columns=labels)
 for col in labels:
model = LogisticRegression()
model.fit(x_train_tfidf_vec, train[col])
 y_prediction[col] = model.predict_proba(x_test_tfidf_vec)[:,1]
print(col, " ROC AUC Score = ",roc_auc_score(test[col], y_prediction[col]))<categorify>
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:7] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:7] < cutoff_hi, axis=1),
0, concat_sub['m_median']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )
svd = TruncatedSVD(n_components = 100)
 x_train_tfidf_vec = svd.fit_transform(x_train_tfidf_vec)
 x_test_tfidf_vec = svd.fit_transform(x_test_tfidf_vec )<concatenate>
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:7] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:7] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_mean']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )
<split>
concat_sub['scalar_coupling_constant'] = np.where(np.all(concat_sub.iloc[:,1:7] > cutoff_lo, axis=1),
concat_sub['m_max'],
np.where(np.all(concat_sub.iloc[:,1:7] < cutoff_hi, axis=1),
concat_sub['m_min'],
concat_sub['m_median']))
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )
<split><EOS> 
concat_sub['scalar_coupling_constant'] = concat_sub['mol0'].rank(method ='min')+ concat_sub['mol1'].rank(method ='min')+ concat_sub['mol2'].rank(method ='min')
 concat_sub['scalar_coupling_constant'] =(concat_sub['scalar_coupling_constant']-concat_sub['scalar_coupling_constant'].min())/(concat_sub['scalar_coupling_constant'].max() - concat_sub['scalar_coupling_constant'].min())
 concat_sub.describe()
concat_sub[['id', 'scalar_coupling_constant']].to_csv('stack_rank.csv', index=False, float_format='%.8f' )