Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<train_on_grid>
%matplotlib inline
param_grid = {
'n_estimators': [500, 1000],
'missing': [-999],
'random_state': [1993],
'n_jobs': [1],
'tree_method': ['gpu_hist'],
'max_depth': [9],
'learning_rate': [0.048, 0.05],
'subsample': [0.85, 0.9],
'colsample_bytree': [0.85, 0.9],
'reg_alpha': [0, 0.1],
'reg_lambda': [1, 0.9]
}
grid = XGBGridSearch(param_grid, cv=6, verbose=1)
 grid.fit(X, y)
 print("Best Score:", grid.best_score_)
 print("Best Params:", grid.best_params_ )<train_model>
train_data = pd.read_table('.. /input/jerboris/train.tsv')
 test_data = pd.read_table('.. /input/jerboris/test.tsv')
 train_data.head()
clf = xgb.XGBClassifier(**grid.best_params_)
 clf.fit(X, y )<save_to_csv>
def check_nan(data):
data_check = data.replace('NaN',np.nan)
 data_check = data.replace('No description yet',np.nan)
 data_nan = data_check.isnull().sum()
data_nan.sort_values(ascending=False, inplace=True)
 print('
Total number of NaN values in the dataset:',data_nan.sum())
 print('
Top three features with most NaN values in the dataset:')
 print(data_nan[:5])
 check_nan(train_data)
 check_nan(test_data )
sample_submission['isFraud'] = clf.predict_proba(X_test)[:,1]
sample_submission.to_csv('simple_xgboost.csv' )<import_modules>
def get_feature_label(data):
data_after = data[(data['price']<400)&(data['price']>1)]
train_features = data_after.drop(['price'],axis=1)
 train_labels = data_after.price
return train_features,train_labels
FileLinks('.' )<import_modules>
train_features,train_labels=get_feature_label(train_data)
 train_features=train_features
train_labels=train_labels
test_features=test_data
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os 
<load_from_csv>
train_labels = np.log(train_labels )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
def category(data):
cat = data.category_name.str.split('/', expand = True)
 data["main_cat"] = cat[0]
data["subcat1"] = cat[1]
data["subcat2"] = cat[2]
try:
data["subcat3"] = cat[3]
except:
data["subcat3"] = np.nan
try:
data["subcat4"] = cat[4]
except:
data["subcat4"] = np.nan
def missing_data(data, _value = 'None'):
for col in data.columns:
data[col].fillna(_value,inplace=True )
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224<load_pretrained>
le = preprocessing.LabelEncoder()
def cat_to_num(train,test):
suf="_le"
for col in ['brand_name','main_cat','subcat1','subcat2','subcat3','subcat4']:
train[col+suf] = le.fit_transform(train[col])
 dic = dict(zip(le.classes_, le.transform(le.classes_)))
 test[col+suf] = test[col].map(dic ).fillna(0 ).astype(int)
 print("{} is transformed to {}".format(col,col+suf))
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )<define_variables>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
cat_to_num(train_features,test_features )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
train_features['Length_of_item_description']=train_features['item_description'].apply(len)
 test_features['Length_of_item_description']=test_features['item_description'].apply(len )
train_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB0_ns.npy')
 test_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB0_ns.npy')
 train_EB1_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB1_ns.npy')
 test_EB1_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB1_ns.npy')
 train_EB2_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB2_ns.npy')
 test_EB2_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB2_ns.npy')
 train_EB3_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB3_ns.npy')
 test_EB3_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB3_ns.npy')
 train_EB4_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB4_ns.npy')
 test_EB4_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB4_ns.npy')
 train_EB5_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB5_ns.npy')
 test_EB5_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB5_ns.npy')
 train_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB6_ns.npy')
 test_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB6_ns.npy')
 train_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB7_ns.npy')
 test_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB7_ns.npy' )<split>
def numeric_to_features(data):
numeric_features = list(data.apply(lambda x:(x['shipping'],x['item_condition_id'],x['main_cat_le'],\
x['subcat1_le'],x['subcat2_le'],x['subcat3_le'],\
x['subcat4_le'],x['Length_of_item_description'],\
x['brand_name_le']), axis=1))
return numeric_features
X_train, X_val, y_train, y_val = train_test_split(train_EB0_ns, target, test_size=0.1, random_state=42 )<train_model>
train_numeric_features = numeric_to_features(train_features)
 test_numeric_features = numeric_to_features(test_features)
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
tfidf_d = TfidfVectorizer(sublinear_tf=True,ngram_range=(1,3),min_df=0, stop_words = 'english',max_features = 500)
 Description_matrix_train= tfidf_d.fit_transform(train_features['item_description'])
 Description_matrix_test= tfidf_d.transform(test_features['item_description'])
 tfidf_n = CountVectorizer(min_df = 10)
 name_features_train = tfidf_n.fit_transform(train_features['name'])
 name_features_test = tfidf_n.transform(test_features['name'])
 train_text_features = sparse.hstack([ name_features_train, Description_matrix_train], format='csr')
 test_text_features = sparse.hstack([ name_features_test, Description_matrix_test], format='csr')
val_preds_EB0 = lr.predict_proba(X_val)[:,1]<compute_test_metric>
train_final_features = sparse.hstack(( train_numeric_features,train_text_features)).tocsr()
test_final_features = sparse.hstack(( test_numeric_features,test_text_features)).tocsr()
roc_auc_score(y_val, val_preds_EB0 )<compute_test_metric>
xgb = xgboost.XGBRegressor(n_estimators=800, learning_rate=0.25, gamma=0,booster='gbtree',n_jobs=4,subsample=1,colsample_bytree=1,min_child_weight=1, max_depth=15,seed=1505)
log_loss(y_val, val_preds_EB0 )<predict_on_test>
xgb.fit(train_final_features,train_labels)
 predictions = xgb.predict(test_final_features )
<find_best_model_class><EOS> 
results = np.exp(predictions)
 results[results<0]=0
outfile_name = 'submit_xgboost_regression.csv'
prediction = pd.DataFrame(np.array(results), columns = ['price'])
 prediction.index.name = 'test_id'
prediction.to_csv(outfile_name, encoding='utf-8' )