Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<train_model>
import numpy as np
import pandas as pd
X_train = X[:nrow_train]
model = Ridge(solver = "lsqr", fit_intercept=False)
 model.fit(X_train, y_train)
 model.score(X_train, y_train )<save_to_csv>
df1 = pd.read_csv('.. /input/moa-blending/0.01832.csv')
 df2 = pd.read_csv('.. /input/mypredpublic/my_predictions_0000001.csv')
 df3 = pd.read_csv('.. /input/moa-blending/0.01833.csv')
X_test = X[nrow_train:]
preds = model.predict(X_test)
 df_test["price"] = np.expm1(preds)
 df_test[["test_id", "price"]].to_csv("submission_ridge.csv", index = False )<set_options>
def order_sub(sub):
return sub.sort_values('sig_id' ).reset_index(drop=True )
warnings.filterwarnings('ignore')
 plt.style.use('dark_background')
 os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
 KTF.set_session(sess)
 def mse(y_true, y_pred):
return KTF.mean(KTF.square(y_pred - y_true))
class Clock:
def __enter__(self):
self.begin_time = time.time()
def __exit__(self, exec_type, exec_value, exec_trace):
print("time:", time.time() - self.begin_time, "s")
 def parallelize_dataframe(df, func, cores=4):

 df_split = np.array_split(df, cores)
 pool = Pool(cores)
 df = pd.concat(pool.map(func, df_split), axis=0)
 pool.close()
pool.join()
return df
def clean_str(text, max_text_length=60):

 try:
text = ' '.join([w for w in text.split() [:max_text_length]])
 text = text.lower()
text = re.sub(u"é", u"e", text)
 text = re.sub(u"ē", u"e", text)
 text = re.sub(u"è", u"e", text)
 text = re.sub(u"ê", u"e", text)
 text = re.sub(u"à", u"a", text)
 text = re.sub(u"â", u"a", text)
 text = re.sub(u"ô", u"o", text)
 text = re.sub(u"ō", u"o", text)
 text = re.sub(u"ü", u"u", text)
 text = re.sub(u"ï", u"i", text)
 text = re.sub(u"ç", u"c", text)
 text = re.sub(u"\u2019", u"'", text)
 text = re.sub(u"\xed", u"i", text)
 text = re.sub(u"w\/", u" with ", text)
 text = re.sub(u"[^a-z0-9]", " ", text)
 text = u" ".join(re.split('(\d+)',text))
text = re.sub(u"\s+", u" ", text ).strip()
except:
text = np.NaN
return text
def take(generator):
try:
for elem in generator:
yield elem
except StopIteration:
return
def fill_brandname(name, brand_name_set):
try:
for size in [4, 3, 2, 1]:
for x in take(ngrams(name.split() , size)) :
if " ".join(x)in brand_name_set:
return " ".join(x)
 return np.NaN
except:
return np.NaN
def clean_str_df(x):
return x.apply(lambda y: clean_str(y))
def fill_brandname_df(x):
global brand_name_set
return x.apply(lambda y: fill_brandname(y, brand_name_set))
def lg(text):
text = [x for x in text.split() if x != '']
return len(text)
 def tokenize(text):
return [w for w in text.split() ]
def get_sample(train_file, test_file, max_text_length=60):
print("读取样本")
 with Clock() :
train = pd.read_csv(train_file, sep='\t', encoding='utf-8')
 train.drop(columns=["train_id"], inplace=True)
 train["price"] = np.log1p(train["price"].values)
 print('train shape:', train.shape, "columns:", train.columns)
 test = pd.read_csv(test_file, sep='\t', encoding='utf-8')
 test.drop(columns=["test_id"], inplace=True)
 print('test shape:', test.shape, "columns:", test.columns)
 sample = pd.concat([train.drop(columns=["price"], axis=0), test], axis=0)
 gc.collect()
with Clock() :
print("处理特征")
 sample["item_description"].fillna("", inplace=True)
 sample["item_description"] = sample["item_description"].apply(lambda x :
x.replace('No description yet',''))
sample["item_description"] = parallelize_dataframe(sample["item_description"], clean_str_df)
 sample["name"] = parallelize_dataframe(sample["name"], clean_str_df)
 sample["brand_name"] = parallelize_dataframe(sample["brand_name"], clean_str_df)
 global brand_name_set
brand_name_set = sample.groupby("brand_name" ).size()
sample.loc[sample["brand_name"].isnull() , "brand_name"] = parallelize_dataframe(
 sample.loc[sample["brand_name"].isnull() , "name"],
fill_brandname_df)
 sample["brand_name"].fillna("", inplace=True)
 sample["item_condition_id"].fillna(2, inplace=True)
 sample["category_name"].fillna("//", inplace=True)
 sample["catery_name_first"] = sample["category_name"].apply(lambda x: x.split("/")[0])
 sample["catery_name_second"] = sample["category_name"].apply(lambda x: x.split("/")[1])
 sample["catery_name_third"] = sample["category_name"].apply(lambda x: x.split("/")[2])
 sample['category_name'] = sample['category_name'].apply(lambda x: ' '.join(x.split('/')).strip())
 sample['nb_words_item_description'] = sample['item_description'].apply(lg ).astype(np.uint16)
 sample['nb_words_item_description'] = 1.0 * sample['nb_words_item_description'] / max_text_length
for x in ['brand_name', 'category_name', 'catery_name_first', 'catery_name_second',
'catery_name_third']:
le = preprocessing.LabelEncoder()
sample[x] = le.fit_transform(sample[x])
 sample["item_condition_id"] = 1.0 * sample["item_condition_id"] / 5
sample["old_name"] = sample["name"].copy()
sample["brand_cat"] = "cat1_" + sample["catery_name_first"].astype(str)+ " " + \
"cat2_" + sample["catery_name_second"].astype(str)+ " " + \
"cat3_" + sample["catery_name_third"].astype(str)+ " " + \
"brand_" + sample["brand_name"].astype(str)
 sample["name"] = sample["brand_cat"] + " " + sample["name"]
sample["name_desc"] = sample["name"] + " " + sample["item_description"].apply(lambda x: " ".join(x.split() [:5]))
gc.collect()
print("构造文本特征:")
 split_num = train.shape[0]
with Clock() :
print("brand_cat")
 vectorizer_brandcat = CountVectorizer(lowercase=True, tokenizer=tokenize,
ngram_range=(1, 3), min_df=2, max_features=100000,
binary=True, dtype=np.int8)
 vectorizer_brandcat.fit(sample[:split_num]['brand_cat'])
 sparse_data_brandcat = vectorizer_brandcat.transform(sample['brand_cat'])
 del vectorizer_brandcat
sample.drop(columns=["brand_cat"], inplace=True)
 gc.collect()
print("old_name")
 vectorizer_name = CountVectorizer(lowercase=True, analyzer="char",
ngram_range=(2, 4), min_df=5, max_features=100000,
binary=True, dtype=np.int8)
 vectorizer_name.fit(sample[:train.shape[0]]['old_name'])
 sparse_data_name = vectorizer_name.transform(sample['old_name'])
 del vectorizer_name
sample.drop(columns=["old_name"], inplace=True)
 gc.collect()
print("item_description")
 vectorizer_desc = CountVectorizer(lowercase=True, tokenizer=tokenize, stop_words="english",
ngram_range=(1, 1), min_df=20, max_features=100000,
binary=True, dtype=np.int8)
 vectorizer_desc.fit(sample[:train.shape[0]]["item_description"])
 sparse_data_desc = vectorizer_desc.transform(sample['item_description'])
 del vectorizer_desc
sample.drop(columns=["item_description"], inplace=True)
 gc.collect()
print("构建统计特征:")
 with Clock() :
train_new = sample[:train.shape[0]].copy()
train_new["price"] = train["price"]
for x in ['catery_name_first', 'catery_name_second', 'catery_name_third', 'category_name', 'brand_name' ]:
tmp = train_new.groupby(x)["price"].mean().astype(np.float32)
 sample['mean_price_' + x] = sample[x].map(tmp ).astype(np.float32)
 sample['mean_price_' + x].fillna(tmp.mean() , inplace=True)
 gc.collect()
print("构造样本:")
 with Clock() :
x = {
'sparse_data_name': sparse_data_name[:split_num],
'sparse_data_brandcat': sparse_data_brandcat[:split_num],
'sparse_data_desc': sparse_data_desc[:split_num],
'item_condition': np.array(sample['item_condition_id'])[:split_num],
'shipping': np.array(sample["shipping"])[:split_num],
'mean_price_catery_name_second': np.array(sample["mean_price_catery_name_second"])[:split_num],
'nb_words_item_description': np.array(sample["nb_words_item_description"])[:split_num]
}
test = {
'sparse_data_name': sparse_data_name[split_num:],
'sparse_data_brandcat': sparse_data_brandcat[split_num:],
'sparse_data_desc': sparse_data_desc[split_num:],
'item_condition': np.array(sample['item_condition_id'])[split_num:],
'shipping': np.array(sample["shipping"])[split_num:],
'mean_price_catery_name_second': np.array(sample["mean_price_catery_name_second"])[split_num:],
'nb_words_item_description': np.array(sample["nb_words_item_description"])[split_num:]
}
y = train.price.values
return x, y, test
def model(len_sparse_data_name, len_sparse_data_brandcat, len_sparse_data_desc):
sparse_data_name = Input(shape=[len_sparse_data_name], name="sparse_data_name", dtype='float32',
sparse=True)
 sparse_data_brandcat = Input(shape=[len_sparse_data_brandcat], name="sparse_data_brandcat",
dtype='float32', sparse=True)
 sparse_data_desc = Input(shape=[len_sparse_data_desc], name="sparse_data_desc",
dtype='float32', sparse=True)
 item_condition = Input(shape=[1], name="item_condition", dtype='float32')
 shipping = Input(shape=[1], name="shipping", dtype='float32')
 mean_price_catery_name_second = Input(shape=[1], name="mean_price_catery_name_second",
dtype='float32')
 nb_words_item_description = Input(shape=[1], name="nb_words_item_description",
dtype='float32')
 x = PReLU()(Dense(50 )(sparse_data_name))
y = PReLU()(Dense(50 )(sparse_data_brandcat))
z = PReLU()(Dense(50 )(sparse_data_desc))
x = concatenate([x, y, z, item_condition, shipping, mean_price_catery_name_second,
nb_words_item_description])
 x = PReLU()(Dense(50 )(x))
x = Dense(1 )(x)
 m = Model([sparse_data_name, sparse_data_brandcat, sparse_data_desc,
item_condition, shipping, mean_price_catery_name_second,
nb_words_item_description], x)
 optimizer = Adam(0.002)
 m.compile(loss="mse", optimizer=optimizer)
 return m<prepare_x_and_y>
df1 = order_sub(df1)
 df2 = order_sub(df2)
 df3 = order_sub(df3 )
gc.collect()
path = ".. /input/"
x, y, test = get_sample(path + "train.tsv", path + "test_stg2.tsv", max_text_length=60 )<define_search_space>
BLEND=df1.copy()
BLEND.iloc[:,1:] = df1.iloc[:,1:]*0.6 +df2.iloc[:,1:]*0.2 +df3.iloc[:,1:]*0.2
m = model(x["sparse_data_name"].shape[1], x["sparse_data_brandcat"].shape[1],
x["sparse_data_desc"].shape[1])
 m.summary()<save_to_csv>
TARGET_COL = ['5-alpha_reductase_inhibitor', '11-beta-hsd1_inhibitor', 'acat_inhibitor', 'acetylcholine_receptor_agonist', 'acetylcholine_receptor_antagonist', 'acetylcholinesterase_inhibitor', 'adenosine_receptor_agonist', 'adenosine_receptor_antagonist', 'adenylyl_cyclase_activator', 'adrenergic_receptor_agonist', 'adrenergic_receptor_antagonist', 'akt_inhibitor', 'aldehyde_dehydrogenase_inhibitor', 'alk_inhibitor', 'ampk_activator', 'analgesic', 'androgen_receptor_agonist', 'androgen_receptor_antagonist', 'anesthetic_-_local', 'angiogenesis_inhibitor', 'angiotensin_receptor_antagonist', 'anti-inflammatory', 'antiarrhythmic', 'antibiotic', 'anticonvulsant', 'antifungal', 'antihistamine', 'antimalarial', 'antioxidant', 'antiprotozoal', 'antiviral', 'apoptosis_stimulant', 'aromatase_inhibitor', 'atm_kinase_inhibitor', 'atp-sensitive_potassium_channel_antagonist', 'atp_synthase_inhibitor', 'atpase_inhibitor', 'atr_kinase_inhibitor', 'aurora_kinase_inhibitor', 'autotaxin_inhibitor', 'bacterial_30s_ribosomal_subunit_inhibitor', 'bacterial_50s_ribosomal_subunit_inhibitor', 'bacterial_antifolate', 'bacterial_cell_wall_synthesis_inhibitor', 'bacterial_dna_gyrase_inhibitor', 'bacterial_dna_inhibitor', 'bacterial_membrane_integrity_inhibitor', 'bcl_inhibitor', 'bcr-abl_inhibitor', 'benzodiazepine_receptor_agonist', 'beta_amyloid_inhibitor', 'bromodomain_inhibitor', 'btk_inhibitor', 'calcineurin_inhibitor', 'calcium_channel_blocker', 'cannabinoid_receptor_agonist', 'cannabinoid_receptor_antagonist', 'carbonic_anhydrase_inhibitor', 'casein_kinase_inhibitor', 'caspase_activator', 'catechol_o_methyltransferase_inhibitor', 'cc_chemokine_receptor_antagonist', 'cck_receptor_antagonist', 'cdk_inhibitor', 'chelating_agent', 'chk_inhibitor', 'chloride_channel_blocker', 'cholesterol_inhibitor', 'cholinergic_receptor_antagonist', 'coagulation_factor_inhibitor', 'corticosteroid_agonist', 'cyclooxygenase_inhibitor', 'cytochrome_p450_inhibitor', 'dihydrofolate_reductase_inhibitor', 'dipeptidyl_peptidase_inhibitor', 'diuretic', 'dna_alkylating_agent', 'dna_inhibitor', 'dopamine_receptor_agonist', 'dopamine_receptor_antagonist', 'egfr_inhibitor', 'elastase_inhibitor', 'erbb2_inhibitor', 'estrogen_receptor_agonist', 'estrogen_receptor_antagonist', 'faah_inhibitor', 'farnesyltransferase_inhibitor', 'fatty_acid_receptor_agonist', 'fgfr_inhibitor', 'flt3_inhibitor', 'focal_adhesion_kinase_inhibitor', 'free_radical_scavenger', 'fungal_squalene_epoxidase_inhibitor', 'gaba_receptor_agonist', 'gaba_receptor_antagonist', 'gamma_secretase_inhibitor', 'glucocorticoid_receptor_agonist', 'glutamate_inhibitor', 'glutamate_receptor_agonist', 'glutamate_receptor_antagonist', 'gonadotropin_receptor_agonist', 'gsk_inhibitor', 'hcv_inhibitor', 'hdac_inhibitor', 'histamine_receptor_agonist', 'histamine_receptor_antagonist', 'histone_lysine_demethylase_inhibitor', 'histone_lysine_methyltransferase_inhibitor', 'hiv_inhibitor', 'hmgcr_inhibitor', 'hsp_inhibitor', 'igf-1_inhibitor', 'ikk_inhibitor', 'imidazoline_receptor_agonist', 'immunosuppressant', 'insulin_secretagogue', 'insulin_sensitizer', 'integrin_inhibitor', 'jak_inhibitor', 'kit_inhibitor', 'laxative', 'leukotriene_inhibitor', 'leukotriene_receptor_antagonist', 'lipase_inhibitor', 'lipoxygenase_inhibitor', 'lxr_agonist', 'mdm_inhibitor', 'mek_inhibitor', 'membrane_integrity_inhibitor', 'mineralocorticoid_receptor_antagonist', 'monoacylglycerol_lipase_inhibitor', 'monoamine_oxidase_inhibitor', 'monopolar_spindle_1_kinase_inhibitor', 'mtor_inhibitor', 'mucolytic_agent', 'neuropeptide_receptor_antagonist', 'nfkb_inhibitor', 'nicotinic_receptor_agonist', 'nitric_oxide_donor', 'nitric_oxide_production_inhibitor', 'nitric_oxide_synthase_inhibitor', 'norepinephrine_reuptake_inhibitor', 'nrf2_activator', 'opioid_receptor_agonist', 'opioid_receptor_antagonist', 'orexin_receptor_antagonist', 'p38_mapk_inhibitor', 'p-glycoprotein_inhibitor', 'parp_inhibitor', 'pdgfr_inhibitor', 'pdk_inhibitor', 'phosphodiesterase_inhibitor', 'phospholipase_inhibitor', 'pi3k_inhibitor', 'pkc_inhibitor', 'potassium_channel_activator', 'potassium_channel_antagonist', 'ppar_receptor_agonist', 'ppar_receptor_antagonist', 'progesterone_receptor_agonist', 'progesterone_receptor_antagonist', 'prostaglandin_inhibitor', 'prostanoid_receptor_antagonist', 'proteasome_inhibitor', 'protein_kinase_inhibitor', 'protein_phosphatase_inhibitor', 'protein_synthesis_inhibitor', 'protein_tyrosine_kinase_inhibitor', 'radiopaque_medium', 'raf_inhibitor', 'ras_gtpase_inhibitor', 'retinoid_receptor_agonist', 'retinoid_receptor_antagonist', 'rho_associated_kinase_inhibitor', 'ribonucleoside_reductase_inhibitor', 'rna_polymerase_inhibitor', 'serotonin_receptor_agonist', 'serotonin_receptor_antagonist', 'serotonin_reuptake_inhibitor', 'sigma_receptor_agonist', 'sigma_receptor_antagonist', 'smoothened_receptor_antagonist', 'sodium_channel_inhibitor', 'sphingosine_receptor_agonist', 'src_inhibitor', 'steroid', 'syk_inhibitor', 'tachykinin_antagonist', 'tgf-beta_receptor_inhibitor', 'thrombin_inhibitor', 'thymidylate_synthase_inhibitor', 'tlr_agonist', 'tlr_antagonist', 'tnf_inhibitor', 'topoisomerase_inhibitor', 'transient_receptor_potential_channel_antagonist', 'tropomyosin_receptor_kinase_inhibitor', 'trpv_agonist', 'trpv_antagonist', 'tubulin_inhibitor', 'tyrosine_kinase_inhibitor', 'ubiquitin_specific_protease_inhibitor', 'vegfr_inhibitor', 'vitamin_b', 'vitamin_d_receptor_agonist', 'wnt_inhibitor']
NUM_TARGET = len(TARGET_COL )
m.fit(x, y - np.mean(y), 2048, epochs=3)
 pred = m.predict(test, 2048)+ np.mean(y)
 pred = np.expm1(pred)
 pred[pred < 3] = 3
pred[pred > 1000] = 1000
test_id= pd.read_csv(path + "test_stg2.tsv", sep='\t', encoding='utf-8')[["test_id"]]
test_id["price"] = pred
test_id.to_csv("ans.csv", index=False)
 del x
del y
del test
del pred
del test_id
del m
gc.collect()<set_options>
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 df = pd.read_csv("/kaggle/input/lish-moa/sample_submission.csv")
 public_id = list(df['sig_id'].values)
 df_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_id = list(df_test['sig_id'].values)
 private_id = list(set(test_id)-set(public_id))
df_submit = pd.DataFrame(index = public_id+private_id, columns=TARGET_COL)
 df_submit.index.name = 'sig_id'
df_submit[:] = 0
df_predict = BLEND.copy()
df_submit.loc[df_predict.sig_id,:] = df_predict[TARGET_COL].values
df_submit.loc[df_test[df_test.cp_type=='ctl_vehicle'].sig_id]= 0
df_submit.to_csv('submission.csv',index=True )