Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
import pandas as pd
import numpy as np
cols = set(merge.columns.values)
 basic_cols = {'name', 'item_condition_id', 'brand_name',
'shipping', 'item_description', 'gencat_name',
'subcat1_name', 'subcat2_name', 'name_first', 'is_train'}
cols_to_normalize = cols - basic_cols - {'price_in_name'}
other_cols = basic_cols | {'price_in_name'}<split>
train = pd.read_csv(r'/kaggle/input/titanic/train.csv')
 test = pd.read_csv(r'/kaggle/input/titanic/test.csv' )
df_test = merge.loc[merge['is_train'] == 0]
df_train = merge.loc[merge['is_train'] == 1]
del merge
gc.collect()
df_test = df_test.drop(['is_train'], axis=1)
 df_train = df_train.drop(['is_train'], axis=1)
 if SUBMIT_MODE:
y_train = y
del y
gc.collect()
else:
df_train, df_test, y_train, y_test = train_test_split(df_train, y, test_size=0.2, random_state=144)
 print('[{}] Splitting completed.'.format(time.time() - start_time))<drop_column>
train.isna().sum()
wb = wordbatch.WordBatch(normalize_text, extractor=(WordBag, {"hash_ngrams": 2,
"hash_ngrams_weights": [1.5, 1.0],
"hash_size": 2 ** 29,
"norm": None,
"tf": 'binary',
"idf": None,
}), procs=8)
 wb.dictionary_freeze = True
X_name_train = wb.fit_transform(df_train['name'])
 X_name_test = wb.transform(df_test['name'])
 del(wb)
 mask = np.where(X_name_train.getnnz(axis=0)> 3)[0]
X_name_train = X_name_train[:, mask]
X_name_test = X_name_test[:, mask]
print('[{}] Vectorize `name` completed.'.format(time.time() - start_time))<drop_column>
dfs = [train ,test]
for df in dfs:
df['Age'].fillna(df['Age'].median() , inplace = True )
wb = wordbatch.WordBatch(normalize_text, extractor=(WordBag, {"hash_ngrams": 2,
"hash_ngrams_weights": [1.0, 1.0],
"hash_size": 2 ** 28,
"norm": "l2",
"tf": 1.0,
"idf": None}), procs=8)
 wb.dictionary_freeze = True
X_description_train = wb.fit_transform(df_train['item_description'])
 X_description_test = wb.transform(df_test['item_description'])
 del(wb)
 mask = np.where(X_description_train.getnnz(axis=0)> 3)[0]
X_description_train = X_description_train[:, mask]
X_description_test = X_description_test[:, mask]
print('[{}] Vectorize `item_description` completed.'.format(time.time() - start_time))<split>
train.isna().sum()
X_train_1, X_train_2, y_train_1, y_train_2 = train_test_split(X_description_train, y_train,
test_size = 0.5,
shuffle = False)
 print('[{}] Finished splitting'.format(time.time() - start_time))
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3.3)
 model.fit(X_train_1, y_train_1)
 print('[{}] Finished to train desc ridge(1)'.format(time.time() - start_time))
desc_ridge_preds1 = model.predict(X_train_2)
 desc_ridge_preds1f = model.predict(X_description_test)
 print('[{}] Finished to predict desc ridge(1)'.format(time.time() - start_time))
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3.3)
 model.fit(X_train_2, y_train_2)
 print('[{}] Finished to train desc ridge(2)'.format(time.time() - start_time))
desc_ridge_preds2 = model.predict(X_train_1)
 desc_ridge_preds2f = model.predict(X_description_test)
 print('[{}] Finished to predict desc ridge(2)'.format(time.time() - start_time))
desc_ridge_preds_oof = np.concatenate(( desc_ridge_preds2, desc_ridge_preds1), axis=0)
 desc_ridge_preds_test =(desc_ridge_preds1f + desc_ridge_preds2f)/ 2.0
print('RMSLE OOF: {}'.format(rmse(desc_ridge_preds_oof, y_train)))
 if not SUBMIT_MODE:
print('RMSLE TEST: {}'.format(rmse(desc_ridge_preds_test, y_test)))
 X_train_1, X_train_2, y_train_1, y_train_2 = train_test_split(X_name_train, y_train,
test_size = 0.5,
shuffle = False)
 print('[{}] Finished splitting'.format(time.time() - start_time))<predict_on_test>
train['Cabin'].value_counts()
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3.3)
 model.fit(X_train_1, y_train_1)
 print('[{}] Finished to train name ridge(1)'.format(time.time() - start_time))
name_ridge_preds1 = model.predict(X_train_2)
 name_ridge_preds1f = model.predict(X_name_test)
 print('[{}] Finished to predict name ridge(1)'.format(time.time() - start_time))
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3.3)
 model.fit(X_train_2, y_train_2)
 print('[{}] Finished to train name ridge(2)'.format(time.time() - start_time))
name_ridge_preds2 = model.predict(X_train_1)
 name_ridge_preds2f = model.predict(X_name_test)
 print('[{}] Finished to predict name ridge(2)'.format(time.time() - start_time))
name_ridge_preds_oof = np.concatenate(( name_ridge_preds2, name_ridge_preds1), axis=0)
 name_ridge_preds_test =(name_ridge_preds1f + name_ridge_preds2f)/ 2.0
print('RMSLE OOF: {}'.format(rmse(name_ridge_preds_oof, y_train)))
 if not SUBMIT_MODE:
print('RMSLE TEST: {}'.format(rmse(name_ridge_preds_test, y_test)) )<drop_column>
letters = []
for i in cabins:
letter= i[0]
letters.append(letter )
del X_train_1
del X_train_2
del y_train_1
del y_train_2
del name_ridge_preds1
del name_ridge_preds1f
del name_ridge_preds2
del name_ridge_preds2f
del desc_ridge_preds1
del desc_ridge_preds1f
del desc_ridge_preds2
del desc_ridge_preds2f
gc.collect()
print('[{}] Finished garbage collection'.format(time.time() - start_time))<categorify>
train['Cabin'] = letters
lb = LabelBinarizer(sparse_output=True)
 X_brand_train = lb.fit_transform(df_train['brand_name'])
 X_brand_test = lb.transform(df_test['brand_name'])
 print('[{}] Finished label binarize `brand_name`'.format(time.time() - start_time))<categorify>
letters = []
for i in cabins:
letter = i[0]
letters.append(letter )
X_cat_train = lb.fit_transform(df_train['gencat_name'])
 X_cat_test = lb.transform(df_test['gencat_name'])
 X_cat1_train = lb.fit_transform(df_train['subcat1_name'])
 X_cat1_test = lb.transform(df_test['subcat1_name'])
 X_cat2_train = lb.fit_transform(df_train['subcat2_name'])
 X_cat2_test = lb.transform(df_test['subcat2_name'])
 print('[{}] Finished label binarize categories'.format(time.time() - start_time))<categorify>
test['Cabin'] = letters
X_dummies_train = csr_matrix(
 pd.get_dummies(df_train[list(cols -(basic_cols - {'item_condition_id', 'shipping'})) ],
sparse=True ).values)
 print('[{}] Create dummies completed - train'.format(time.time() - start_time))
X_dummies_test = csr_matrix(
 pd.get_dummies(df_test[list(cols -(basic_cols - {'item_condition_id', 'shipping'})) ],
sparse=True ).values)
 print('[{}] Create dummies completed - test'.format(time.time() - start_time))<concatenate>
train['Embarked'].value_counts()
sparse_merge_train = hstack(( X_dummies_train, X_description_train, X_brand_train, X_cat_train,
X_cat1_train, X_cat2_train, X_name_train)).tocsr()
del X_description_train, lb, X_name_train, X_dummies_train
gc.collect()
print('[{}] Create sparse merge train completed'.format(time.time() - start_time))
sparse_merge_test = hstack(( X_dummies_test, X_description_test, X_brand_test, X_cat_test,
X_cat1_test, X_cat2_test, X_name_test)).tocsr()
del X_description_test, X_name_test, X_dummies_test
gc.collect()
print('[{}] Create sparse merge test completed'.format(time.time() - start_time))<train_model>
len(train[train['Pclass'] == 1]), len(train[train['Pclass'] == 2]), len(train[train['Pclass'] == 3] )
if SUBMIT_MODE:
iters = 3
else:
iters = 1
rounds = 3
model = FM_FTRL(alpha=0.035, beta=0.001, L1=0.00001, L2=0.15, D=sparse_merge_train.shape[1],
alpha_fm=0.05, L2_fm=0.0, init_fm=0.01,
D_fm=100, e_noise=0, iters=iters, inv_link="identity", threads=4)
 if SUBMIT_MODE:
model.fit(sparse_merge_train, y_train)
 print('[{}] Train FM completed'.format(time.time() - start_time))
predsFM = model.predict(sparse_merge_test)
 print('[{}] Predict FM completed'.format(time.time() - start_time))
else:
for i in range(rounds):
model.fit(sparse_merge_train, y_train)
 predsFM = model.predict(sparse_merge_test)
 print('[{}] Iteration {}/{} -- RMSLE: {}'.format(time.time() - start_time, i + 1, rounds, rmse(predsFM, y_test)) )<normalization>
percentages = []
first = 136 / 216
second = 87/ 184
third = 119/491
percentages.append(first)
 percentages.append(second)
 percentages.append(third )
del model
gc.collect()
if not SUBMIT_MODE:
print("FM_FTRL dev RMSLE:", rmse(predsFM, y_test))
fselect = SelectKBest(f_regression, k=48000)
 train_features = fselect.fit_transform(sparse_merge_train, y_train)
 test_features = fselect.transform(sparse_merge_test)
 print('[{}] Select best completed'.format(time.time() - start_time))
del sparse_merge_train
del sparse_merge_test
gc.collect()
print('[{}] Garbage collection'.format(time.time() - start_time))<feature_engineering>
percents = pd.DataFrame(percentages)
 percents.index+=1
tv = TfidfVectorizer(max_features=250000,
ngram_range=(1, 3),
stop_words=None)
 X_name_train = tv.fit_transform(df_train['name'])
 print('[{}] Finished TFIDF vectorize `name`(1/2)'.format(time.time() - start_time))
X_name_test = tv.transform(df_test['name'])
 print('[{}] Finished TFIDF vectorize `name`(2/2)'.format(time.time() - start_time))
tv = TfidfVectorizer(max_features=500000,
ngram_range=(1, 3),
stop_words=None)
 X_description_train = tv.fit_transform(df_train['item_description'])
 print('[{}] Finished TFIDF vectorize `item_description`(1/2)'.format(time.time() - start_time))
X_description_test = tv.transform(df_test['item_description'])
 print('[{}] Finished TFIDF vectorize `item_description`(2/2)'.format(time.time() - start_time))
X_dummies_train = csr_matrix(
 pd.get_dummies(df_train[['item_condition_id', 'shipping']], sparse=True ).values)
 X_dummies_test = csr_matrix(
 pd.get_dummies(df_test[['item_condition_id', 'shipping']], sparse=True ).values)
 sparse_merge_train = hstack(( X_description_train, X_brand_train, X_cat_train,
X_cat1_train, X_cat2_train, X_name_train)).tocsr()
del X_dummies_train, X_description_train, X_brand_train, X_cat_train
del X_cat1_train, X_cat2_train, X_name_train
gc.collect()
print('[{}] Create sparse merge train completed'.format(time.time() - start_time))
sparse_merge_test = hstack(( X_description_test, X_brand_test, X_cat_test,
X_cat1_test, X_cat2_test, X_name_test)).tocsr()
del X_dummies_test, X_description_test, X_brand_test, X_cat_test
del X_cat1_test, X_cat2_test, X_name_test
gc.collect()
print('[{}] Create sparse merge test completed'.format(time.time() - start_time))
X_train_1, X_train_2, y_train_1, y_train_2 = train_test_split(sparse_merge_train, y_train,
test_size = 0.5,
shuffle = False)
 print('[{}] Finished splitting'.format(time.time() - start_time))
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3.3)
 model.fit(X_train_1, y_train_1)
 print('[{}] Finished to train ridge(1)'.format(time.time() - start_time))
ridge_preds1 = model.predict(X_train_2)
 ridge_preds1f = model.predict(sparse_merge_test)
 print('[{}] Finished to predict ridge(1)'.format(time.time() - start_time))
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3.3)
 model.fit(X_train_2, y_train_2)
 print('[{}] Finished to train ridge(2)'.format(time.time() - start_time))
ridge_preds2 = model.predict(X_train_1)
 ridge_preds2f = model.predict(sparse_merge_test)
 print('[{}] Finished to predict ridge(2)'.format(time.time() - start_time))
ridge_preds_oof = np.concatenate(( ridge_preds2, ridge_preds1), axis=0)
 ridge_preds_test =(ridge_preds1f + ridge_preds2f)/ 2.0
print('RMSLE OOF: {}'.format(rmse(ridge_preds_oof, y_train)))
 if not SUBMIT_MODE:
print('RMSLE TEST: {}'.format(rmse(ridge_preds_test, y_test)))
 model = MultinomialNB(alpha=0.01)
 model.fit(X_train_1, y_train_1 >= 4)
 print('[{}] Finished to train MNB(1)'.format(time.time() - start_time))
mnb_preds1 = model.predict_proba(X_train_2)[:, 1]
mnb_preds1f = model.predict_proba(sparse_merge_test)[:, 1]
print('[{}] Finished to predict MNB(1)'.format(time.time() - start_time))
model = MultinomialNB(alpha=0.01)
 model.fit(X_train_2, y_train_2 >= 4)
 print('[{}] Finished to train MNB(2)'.format(time.time() - start_time))
mnb_preds2 = model.predict_proba(X_train_1)[:, 1]
mnb_preds2f = model.predict_proba(sparse_merge_test)[:, 1]
print('[{}] Finished to predict MNB(2)'.format(time.time() - start_time))
mnb_preds_oof = np.concatenate(( mnb_preds2, mnb_preds1), axis=0)
 mnb_preds_test =(mnb_preds1f + mnb_preds2f)/ 2.0
del ridge_preds1
del ridge_preds1f
del ridge_preds2
del ridge_preds2f
del mnb_preds1
del mnb_preds1f
del mnb_preds2
del mnb_preds2f
del X_train_1
del X_train_2
del y_train_1
del y_train_2
del sparse_merge_train
del sparse_merge_test
del model
gc.collect()
print('[{}] Finished garbage collection'.format(time.time() - start_time))
df_train['ridge'] = ridge_preds_oof
df_train['name_ridge'] = name_ridge_preds_oof
df_train['desc_ridge'] = desc_ridge_preds_oof
df_train['mnb'] = mnb_preds_oof
df_test['ridge'] = ridge_preds_test
df_test['name_ridge'] = name_ridge_preds_test
df_test['desc_ridge'] = desc_ridge_preds_test
df_test['mnb'] = mnb_preds_test
print('[{}] Finished adding submodels'.format(time.time() - start_time))
f_cats = ['brand_name', 'gencat_name', 'subcat1_name', 'subcat2_name', 'name_first']
target_encode = TargetEncoder(min_samples_leaf=100, smoothing=10, noise_level=0.01,
keep_original=True, cols=f_cats)
 df_train, df_test = target_encode.encode(df_train, df_test, y_train)
 print('[{}] Finished target encoding'.format(time.time() - start_time))
df_train.drop(f_cats, axis=1, inplace=True)
 df_test.drop(f_cats, axis=1, inplace=True)
 del mnb_preds_oof
del mnb_preds_test
del ridge_preds_oof
del ridge_preds_test
gc.collect()
print('[{}] Finished garbage collection'.format(time.time() - start_time))
cols = ['gencat_name_te', 'brand_name_te', 'subcat1_name_te', 'subcat2_name_te',
'name_first_te', 'mnb', 'desc_ridge', 'name_ridge', 'ridge']
train_dummies = csr_matrix(df_train[cols].values)
 print('[{}] Finished dummyizing model 1/5'.format(time.time() - start_time))
test_dummies = csr_matrix(df_test[cols].values)
 print('[{}] Finished dummyizing model 2/5'.format(time.time() - start_time))
del df_train
del df_test
gc.collect()
print('[{}] Finished dummyizing model 3/5'.format(time.time() - start_time))
train_features = hstack(( train_features, train_dummies)).tocsr()
print('[{}] Finished dummyizing model 4/5'.format(time.time() - start_time))
test_features = hstack(( test_features, test_dummies)).tocsr()
print('[{}] Finished dummyizing model 5/5'.format(time.time() - start_time))
d_train = lgb.Dataset(train_features, label=y_train)
 del train_features; gc.collect()
if SUBMIT_MODE:
watchlist = [d_train]
else:
d_valid = lgb.Dataset(test_features, label=y_test)
 watchlist = [d_train, d_valid]
params = {
'learning_rate': 0.15,
'application': 'regression',
'max_depth': 13,
'num_leaves': 400,
'verbosity': -1,
'metric': 'RMSE',
'data_random_seed': 1,
'bagging_fraction': 0.8,
'feature_fraction': 0.6,
'nthread': 4,
'lambda_l1': 10,
'lambda_l2': 10
}
print('[{}] Finished compiling LGB'.format(time.time() - start_time))
modelL = lgb.train(params,
train_set=d_train,
num_boost_round=1350,
valid_sets=watchlist,
verbose_eval=50)
 predsL = modelL.predict(test_features)
 predsL[predsL < 0] = 0
if not SUBMIT_MODE:
print("LGB RMSLE:", rmse(predsL, y_test))
del d_train
del modelL
if not SUBMIT_MODE:
del d_valid
gc.collect()
preds_final = predsFM * 0.33 + predsL * 0.67
if not SUBMIT_MODE:
print('Final RMSE: ', rmse(preds_final, y_test))
if SUBMIT_MODE:
preds_final = np.expm1(preds_final)
 submission['price'] = preds_final
submission.to_csv('lgb_and_fm.csv', index=False)
 print('[{}] Writing submission done'.format(time.time() - start_time))<compute_test_metric>
train['Family'] = train.apply(lambda x: x['SibSp'] + x['Parch'], axis = 1)
 test['Family'] = test.apply(lambda x: x['SibSp'] + x['Parch'], axis = 1 )
def rmsle(y, y_pred):
assert len(y)== len(y_pred)
 to_sum = [(math.log(y_pred[i] + 1)- math.log(y[i] + 1)) ** 2.0 for i,pred in enumerate(y_pred)]
return(sum(to_sum)*(1.0/len(y)))** 0.5 
<load_from_csv>
train.drop(['SibSp', 'Parch', 'Name', 'Ticket'], axis = 1, inplace = True)
 test.drop(['SibSp', 'Parch', 'Name', 'Ticket'], axis = 1, inplace = True )
print("Loading data...")
 train = pd.read_table(".. /input/train.tsv")
 test = pd.read_table(".. /input/test.tsv")
 print(train.shape)
 print(test.shape )<categorify>
test.isna().sum()
print("Handling missing values...")
 def handle_missing(dataset):
dataset.category_name.fillna(value="missing", inplace=True)
 dataset.brand_name.fillna(value="missing", inplace=True)
 dataset.item_description.fillna(value="missing", inplace=True)
 return(dataset)
 train = handle_missing(train)
 test = handle_missing(test)
 print(train.shape)
 print(test.shape )<categorify>
test['Fare'].fillna(test['Fare'].median() , inplace = True )
print("Handling categorical variables...")
 le = LabelEncoder()
le.fit(np.hstack([train.category_name, test.category_name]))
train.category_name = le.transform(train.category_name)
 test.category_name = le.transform(test.category_name)
 le.fit(np.hstack([train.brand_name, test.brand_name]))
train.brand_name = le.transform(train.brand_name)
 test.brand_name = le.transform(test.brand_name)
 del le
train.head(3)
<feature_engineering>
train_df = pd.get_dummies(train)
 test_df = pd.get_dummies(test )
print("Text to seq process...")
 raw_text = np.hstack([train.item_description.str.lower() , train.name.str.lower() ])
 print(" Fitting tokenizer...")
 tok_raw = Tokenizer()
tok_raw.fit_on_texts(raw_text)
 print(" Transforming text to seq...")
 train["seq_item_description"] = tok_raw.texts_to_sequences(train.item_description.str.lower())
 test["seq_item_description"] = tok_raw.texts_to_sequences(test.item_description.str.lower())
 train["seq_name"] = tok_raw.texts_to_sequences(train.name.str.lower())
 test["seq_name"] = tok_raw.texts_to_sequences(test.name.str.lower())
 train.head(3 )<define_variables>
train_df.drop('PassengerId', axis = 1, inplace = True )
MAX_NAME_SEQ = 10
MAX_ITEM_DESC_SEQ = 75
MAX_TEXT = np.max([np.max(train.seq_name.max())
 , np.max(test.seq_name.max())
 , np.max(train.seq_item_description.max())
 , np.max(test.seq_item_description.max())])+2
MAX_CATEGORY = np.max([train.category_name.max() , test.category_name.max() ])+1
MAX_BRAND = np.max([train.brand_name.max() , test.brand_name.max() ])+1
MAX_CONDITION = np.max([train.item_condition_id.max() , test.item_condition_id.max() ])+1<normalization>
y = train_df['Survived']
train_df.drop('Survived', axis = 1, inplace = True)
 train_df.drop('Cabin_T', axis = 1, inplace = True)
 test_df.drop('PassengerId', axis = 1, inplace = True )
train["target"] = np.log(train.price+1)
 target_scaler = MinMaxScaler(feature_range=(-1, 1))
train["target"] = target_scaler.fit_transform(train.target.reshape(-1,1))
pd.DataFrame(train.target ).hist()<split>
X_test = test_df
X_train = train_df
dtrain, dvalid = train_test_split(train, random_state=123, train_size=0.99)
 print(dtrain.shape)
 print(dvalid.shape )<categorify>
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
def get_keras_data(dataset):
X = {
'name': pad_sequences(dataset.seq_name, maxlen=MAX_NAME_SEQ)
 ,'item_desc': pad_sequences(dataset.seq_item_description, maxlen=MAX_ITEM_DESC_SEQ)
 ,'brand_name': np.array(dataset.brand_name)
 ,'category_name': np.array(dataset.category_name)
 ,'item_condition': np.array(dataset.item_condition_id)
 ,'num_vars': np.array(dataset[["shipping"]])
 }
return X
X_train = get_keras_data(dtrain)
 X_valid = get_keras_data(dvalid)
 X_test = get_keras_data(test )<compute_train_metric>
rfc = RandomForestClassifier()
def get_callbacks(filepath, patience=2):
es = EarlyStopping('val_loss', patience=patience, mode="min")
 msave = ModelCheckpoint(filepath, save_best_only=True)
 return [es, msave]
def rmsle_cust(y_true, y_pred):
first_log = K.log(K.clip(y_pred, K.epsilon() , None)+ 1.)
second_log = K.log(K.clip(y_true, K.epsilon() , None)+ 1.)
return K.sqrt(K.mean(K.square(first_log - second_log), axis=-1))
def get_model() :
dr_r = 0.1
name = Input(shape=[X_train["name"].shape[1]], name="name")
 item_desc = Input(shape=[X_train["item_desc"].shape[1]], name="item_desc")
 brand_name = Input(shape=[1], name="brand_name")
 category_name = Input(shape=[1], name="category_name")
 item_condition = Input(shape=[1], name="item_condition")
 num_vars = Input(shape=[X_train["num_vars"].shape[1]], name="num_vars")
 emb_name = Embedding(MAX_TEXT, 50 )(name)
 emb_item_desc = Embedding(MAX_TEXT, 50 )(item_desc)
 emb_brand_name = Embedding(MAX_BRAND, 10 )(brand_name)
 emb_category_name = Embedding(MAX_CATEGORY, 10 )(category_name)
 emb_item_condition = Embedding(MAX_CONDITION, 5 )(item_condition)
 rnn_layer1 = GRU(16 )(emb_item_desc)
 rnn_layer2 = GRU(8 )(emb_name)
 main_l = concatenate([
Flatten()(emb_brand_name)
 , Flatten()(emb_category_name)
 , Flatten()(emb_item_condition)
 , rnn_layer1
, rnn_layer2
, num_vars
])
 main_l = Dropout(dr_r )(Dense(128 )(main_l))
main_l = Dropout(dr_r )(Dense(64 )(main_l))
output = Dense(1, activation="linear" )(main_l)
 model = Model([name, item_desc, brand_name
, category_name, item_condition, num_vars], output)
 model.compile(loss="mse", optimizer="adam", metrics=["mae", rmsle_cust])
 return model
model = get_model()
model.summary() 
<train_model>
param_grid = {
'n_estimators': [200, 500, 1000],
'max_features': ['auto'],
'max_depth': [6, 7, 8],
'criterion': ['entropy']
}
BATCH_SIZE = 20000
epochs = 5
model = get_model()
model.fit(X_train, dtrain.target, epochs=epochs, batch_size=BATCH_SIZE
, validation_data=(X_valid, dvalid.target)
 , verbose=1 )<compute_train_metric>
CV = GridSearchCV(estimator = rfc, param_grid = param_grid, cv = 5)
 CV.fit(X_train, y)
 CV.best_estimator_
val_preds = model.predict(X_valid)
 val_preds = target_scaler.inverse_transform(val_preds)
 val_preds = np.exp(val_preds)+1
y_true = np.array(dvalid.price.values)
 y_pred = val_preds[:,0]
v_rmsle = rmsle(y_true, y_pred)
 print(" RMSLE error on dev test: "+str(v_rmsle))<predict_on_test>
rfc = RandomForestClassifier(criterion = 'entropy', max_depth = 8, n_estimators = 500, random_state = 42 )
preds = model.predict(X_test, batch_size=BATCH_SIZE)
 preds = target_scaler.inverse_transform(preds)
 preds = np.exp(preds)-1
submission = test[["test_id"]]
submission["price"] = preds<save_to_csv>
rfc.fit(X_train, y )
submission.to_csv("./myNNsubmission.csv", index=False)
 submission.price.hist() 
<set_options>
y_pred = rfc.predict(X_test )
%matplotlib inline<load_from_csv>
y_pred
train = pd.read_csv('.. /input/mercar/train.tsv', sep='\t')
 test = pd.read_csv('.. /input/mercari-price-suggestion-challenge/test_stg2.tsv', sep='\t')
 print(train.shape)
 print(test.shape )<compute_test_metric>
submission = y_pred.reshape(-1, 1 )
def rmsle(y, y_pred):
return(np.sum(( np.log(y_pred + 1)-(np.log(y + 1)))** 2)/ len(y)) ** 0.5<categorify>
sub_df = pd.DataFrame(submission )
def preprocessing_data(data):
data.category_name.fillna(value='missing', inplace=True)
 data.brand_name.fillna(value='missing', inplace=True)
 data.item_description.fillna(value='missing', inplace=True)
 return data<categorify>
sub_df['PassengerId'] = test['PassengerId']
sub_df['Survived'] = submission
cols = ['PassengerId',
'Survived']
sub_df.drop(0, axis = 1, inplace = True)
 sub_df.columns = [i for i in cols]
sub_df = sub_df.set_index('PassengerId' )
<categorify><EOS> 
sub_df.to_csv(r'submission.csv' )