Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<define_variables>
%load_ext autoreload
%autoreload 2
%matplotlib inline
seed = 93
punctuation_symbols = []
for symbol in punctuation:
punctuation_symbols.append(( symbol, ''))<string_transform>
warnings.filterwarnings('ignore')
 plt.style.use('ggplot')
def remove_punctuation(sentence: str)-> str:
return sentence.translate(str.maketrans('', '', string.punctuation))<string_transform>
PATH = '.. /input/'
def remove_digits(x):
x = ''.join([i for i in x if not i.isdigit() ])
 return x<string_transform>
train_raw = pd.read_csv(f'{PATH}/train/train.csv', low_memory=False)
 test_raw = pd.read_csv(f'{PATH}/test/test.csv', low_memory=False )
stop = stopwords.words('english')
 def remove_stop_words(x):
x = ' '.join([i for i in x.lower().split(' ')if i not in stop])
 return x<categorify>
train_raw.isnull().sum(axis=0 )
def to_lower(x):
return x.lower()<feature_engineering>
train_raw.isnull().sum(axis=1 ).sort_values(ascending=False ).head(10 )
def transform_category_name(category_name):
try:
main, sub1, sub2= category_name.split('/')
 return main, sub1, sub2
except:
return np.nan, np.nan, np.nan
train_data['category_main'], train_data['subcat_1'], train_data['subcat_2'] = zip(*train_data['category_name'].apply(transform_category_name))
cat_train = train_data[['category_main','subcat_1','subcat_2', 'price']]<set_options>
train_raw['from_dataset'] = 'train'
test_raw['from_dataset'] = 'test'
alldata = pd.concat([train_raw, test_raw], axis = 0 )
gc.collect()<feature_engineering>
feats_counts = alldata.nunique(dropna = False )
combined_data.item_description = combined_data.item_description.astype(str)
 descr = combined_data[['item_description', 'price']]
descr['count'] = descr['item_description'].apply(lambda x : len(str(x)))
 descr['item_description'] = descr['item_description'].apply(remove_digits)
 descr['item_description'] = descr['item_description'].apply(remove_punctuation)
 descr['item_description'] = descr['item_description'].apply(remove_stop_words)
 descr.head(20 )<feature_engineering>
feats_counts.sort_values() [:10]
porter = PorterStemmer()
descr['item_description'] = descr['item_description'].apply(porter.stem)
 descr.tail(20 )<categorify>
alldata.fillna('NaN', inplace=True )
def handle_missing_values(df):
df['category_name'].fillna(value='missing', inplace=True)
 df['brand_name'].fillna(value='None', inplace=True)
 df['item_description'].fillna(value='None', inplace=True )<data_type_conversions>
alldata['Age'].value_counts().head(20 )
def to_categorical(df):
df['brand_name'] = df['brand_name'].astype('category')
 df['category_name'] = df['category_name'].astype('category')
 df['item_condition_id'] = df['item_condition_id'].astype('category' )<categorify>
alldata['age_mod_12'] = alldata['Age'].apply(lambda x: True if(x%12)==0 else False )
handle_missing_values(combined_frac)
 to_categorical(combined_frac )<categorify>
dogs = alldata.loc[alldata['Type'] == 1]
cats = alldata.loc[alldata['Type'] == 2]
handle_missing_values(combined_data)
 to_categorical(combined_data )<set_options>
dogs['Name'].value_counts().head(20 )
gc.collect()<feature_engineering>
cats['Name'].value_counts().head(20 )
combined_frac.item_description = combined_frac.item_description.astype(str)
 combined_frac['item_description'] = combined_frac['item_description'].apply(remove_digits)
 combined_frac['item_description'] = combined_frac['item_description'].apply(remove_punctuation)
 combined_frac['item_description'] = combined_frac['item_description'].apply(remove_stop_words)
 combined_frac['item_description'] = combined_frac['item_description'].apply(to_lower)
 combined_frac['name'] = combined_frac['name'].apply(remove_digits)
 combined_frac['name'] = combined_frac['name'].apply(remove_punctuation)
 combined_frac['name'] = combined_frac['name'].apply(remove_stop_words)
 combined_frac['name'] = combined_frac['name'].apply(to_lower)
 combined_frac.head()<feature_engineering>
alldata['NaN_name'] = alldata['Name'].apply(lambda x: True if str(x)== 'NaN' else False )
combined_data.item_description = combined_data.item_description.astype(str)
 combined_data['item_description'] = combined_data['item_description'].apply(remove_digits)
 combined_data['item_description'] = combined_data['item_description'].apply(remove_punctuation)
 combined_data['item_description'] = combined_data['item_description'].apply(remove_stop_words)
 combined_data['item_description'] = combined_data['item_description'].apply(to_lower)
 combined_data['name'] = combined_data['name'].apply(remove_digits)
 combined_data['name'] = combined_data['name'].apply(remove_punctuation)
 combined_data['name'] = combined_data['name'].apply(remove_stop_words)
 combined_data['name'] = combined_data['name'].apply(to_lower)
 combined_data.head()<set_options>
alldata['NaN_name'].value_counts()
gc.collect()<import_modules>
alldata['name_len_one_or_two'] = alldata['Name'].apply(lambda x: True if len(str(x)) < 3 else False )
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.preprocessing import LabelBinarizer<prepare_x_and_y>
alldata['name_len_one_or_two'].value_counts()
cv = CountVectorizer(min_df=10)
 X_name = cv.fit_transform(combined_data['name'] )<feature_engineering>
train_raw['RescuerID'].value_counts().head(15 )
cv = CountVectorizer()
X_category = cv.fit_transform(combined_data['category_name'] )<feature_engineering>
test_raw['RescuerID'].value_counts().head(15 )
tv = TfidfVectorizer(max_features=55000, ngram_range=(1, 2), stop_words='english')
 X_description = tv.fit_transform(combined_data['item_description'] )<categorify>
top_20_rescuers = list(train_raw['RescuerID'].value_counts() [:20].index)
 top_20_data = train_raw.loc[train_raw['RescuerID'].isin(top_20_rescuers)]
lb = LabelBinarizer(sparse_output=True)
 X_brand = lb.fit_transform(combined_data['brand_name'] )<categorify>
def desc_len_feature(df):
descs = np.stack([item for item in df.Description])
 desc_len = [len(item)for item in descs]
df['desc_length'] = desc_len
def rescue_count_feature(df):
rescuers_df = pd.DataFrame(df.RescuerID)
 rescuer_counts = rescuers_df.apply(pd.value_counts)
 rescuer_counts.columns = ['rescue_count']
rescuer_counts['RescuerID'] = rescuer_counts.index
df = df.merge(rescuer_counts, how='left', on='RescuerID')
 return df
def name_len_feature(df):
names = np.stack([item for item in df.Name])
 name_len = [len(item)for item in names]
df['name_length'] = name_len
return df
X_dummies = csr_matrix(pd.get_dummies(combined_data[['item_condition_id', 'shipping']], sparse=True ).values )<concatenate>
alldata['RescuerID'] = alldata.RescuerID.astype('category')
 alldata['RescuerID'] = alldata.RescuerID.cat.codes
desc_len_feature(alldata)
 name_len_feature(alldata)
 alldata = rescue_count_feature(alldata )
sparse_merge = hstack(( X_dummies, X_description, X_brand, X_category, X_name)).tocsr()<prepare_x_and_y>
def parse_sentiment_files(datatype):
sentiments = {}
for filename in os.listdir('.. /input/' + datatype + '_sentiment'):
with open('.. /input/' + datatype + '_sentiment/' + filename, 'r')as f:
sentiment = json.load(f)
 pet_id = filename.split('.')[0]
sentiments[pet_id] = {}
sentiments[pet_id]['magnitude'] = sentiment['documentSentiment']['magnitude']
sentiments[pet_id]['score'] = sentiment['documentSentiment']['score']
sentiments[pet_id]['language'] = sentiment['language']
return sentiments
def sentiment_features(df, sentiments):
df['lang'] = df['PetID'].apply(lambda x: sentiments[x]['language']
if x in sentiments else 'no')
 df['magnitude'] = df['PetID'].apply(lambda x: sentiments[x]['magnitude']
if x in sentiments else 0)
 df['score'] = df['PetID'].apply(lambda x: sentiments[x]['magnitude']
if x in sentiments else 0)
 return df
X_train_sparse = sparse_merge[:train_size]
X_test = sparse_merge[train_size:]<prepare_x_and_y>
train_sentiment = parse_sentiment_files('train')
 test_sentiment = parse_sentiment_files('test')
 sentiment_features(alldata, train_sentiment);
sentiment_features(alldata, test_sentiment);
kf = KFold(n_splits=3, shuffle=True, random_state=12345)
 y = np.log1p(train_data['price'])
 i = 0;
for train_indicies, valid_indicies in kf.split(X_train_sparse):
X_train, y_train = X_train_sparse[train_indicies], y[train_indicies]
X_valid, y_valid = X_train_sparse[valid_indicies], y[valid_indicies]<set_options>
cols = ['Breed1']
for col in cols:
frequencies = dict(alldata[col].value_counts() /alldata[col].shape[0])
 alldata[col + '_frequency'] = alldata[col].apply(lambda x: frequencies[x] )
gc.collect()<compute_train_metric>
headlines = ["President trump won the election", "The world was shocked",
"Barcelona won the champions league"]
def run_model_advText(model, X_train, y_train, X_valid, y_valid, verbose = False):
model.fit(X_train, y_train)
 preds_valid = model.predict(X_valid)
 mse = mean_squared_error(y_valid,preds_valid)
 r_sq = r2_score(y_valid,preds_valid)
 print("Mean Squared Error Value : "+"{:.2f}".format(mse))
print("R-Squared Value : "+"{:.2f}".format(r_sq))
return model, mse, r_sq<train_model>
vectorizer = CountVectorizer(analyzer='word')
 X = vectorizer.fit_transform(headlines)
 columns = [x for x in vectorizer.get_feature_names() ]
pd.DataFrame(X.todense() , columns=columns )
ridge_reg = linear_model.Ridge(solver = "saga", fit_intercept=False)
 print("Ridge Regression(After advanced Text Pre-processing)")
 print("-----------------------------------------------------")
 model_11, mse_11, r_sq_11 = run_model_advText(ridge_reg, X_train, y_train, X_valid, y_valid )<train_on_grid>
count_matrix = pd.DataFrame(X.todense() , columns=columns)
 tfidf = TfidfTransformer()
inverse_frequencies = tfidf.fit_transform(count_matrix)
 pd.DataFrame(inverse_frequencies.todense() , columns=columns )
lgbm_reg = lightgbm.LGBMRegressor()
print("LGBM Regression(After advanced Text Pre-processing)")
 print("----------------------------------------------------")
 model_22, mse_22, r_sq_22 = run_model_advText(lgbm_reg, X_train, y_train, X_valid, y_valid )<train_model>
def tfidf_features(corpus):
tfv = TfidfVectorizer(min_df=2, max_features=None,
strip_accents='unicode', analyzer='word', token_pattern=r'(?u)\b\w+\b',
ngram_range=(1, 3), use_idf=1, smooth_idf=1, sublinear_tf=1,)
 tfv.fit(list(corpus))
X = tfv.transform(corpus)
 return X
def svd_features(df, freq_matrix, n_comps=1):
svd = TruncatedSVD(n_components=n_comps)
 svd.fit(freq_matrix)
 freq_matrix = svd.transform(freq_matrix)
 freq_matrix = pd.DataFrame(freq_matrix, columns=['svd_{}'.format(i)for i in range(n_comps)])
 df = pd.concat(( df, freq_matrix), axis=1)
 return df
xgb_params = {'n_estimators':500, 'max_depth':8}
xgb_reg = xgboost.XGBRegressor(**xgb_params)
 print("XGB Regression(After advanced Text Pre-processing)")
 print("---------------------------------------------------")
 model_33, mse_33, r_sq_33 = run_model_advText(xgb_reg, X_train, y_train, X_valid, y_valid )<create_dataframe>
X = tfidf_features(alldata['Description']); X
mse_before = [mse_1, mse_2, mse_3]
r_sq_before = [r_sq_1, r_sq_2, r_sq_3]
mse_after = [mse_11, mse_22, mse_33]
r_sq_after = [r_sq_11, r_sq_22, r_sq_33]
model_data = {'Model': ['Ridge','LGBM','XGB'],
'MSE_without_Advanced_TextProcessing': mse_before,
'R_Square_without_Advanced_TextProcessing': r_sq_before,
'MSE_with_Advanced_TextProcessing': mse_after,
'R_Square_with_Advanced_TextProcessing': r_sq_after}
data_compare = pd.DataFrame(model_data)
 fig,(ax[0],ax[1])= plt.subplots(1,2, figsize=(13,4))
ax[0]=data_compare.plot(kind='line', x='Model', y='MSE_without_Advanced_TextProcessing', color='DarkBlue', linewidth=0.7, marker='o', markersize=6, ax=ax[0])
 ax[0]=data_compare.plot(kind='line', x='Model', y='MSE_with_Advanced_TextProcessing', secondary_y=False,color='Red', linewidth=0.7, marker='o', markersize=6, ax=ax[0])
 ax[1]=data_compare.plot(kind='line', x='Model', y='R_Square_without_Advanced_TextProcessing', color='DarkBlue', linewidth=0.7, marker='^', markersize=6, ax=ax[1])
 ax[1]=data_compare.plot(kind='line', x='Model', y='R_Square_with_Advanced_TextProcessing', secondary_y=False,color='Red', linewidth=0.7, marker='^', markersize=6, ax=ax[1])
 ax[0].set_title("Improvement in MSE after Advanced Text Processing", fontsize = 13)
 ax[0].set_ylabel("Mean Square Error")
 ax[1].set_title("Improvement in R-Square after Advanced Text Processing", fontsize = 13)
 ax[1].set_ylabel("R-Square Value")
 plt.tight_layout()
plt.show()<feature_engineering>
alldata=svd_features(alldata, X )
cvectorizer = CountVectorizer(max_features=20000,stop_words='english',lowercase=True)
 cvz = cvectorizer.fit_transform(combined_frac['item_description'])
 lda_model = LatentDirichletAllocation(n_components=10,random_state=12345)
 X_topics = lda_model.fit_transform(cvz)
 n_top_words = 10
topic_summaries = []
topic_word = lda_model.components_
vocab = cvectorizer.get_feature_names()
for i, topic_dist in enumerate(topic_word):
topic_words = np.array(vocab)[np.argsort(topic_dist)][:-(n_top_words+1):-1]
topic_summaries.append(' '.join(topic_words))
print('Topic {}: {}'.format(i, ' | '.join(topic_words)) )<predict_on_test>
train_pet_ids = train_raw.PetID
test_pet_ids = test_raw.PetID
predictions = ridge_reg.predict(X_test)
 submission["price"] = np.expm1(predictions)
 submission.head()<save_to_csv>
alldata = alldata.drop(['Description', 'PetID', 'Name', 'lang', 'RescuerID'], axis=1 )
submission.to_csv("submission.csv", index = False )<install_modules>
train = alldata.loc[alldata['from_dataset'] == 'train']
test = alldata.loc[alldata['from_dataset'] == 'test']
!pip install py7zr<set_options>
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(train['AdoptionSpeed'])
 y = pd.DataFrame(y, columns=['AdoptionSpeed'] )
%matplotlib inline
warnings.filterwarnings("ignore")
 tf.keras.backend.clear_session()<load_pretrained>
train = train.drop(['from_dataset', 'AdoptionSpeed'], axis=1)
 test = test.drop(['from_dataset', 'AdoptionSpeed'], axis=1);
def extract_from_archive() :
if not os.path.exists('/kaggle/working/train/'):
os.makedirs('/kaggle/working/train/')
 shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)
 shutil.unpack_archive('/kaggle/input/mercari-price-suggestion-challenge/train.tsv.7z', '/kaggle/working/train/')
 shutil.unpack_archive('/kaggle/input/mercari-price-suggestion-challenge/test_stg2.tsv.zip', '/kaggle/working/test/')
 extract_from_archive()<feature_engineering>
m = RandomForestClassifier(n_estimators=500, random_state=seed,
max_features='sqrt',
min_samples_leaf=25, n_jobs=-1);
m.fit(train, y);
def split_categories(category):

 try:
sub_cat_1,sub_cat_2,sub_cat_3 = category.split("/")
 return sub_cat_1,sub_cat_2,sub_cat_3
except:
return("No label","No label","No label")
 def create_split_categories(data):

 data['main_category'],data['sub_cat_1'],data['sub_cat_2']=zip(*data['category_name'].\
apply(lambda x: split_categories(x)))
 def log_price(price):
return np.log1p(price)
 def countwords(text):
try:
if text == 'No description yet':
return 0
else:
text = text.lower()
words = [w for w in text.split(" ")]
return len(words)
 except:
return 0
def fill_nan(dataset):

 dataset["item_description"].fillna("No description yet",inplace=True)
 dataset["brand_name"].fillna("missing",inplace=True)
 dataset["category_name"].fillna("missing",inplace=True)
 def get_dummies_item_id_shipping(df):
df['item_condition_id'] = df["item_condition_id"].astype("category")
 df['shipping'] = df["shipping"].astype("category")
 item_id_shipping = csr_matrix(pd.get_dummies(df[['item_condition_id', 'shipping']],\
sparse=True ).values)
 return item_id_shipping
def one_hot_encode(train,test):

 vectorizer = CountVectorizer(token_pattern='.+')
 vectorizer = vectorizer.fit(train['category_name'].values)
 column_cat = vectorizer.transform(test['category_name'].values)
 vectorizer = vectorizer.fit(train['main_category'].values)
 column_mc = vectorizer.transform(test['main_category'].values)
 vectorizer = vectorizer.fit(train['sub_cat_1'].values)
 column_sb1 = vectorizer.transform(test['sub_cat_1'].values)
 vectorizer = vectorizer.fit(train['sub_cat_2'].values)
 column_sb2 = vectorizer.transform(test['sub_cat_2'].values)
 vectorizer = vectorizer.fit(train['brand_name'].astype(str))
brand_encodes = vectorizer.transform(test['brand_name'].astype(str))
print("created OHE columns for main_category,sub_cat_1,sub_cat_2
")
 print(column_cat.shape)
 print(column_mc.shape)
 print(column_sb1.shape)
 print(column_sb2.shape)
 print(brand_encodes.shape)
 print("="*100)
 return column_cat,column_mc,column_sb1,column_sb2,brand_encodes
def rank_category(dataset,column_name):

 counter = dataset[column_name].value_counts().index.values
total = list(dataset[column_name])
 ranked_cat = {}
for i in range(1,len(counter)+1):
ranked_cat.update({counter[i-1] : i})
 return ranked_cat,len(counter)
 def encode_ranked_category(train,test,column):

 train[column] = train[column].astype('category')
 test[column] = test[column].astype('category')
 cat_list = list(train[column].unique())
 ranked_cat_tr,count = rank_category(train,column)
 encoded_col_tr = []
encoded_col_te = []
for category in train[column]:
encoded_col_tr.append(ranked_cat_tr[category])
 for category in test[column]:
if category in cat_list:
encoded_col_te.append(ranked_cat_tr[category])
 else:
encoded_col_te.append(0)
 encoded_col_tr = np.asarray(encoded_col_tr)
 encoded_col_te = np.asarray(encoded_col_te)
 return encoded_col_tr,encoded_col_te,count
def tokenize_text(train,test,column):
global t
t = Tokenizer()
t.fit_on_texts(train[column].str.lower())
 vocab_size = len(t.word_index)+ 1
encoded_text_tr = t.texts_to_sequences(train[column].str.lower())
 encoded_text_te = t.texts_to_sequences(test[column].str.lower())
 return encoded_text_tr,encoded_text_te,vocab_size
def data_gru(train,test):
global max_length,desc_size,name_size
encoded_brand_tr,encoded_brand_te,brand_len = encode_ranked_category(train,test,'brand_name')
 encoded_main_cat_tr,encoded_main_cat_te,main_cat_len = encode_ranked_category(train,test,'main_category')
 encoded_sub_cat_1_tr,encoded_sub_cat_1_te,sub_cat1_len = encode_ranked_category(train,test,'sub_cat_1')
 encoded_sub_cat_2_tr,encoded_sub_cat_2_te,sub_cat2_len = encode_ranked_category(train,test,'sub_cat_2')
 tokenized_desc_tr,tokenized_desc_te,desc_size = tokenize_text(train,test,'item_description')
 tokenized_name_tr,tokenized_name_te,name_size = tokenize_text(train,test,'name')
 max_length = 160
desc_tr_padded = pad_sequences(tokenized_desc_tr, maxlen=max_length, padding='post')
 desc_te_padded = pad_sequences(tokenized_desc_te, maxlen=max_length, padding='post')
 del tokenized_desc_tr,tokenized_desc_te
name_tr_padded = pad_sequences(tokenized_name_tr, maxlen=10, padding='post')
 name_te_padded = pad_sequences(tokenized_name_te, maxlen=10, padding='post')
 del tokenized_name_tr,tokenized_name_te
gc.collect()
train_inputs = [name_tr_padded,desc_tr_padded,encoded_brand_tr.reshape(-1,1),\
encoded_main_cat_tr.reshape(-1,1),encoded_sub_cat_1_tr.reshape(-1,1),\
encoded_sub_cat_2_tr.reshape(-1,1),train['shipping'],\
train['item_condition_id'],train['wc_desc'],\
train['wc_name']]
test_inputs = [name_te_padded,desc_te_padded,encoded_brand_te.reshape(-1,1),\
encoded_main_cat_te.reshape(-1,1),encoded_sub_cat_1_te.reshape(-1,1),\
encoded_sub_cat_2_te.reshape(-1,1),test['shipping'],\
test['item_condition_id'],test['wc_desc'],\
test['wc_name']]
item_condition_counter = train['item_condition_id'].value_counts().index.values
list_var = [brand_len,main_cat_len,sub_cat1_len,sub_cat2_len,len(item_condition_counter)]
return train_inputs,test_inputs,list_var
def construct_GRU(train,var_list,drop_out_list):
input_name = tf.keras.layers.Input(shape=(10,), name='name')
 embedding_name = tf.keras.layers.Embedding(name_size, 20 )(input_name)
 gru_name = tf.keras.layers.GRU(8 )(embedding_name)
 input_desc = tf.keras.layers.Input(shape=(max_length,), name='desc')
 embedding_desc = tf.keras.layers.Embedding(desc_size, 60 )(input_desc)
 gru_desc = tf.keras.layers.GRU(16 )(embedding_desc)
 input_brand = tf.keras.layers.Input(shape=(1,), name='brand')
 embedding_brand = tf.keras.layers.Embedding(var_list[0] + 1, 10 )(input_brand)
 flatten1 = tf.keras.layers.Flatten()(embedding_brand)
 input_cat = tf.keras.layers.Input(shape=(1,), name='main_cat')
 Embed_cat = tf.keras.layers.Embedding(var_list[1] + 1, \
10,input_length=1 )(input_cat)
 flatten2 = tf.keras.layers.Flatten()(Embed_cat)
 input_subcat1 = tf.keras.layers.Input(shape=(1,), name='subcat1')
 Embed_subcat1 = tf.keras.layers.Embedding(var_list[2] + 1, \
10,input_length=1 )(input_subcat1)
 flatten3 = tf.keras.layers.Flatten()(Embed_subcat1)
 input_subcat2 = tf.keras.layers.Input(shape=(1,), name='subcat2')
 Embed_subcat2 = tf.keras.layers.Embedding(var_list[3] + 1, \
10,input_length=1 )(input_subcat2)
 flatten4 = tf.keras.layers.Flatten()(Embed_subcat2)
 input_shipping = tf.keras.layers.Input(shape=(1,), name='shipping')
 input_item = tf.keras.layers.Input(shape=(1,), name='item_condition_id')
 Embed_item = tf.keras.layers.Embedding(var_list[4] + 1, \
5,input_length=1 )(input_item)
 flatten5 = tf.keras.layers.Flatten()(Embed_item)
 desc_len_input = tf.keras.layers.Input(shape=(1,), name='description_length')
 desc_len_embd = tf.keras.layers.Embedding(DESC_LEN,5 )(desc_len_input)
 flatten6 = tf.keras.layers.Flatten()(desc_len_embd)
 name_len_input = tf.keras.layers.Input(shape=(1,), name='name_length')
 name_len_embd = tf.keras.layers.Embedding(NAME_LEN,5 )(name_len_input)
 flatten7 = tf.keras.layers.Flatten()(name_len_embd)
 concat_layer = tf.keras.layers.concatenate(inputs=[gru_name,gru_desc,flatten1,flatten2,flatten3,flatten4,input_shipping,flatten5,\
flatten6,flatten7],name="concatenate")
 Dense_layer1 = tf.keras.layers.Dense(units=512,activation='relu',kernel_initializer='he_normal',\
name="Dense_l" )(concat_layer)
 dropout_1 = tf.keras.layers.Dropout(drop_out_list[0],name='dropout_1' )(Dense_layer1)
 batch_n1 = tf.keras.layers.BatchNormalization()(dropout_1)
 Dense_layer2 = tf.keras.layers.Dense(units=256,activation='relu',kernel_initializer='he_normal',\
name="Dense_2" )(batch_n1)
 dropout_2 = tf.keras.layers.Dropout(drop_out_list[1],name='dropout_2' )(Dense_layer2)
 batch_n2 = tf.keras.layers.BatchNormalization()(dropout_2)
 Dense_layer3 = tf.keras.layers.Dense(units=128,activation='relu',kernel_initializer='he_normal',\
name="Dense_3" )(batch_n2)
 dropout_3 = tf.keras.layers.Dropout(drop_out_list[2],name='dropout_3' )(Dense_layer3)
 Dense_layer4 = tf.keras.layers.Dense(units=64,activation='relu',kernel_initializer='he_normal',\
name="Dense_4" )(dropout_3)
 dropout_4 = tf.keras.layers.Dropout(drop_out_list[3],name='dropout_4' )(Dense_layer4)
 final_output = tf.keras.layers.Dense(units=1,activation='linear',name='output_layer' )(dropout_4)
 model = tf.keras.Model(inputs=[input_name,input_desc,input_brand,input_cat,input_subcat1,input_subcat2,\
input_shipping,input_item,desc_len_input,name_len_input],
outputs=[final_output])
 print(model.summary())
 return model
def compile_predict_GRU(train_input,test_input,y_train,variable_list,drop_list):
filepath="GRU_lr-{epoch:03d}-{val_loss:.3f}.hdf5"
model_checkpoint = ModelCheckpoint(filepath, monitor='val_loss', mode='min', save_best_only=True)
 def scheduler(epoch):
if epoch < 2:
return 0.005
else:
return 0.001
lr_schedule = tf.keras.callbacks.LearningRateScheduler(scheduler)
 call_backs = [model_checkpoint,lr_schedule]
x_train = train_input
model= construct_GRU(x_train,variable_list,drop_list)
 model.compile(optimizer='adam', loss='mean_squared_error')
 model.fit(x_train, y_train, epochs=3,batch_size=2**10, callbacks=call_backs,validation_split=0.1)
 pred_on_test = model.predict(test_input,batch_size = 2**10,verbose = 1)
 return pred_on_test
def get_tensorboard_ready() :
! rm -rf./logs/
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
print(logdir)
 return logdir
def hyperparameter_tuning_random(x,y,model_estimator,param_dict,cv_no):
start = time.time()
hyper_tuned = GridSearchCV(estimator = model_estimator, param_grid = param_dict,\
return_train_score=True, scoring = 'neg_mean_squared_error',\
cv = cv_no, \
verbose=2, n_jobs = -1)
 hyper_tuned.fit(x,y)
 print("
")
 print('Time taken for hyperparameter tuning is {} sec
'.format(time.time() -start))
print('The best parameters_: {}'.format(hyper_tuned.best_params_))
return hyper_tuned.best_params_
def rmsle_compute(y_true, y_pred):
assert len(y_true)== len(y_pred)
 score = np.sqrt(np.mean(np.power(np.log1p(y_pred)- np.log1p(y_true), 2)))
 return score
def scale_back(x):

 x= np.expm1(y_scalar.inverse_transform(x.reshape(-1,1)) [:,0])
 return x
@contextmanager
def timer(name):
t0 = time.time()
yield
print(f'[{name}] done in {time.time() - t0:.0f} s')
<load_from_csv>
def rf_feature_importance(df, m):
df = pd.DataFrame({'cols' : df.columns, 'imp' : m.feature_importances_} ).sort_values('imp', ascending=False)
 return df
def function_1(x):
gc.collect()
train = pd.read_csv('/kaggle/working/train/train.tsv',sep='\t')
 test = x
print("Finished loading the files....
")
 print("train: {0}
test: {1}
".format(train.shape,test.shape))
train = train.drop(train[(train.price < 3.0)].index)
 fill_nan(train)
 fill_nan(test)
 print("filled nan
")
 train['wc_desc'] = train['item_description'].apply(lambda x: countwords(x))
test['wc_desc'] = test['item_description'].apply(lambda x: countwords(x))
train['wc_name'] = train['name'].apply(lambda x: countwords(x))
test['wc_name'] = test['name'].apply(lambda x: countwords(x))
create_split_categories(train)
 create_split_categories(test)
 print("Completed data cleaning and preprocessing
")
 print("shape of train: {}".format(train.shape))
print("shape of test: {}".format(test.shape))
global y_scalar,DESC_LEN,NAME_LEN
y_scalar = StandardScaler()
y_train = y_scalar.fit_transform(log_price(train['price'] ).values.reshape(-1, 1))
DESC_LEN = train.wc_desc.max() + 1
NAME_LEN = train.wc_name.max() + 1
train_inputs,test_inputs,list_var = data_gru(train,test)
 dropout_list = [0.10,0.10,0.20,0.20]
te_preds_m1 = compile_predict_GRU(train_inputs,test_inputs,y_train,list_var,dropout_list)
 dropout_list = [0.10,0.20,0.30,0.40]
te_preds_m2 = compile_predict_GRU(train_inputs,test_inputs,y_train,list_var,dropout_list)
 del train_inputs,test_inputs,list_var
gc.collect()
y_hats = np.array([te_preds_m1,te_preds_m2])
 mean_preds = np.mean(y_hats, axis=0)
 return scale_back(mean_preds )<save_to_csv>
m = RandomForestClassifier(n_estimators=500, random_state=seed,
max_features='sqrt',
min_samples_leaf=1, n_jobs=-1)
 test_preds = np.zeros(test.shape[0])
 results=[]
n_folds = 4
cv = StratifiedKFold(n_splits=n_folds, random_state=seed)
 for(train_idx, valid_idx)in cv.split(train,y):
m.fit(train.iloc[train_idx], y.iloc[train_idx])
 score = metrics.cohen_kappa_score(y.loc[valid_idx], m.predict(train.iloc[valid_idx]), weights='quadratic')
 results.append(score)
 y_test = m.predict(test)
 test_preds += y_test.reshape(-1)/n_folds
mean = np.mean(results)
 std = np.std(results )
test = pd.read_csv('/kaggle/working/test/test_stg2.tsv',sep='\t')
 test["price"] = function_1(test)
 sub = test[["test_id", "price"]]
sub.to_csv("submission.csv", index = False )<save_to_csv>
print(f'Mean kappa score: {mean} with std: {std}' )
data = pd.read_csv('/kaggle/input/submission/prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )<save_to_csv>
df_sub = pd.DataFrame({'PetID' : test_pet_ids})
 df_sub['AdoptionSpeed'] = test_preds
df_sub['AdoptionSpeed'] = df_sub['AdoptionSpeed'].astype(int)
 df_sub.head()
<save_to_csv><EOS> 
df_sub.to_csv(f'submission.csv', index=False )