Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<feature_engineering>
%matplotlib inline
train['ord_5_band'] = pd.qcut(train['ord_5'], 6)
 bands=train.ord_5_band.unique()
keys_bands=np.sort(bands)
 values_bands=np.arange(len(keys_bands))
map_bands = dict(zip(keys_bands, values_bands))<feature_engineering>
def handle_missing_inplace(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)
 def cutting(dataset):
pop_brand = dataset['brand_name'].value_counts().loc[lambda x: x.index != 'missing'].index[:NUM_BRANDS]
dataset.loc[~dataset['brand_name'].isin(pop_brand), 'brand_name'] = 'missing'
pop_category = dataset['category_name'].value_counts().loc[lambda x: x.index != 'missing'].index[:NUM_BRANDS]
dataset.loc[~dataset['category_name'].isin(pop_category), 'category_name'] = 'missing'
def to_categorical(dataset):
dataset['category_name'] = dataset['category_name'].astype('category')
 dataset['brand_name'] = dataset['brand_name'].astype('category')
 dataset['item_condition_id'] = dataset['item_condition_id'].astype('category' )
train['ord_5_band'] = train['ord_5_band'].replace(map_bands)
 test['ord_5_band']=pd.cut(test.ord_5,pd.IntervalIndex(keys_bands))<categorify>
train = pd.read_table('.. /input/train.tsv', engine='c')
 test = pd.read_table('.. /input/test.tsv', engine='c')
 nrow_train = train.shape[0]
test['ord_5_band'] = test['ord_5_band'].replace(map_bands)
 test.ord_5_band.head()<count_unique_values>
y = np.log1p(train["price"])
 merge: pd.DataFrame = pd.concat([train, test])
 submission: pd.DataFrame = test[['test_id']]
train[nom_cols].nunique()<count_unique_values>
NUM_BRANDS = 4000
NUM_CATEGORIES = 1000
NAME_MIN_DF = 10
MAX_FEATURES_ITEM_DESCRIPTION = 50000
handle_missing_inplace(merge)
 cutting(merge)
 to_categorical(merge )
test[nom_cols].nunique()<count_unique_values>
cv = CountVectorizer(min_df=NAME_MIN_DF)
 X_name = cv.fit_transform(merge['name'])
 cv = CountVectorizer()
X_category = cv.fit_transform(merge['category_name'])
 tv = TfidfVectorizer(max_features=MAX_FEATURES_ITEM_DESCRIPTION,
ngram_range=(1, 3),
stop_words='english')
 X_description = tv.fit_transform(merge['item_description'])
 lb = LabelBinarizer(sparse_output=True)
 X_brand = lb.fit_transform(merge['brand_name'])
 X_dummies = csr_matrix(pd.get_dummies(merge[['item_condition_id', 'shipping']],
sparse=True ).values)
 sparse_merge = hstack(( X_dummies, X_description, X_brand, X_category, X_name)).tocsr()
X = sparse_merge[:nrow_train]
X_test = sparse_merge[nrow_train:]
train[nom_cols].nunique()<count_unique_values>
model = Ridge(solver="sag", fit_intercept=True, random_state=205, alpha=3)
 model.fit(X, y)
 predsR = model.predict(X=X_test)
 model = Ridge(solver="lsqr", fit_intercept=False, random_state=145, alpha = 3)
 model.fit(X, y)
 predsR2 = model.predict(X=X_test)
 model = Ridge(solver="sag", fit_intercept=False, random_state=205, alpha = 3)
 model.fit(X, y)
 predsR3 = model.predict(X=X_test )
test[nom_cols].nunique()<filter>
model = RandomForestRegressor(max_features='log2', min_weight_fraction_leaf=0.1)
 model.fit(X, y)
 predsL = model.predict(X=X_test)
<filter><EOS> 
preds1 = np.expm1(predsR3*0.24 + predsR2*0.24 + predsR*0.52)
 preds2 = np.expm1(predsL)
 submission['price'] = preds1*0.75 + preds2*0.25
submission.to_csv('submission.csv', index=False )