Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<find_best_model_class>
%matplotlib inline
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB2_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB2 = lr.predict_proba(X_val)[:,1]
test_preds_EB2 = lr.predict_proba(test_EB2_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB2))
print(log_loss(y_val, val_preds_EB2))<find_best_model_class>
Start_time = time.time()
train_data = pd.read_csv('.. /input/train.tsv', sep='\t')
 test_data = pd.read_csv('.. /input/test.tsv', sep='\t' )
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB3_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB3 = lr.predict_proba(X_val)[:,1]
test_preds_EB3 = lr.predict_proba(test_EB3_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB3))
print(log_loss(y_val, val_preds_EB3))<find_best_model_class>
train_data['price'] = np.log1p(train_data['price'])
 train_data['shipping'] = np.log1p(train_data['shipping'])
 test_data['shipping'] = np.log1p(test_data['shipping'] )
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB4_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.019, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB4 = lr.predict_proba(X_val)[:,1]
test_preds_EB4 = lr.predict_proba(test_EB4_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB4))
print(log_loss(y_val, val_preds_EB4))<find_best_model_class>
y = train_data['price']
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB5_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB5 = lr.predict_proba(X_val)[:,1]
test_preds_EB5 = lr.predict_proba(test_EB5_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB5))
print(log_loss(y_val, val_preds_EB5))<find_best_model_class>
def if_catname(row):

 if row == row:
return 0
else:
return 1
train_data['Category_missing'] = train_data.category_name.apply(lambda row : if_catname(row))
train_data['Brand_missing'] = train_data.brand_name.apply(lambda row : if_catname(row))
train_data['Item_missing'] = train_data.item_description.apply(lambda row : if_catname(row))
test_data['Category_missing'] = test_data.category_name.apply(lambda row : if_catname(row))
test_data['Brand_missing'] = test_data.brand_name.apply(lambda row : if_catname(row))
test_data['Item_missing'] = test_data.item_description.apply(lambda row : if_catname(row))
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB6_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB6 = lr.predict_proba(X_val)[:,1]
test_preds_EB6 = lr.predict_proba(test_EB6_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB6))
print(log_loss(y_val, val_preds_EB6))<find_best_model_class>
train_data["category_name"].fillna("None/None/None", inplace=True)
 test_data["category_name"].fillna("None/None/None", inplace=True)
 train_data["brand_name"].fillna("None", inplace=True)
 test_data["brand_name"].fillna("None", inplace=True)
 train_data["item_description"].fillna("None", inplace=True)
 test_data["item_description"].fillna("None", inplace=True)
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB7_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.026, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB7 = lr.predict_proba(X_val)[:,1]
test_preds_EB7 = lr.predict_proba(test_EB7_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB7))
print(log_loss(y_val, val_preds_EB7))<compute_test_metric>
ID_train = train_data['train_id']
ID_test = test_data['test_id']
print(roc_auc_score(y_val, 0.6*val_preds_EB1+0.4*val_preds_EB0))
print(log_loss(y_val, 0.6*val_preds_EB1+0.4*val_preds_EB0))<save_to_csv>
train_data.drop("train_id", axis = 1, inplace = True)
 test_data.drop("test_id", axis = 1, inplace = True)
 train_data.drop("price", axis = 1, inplace = True)
sample_submission['id'] = ids
sample_submission['label'] = 0.8*test_preds_EB3+0.2*(0.6*test_preds_EB2+0.4*(0.6*test_preds_EB1+0.4*test_preds_EB0))
sample_submission.to_csv('submission_0.csv', index=False)
 sample_submission.head()<save_to_csv>
Combined_data = pd.concat([train_data,test_data] ).reset_index(drop=True )
sample_submission['id'] = ids
sample_submission['label'] = 0.5*test_preds_EB4+0.5*test_preds_EB5
sample_submission.to_csv('submission_1.csv', index=False)
 sample_submission.head()<save_to_csv>
Combined_data_numerical = Combined_data[numerical_features]
sample_submission['id'] = ids
sample_submission['label'] = 0.9*(0.5*test_preds_EB4+0.5*test_preds_EB5)+0.1*test_preds_EB6
sample_submission.to_csv('submission_2.csv', index=False)
 sample_submission.head()<save_to_csv>
count = CountVectorizer(min_df=10)
 X_name = count.fit_transform(Combined_data['name'])
 X_name.shape
sample_submission['id'] = ids
sample_submission['label'] = 0.9*test_preds_EB7 + 0.1*(0.9*(0.5*test_preds_EB4+0.5*test_preds_EB5)+0.1*test_preds_EB6)
 sample_submission.to_csv('submission_3.csv', index=False)
 sample_submission.head()<import_modules>
vector_brand = LabelBinarizer(sparse_output=True)
 X_brand = vector_brand.fit_transform(Combined_data['brand_name'])
 X_brand.shape
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>
vector_L0 = LabelBinarizer(sparse_output=True)
 X_L0 = vector_brand.fit_transform(Combined_data['category_name'])
 X_L0.shape
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
tfidf_vec = TfidfVectorizer(stop_words='english', ngram_range=(1,2), max_features=50000)
 X_description = tfidf_vec.fit_transform(Combined_data['item_description'])
 X_description.shape
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>
X = sparse.hstack(( X_name, X_brand, X_L0, num_features, X_description)).tocsr()
X.shape
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>
def evaluate_model(X, y, algorithm):
X_train, X_test, y_train, y_test = train_test_split(train, y, test_size = 0.20, random_state = 1)
 print(algorithm)
 print()
print('Train - Mean Squared Error')
 print(( np.sqrt(-cross_val_score(algorithm, X_train, y_train, scoring="neg_mean_squared_error", cv = 2)).mean()))
print()
print('Test - Mean Squared Error')
 print(( np.sqrt(-cross_val_score(algorithm, X_test, y_test, scoring="neg_mean_squared_error", cv = 2)).mean()))
print()
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
ridge = Ridge(solver='sag', fit_intercept=True, alpha=0.02, max_iter=200, normalize=False, tol=0.01)
 ridge.fit(train, y )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
labels_ridge = np.expm1(ridge.predict(test))
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' )<split>
pd.DataFrame({'test_id': ID_test , 'price': labels_ridge} ).to_csv('MercariPredictions.csv', index =False )
X_train, X_val, y_train, y_val = train_test_split(train_EB4_ns, target, test_size=0.1, random_state=42 )<train_model>
end_time = time.time()
<predict_on_test><EOS> 
( end_time - Start_time )