Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<load_pretrained>
%matplotlib inline
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
 warnings.filterwarnings('ignore' )
joblib.dump(model.best_iteration, "fold0_lgb_model_best_iteration.pkl.zip" )<compute_test_metric>
np.set_printoptions(precision=6 )
preds1 = model.predict(valid[FEATS], num_iteration=400)
 print('auc at 400:', roc_auc_score(y_va, preds1))<compute_train_metric>
DATA_DIR='.. /input'
TRAIN_FILE=DATA_DIR + '/train.json'
TEST_FILE=DATA_DIR + '/test.json'
SUBMIT_FILE= 'sample_submission.csv'
preds2 = model.predict(valid[FEATS], num_iteration=700)
 print('auc at 700:', roc_auc_score(y_va, preds2))<predict_on_test>
def clean_data(X):
X['ingredients'] = X['ingredients'].map(lambda l: [x.lower() for x in l])
 X['ingredients'] = X['ingredients'].map(lambda l: [re.sub(r'\(\s*[^\s]*\s+oz\.\s*\)\s*', '', x ).strip() for x in l])
 X['ingredients'] = X['ingredients'].map(lambda l: [x.replace("-", " ")for x in l])
 X['ingredients'] = X['ingredients'].map(lambda l: [x.replace("half & half", "half milk cream")for x in l])
 X['ingredients'] = X['ingredients'].map(lambda l: [x.replace(" & ", " ")for x in l])
 X['ingredients'] = X['ingredients'].map(lambda l: [re.sub(r'\d+%\s+less\s+sodium ', '', x ).strip() for x in l])
 lemmatizer = WordNetLemmatizer()
X['ingredients'] = X['ingredients'].map(lambda l: [lemmatizer.lemmatize(x)for x in l])
 return X
preds3 = model.predict(valid[FEATS], num_iteration=model.best_iteration)
 print(f'auc at {model.best_iteration}', roc_auc_score(y_va, preds3))<prepare_output>
def words_to_text(list_of_words, add_separate_words=True):
if isinstance(list_of_words, list):
l = list_of_words
else:
l = eval(list_of_words)
 s = ''
for i, w_0 in enumerate(l):
if i > 0:
s = s + ' '
w_0 = w_0.strip()
w_1 = w_0.replace(' ', '_' ).replace(',','_vir_')
 s = s + w_1
if add_separate_words and ' ' in w_0:
s = s + ' ' + w_0
return s
def vectorize(X_words, sublinear_tf=True, max_df=0.75):
print("Extracting vectorized feats from the training data using a sparse vectorizer")
 t0 = time()
vectorizer = TfidfVectorizer(sublinear_tf=True,
max_df=max_df,
stop_words='english')
 X_vectorized = vectorizer.fit_transform(X_words)
 duration = time() - t0
print("done in %fs " %(duration))
print("n_samples: %d, n_features: %d" % X_vectorized.shape)
 print()
return X_vectorized, vectorizer, duration
oof = np.zeros(( 3, y_va.shape[0]))
oof[0] = preds1
oof[1] = preds2
oof[2] = preds3<feature_engineering>
def benchmark(clf, X_train, y_train, X_test, y_test):
print('_' * 80)
 print("Training {}: ".format(str(type(clf))))
print(clf)
 t0 = time()
clf.fit(X_train, y_train)
 train_time = time() - t0
print("train time: %0.3fs" % train_time)
 t0 = time()
y_pred = clf.predict(X_test)
 test_time = time() - t0
print("test time: %0.3fs" % test_time)
 score = metrics.accuracy_score(y_test, y_pred)
 print("accuracy score: %0.3f" % score)

cm = metrics.confusion_matrix(y_test, y_pred)
 clf_descr = str(clf ).split('(')[0]
ret = {
'classifier': clf,
'accuracy_score': score,
'confusion_matrix': cm,
'train_time': train_time,
'test_time': test_time,
'y_pred': y_pred
}
return ret
@njit
def grad_func_jit(weights):
oof_clip = np.minimum(1 - 1e-15, np.maximum(oof, 1e-15))
gradients = np.zeros(oof.shape[0])
 for i in range(oof.shape[0]):
a, b, c = y_true, oof_clip[i], np.zeros(( oof.shape[1]))
for j in range(oof.shape[0]):
if j != i:
c += weights[j] * oof_clip[j]
gradients[i] = -np.mean(( -a*b+(b**2)*weights[i]+b*c)/(( b**2)*(weights[i]**2)+2*b*c*weights[i]-b*weights[i]+(c**2)-c))
return gradients<compute_test_metric>
df = pd.read_json(TRAIN_FILE, encoding='iso-8859-1')
 clean_data(df)
 df.head(3 )
def func_numpy_metric(weights):
oof_blend = np.tensordot(weights, oof, axes =(( 0),(0)))
 return -roc_auc_score(y_va, oof_blend )<import_modules>
X_words = df['ingredients'].map(words_to_text)
 X_words.head(3 )
import datetime
from time import time<train_on_grid>
X_vects, Vectorizer_inst, Vectorizer_duration = vectorize(X_words)
 X_column_names = Vectorizer_inst.get_feature_names()
tol = 1e-10
init_guess = [1 / oof.shape[0]] * oof.shape[0]
bnds = [(0, 1)for _ in range(oof.shape[0])]
cons = {'type': 'eq',
'fun': lambda x: np.sum(x)- 1,
'jac': lambda x: [1] * len(x)}
print('Inital Ensemble OOF:', func_numpy_metric(init_guess))
start_time = time()
res_scipy = minimize(fun = func_numpy_metric,
x0 = init_guess,
method = 'SLSQP',
jac = grad_func_jit,
bounds = bnds,
constraints = cons,
tol = tol)
 print(f'[{str(datetime.timedelta(seconds = time() - start_time)) [2:7]}] Optimised Ensemble OOF:', res_scipy.fun)
 print('Optimised Weights:', res_scipy.x )<compute_test_metric>
encoder = LabelEncoder()
y_encoded = encoder.fit_transform(df['cuisine'])
 y_encoded_classes_names = encoder.inverse_transform(range(20))
preds = oof[0] * res_scipy.x[0] + oof[1] * res_scipy.x[1] + oof[2] * res_scipy.x[2]
print(f'auc at {model.best_iteration}', roc_auc_score(y_va, preds))<load_pretrained>
Xs_train, Xs_test, y_train, y_test = train_test_split(X_vects, y_encoded, random_state=0 )
optimized_weights = res_scipy.x
joblib.dump(optimized_weights, "optimized_weights.pkl.zip" )<feature_engineering>
BEST_PARAMS = {
'C': 80,
'kernel': 'rbf',
'gamma': 1.7,
'coef0': 1,
'cache_size': 500
}
BEST_ESTIMATOR = OneVsRestClassifier(
 SVC(**BEST_PARAMS))
class Iter_Valid(object):
def __init__(self, df, max_user=1000):
df = df.reset_index(drop=True)
 self.df = df
self.user_answer = df['user_answer'].astype(str ).values
self.answered_correctly = df['answered_correctly'].astype(str ).values
df['prior_group_responses'] = "[]"
df['prior_group_answers_correct'] = "[]"
self.sample_df = df[df['content_type_id'] == 0][['row_id']]
self.sample_df['answered_correctly'] = 0
self.len = len(df)
 self.user_id = df.user_id.values
self.task_container_id = df.task_container_id.values
self.content_type_id = df.content_type_id.values
self.max_user = max_user
self.current = 0
self.pre_user_answer_list = []
self.pre_answered_correctly_list = []
def __iter__(self):
return self
def fix_df(self, user_answer_list, answered_correctly_list, pre_start):
df= self.df[pre_start:self.current].copy()
sample_df = self.sample_df[pre_start:self.current].copy()
df.loc[pre_start,'prior_group_responses'] = '[' + ",".join(self.pre_user_answer_list)+ ']'
df.loc[pre_start,'prior_group_answers_correct'] = '[' + ",".join(self.pre_answered_correctly_list)+ ']'
self.pre_user_answer_list = user_answer_list
self.pre_answered_correctly_list = answered_correctly_list
return df, sample_df
def __next__(self):
added_user = set()
pre_start = self.current
pre_added_user = -1
pre_task_container_id = -1
pre_content_type_id = -1
user_answer_list = []
answered_correctly_list = []
while self.current < self.len:
crr_user_id = self.user_id[self.current]
crr_task_container_id = self.task_container_id[self.current]
crr_content_type_id = self.content_type_id[self.current]
if crr_user_id in added_user and(crr_user_id != pre_added_user or(crr_task_container_id != pre_task_container_id and crr_content_type_id == 0 and pre_content_type_id == 0)) :
return self.fix_df(user_answer_list, answered_correctly_list, pre_start)
 if len(added_user)== self.max_user:
if crr_user_id == pre_added_user and(crr_task_container_id == pre_task_container_id or crr_content_type_id == 1):
user_answer_list.append(self.user_answer[self.current])
 answered_correctly_list.append(self.answered_correctly[self.current])
 self.current += 1
continue
else:
return self.fix_df(user_answer_list, answered_correctly_list, pre_start)
 added_user.add(crr_user_id)
 pre_added_user = crr_user_id
pre_task_container_id = crr_task_container_id
pre_content_type_id = crr_content_type_id
user_answer_list.append(self.user_answer[self.current])
 answered_correctly_list.append(self.answered_correctly[self.current])
 self.current += 1
if pre_start < self.current:
return self.fix_df(user_answer_list, answered_correctly_list, pre_start)
 else:
raise StopIteration()<categorify>
RESULT = benchmark(BEST_ESTIMATOR, Xs_train, y_train, Xs_test, y_test )
if validaten_flg:
target_df = pd.read_pickle(valid_pickle)
 if debug:
target_df = target_df[:10000]
iter_test = Iter_Valid(target_df,max_user=1000)
 predicted = []
def set_predict(df):
predicted.append(df)
 answered_correctly_sum_u_dict = defaultdict(int)
 count_u_dict = defaultdict(int)
 train = pd.read_pickle(train_pickle)[['user_id','answered_correctly','content_type_id']]
if debug:
train = train[:1000000]
train = train[train.content_type_id == False].reset_index(drop=True)
 update_user_feats(train, answered_correctly_sum_u_dict, count_u_dict)
 del train
else:
env = riiideducation.make_env()
iter_test = env.iter_test()
set_predict = env.predict<merge>
pd.set_option('display.max_colwidth', 120)
 y_pred = RESULT['y_pred']
X_test_words = Vectorizer_inst.inverse_transform(Xs_test)
 y_test_class = encoder.inverse_transform(y_test)
 y_test_pred_class = encoder.inverse_transform(y_pred)
 df_check = pd.DataFrame({
'TRUE_CLASS': y_test_class,
'PRED_CLASS': y_test_pred_class,
'ingredients': X_test_words,
})
 df_ko = df_check[df_check['TRUE_CLASS'] != df_check['PRED_CLASS']]
print('Some misclassfied cases:')
 df_ko.sample(10 )
previous_test_df = None
for(test_df, sample_prediction_df)in iter_test:
if previous_test_df is not None:
previous_test_df[TARGET] = eval(test_df["prior_group_answers_correct"].iloc[0])
 update_user_feats(previous_test_df, answered_correctly_sum_u_dict, count_u_dict)
 previous_test_df = test_df.copy()
test_df = test_df[test_df['content_type_id'] == 0].reset_index(drop=True)
 test_df = add_user_feats_without_update(test_df, answered_correctly_sum_u_dict, count_u_dict)
 test_df = pd.merge(test_df, content_df, on='content_id', how="left")
 test_df = pd.merge(test_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 test_df['prior_question_had_explanation'] = test_df.prior_question_had_explanation.fillna(False ).astype('int8')
 test_df['prior_question_elapsed_time_mean'] = test_df.prior_question_elapsed_time.fillna(prior_question_elapsed_time_mean)
 preds = optimized_weights[0] * model.predict(test_df[FEATS], num_iteration=400)
 preds += optimized_weights[1] * model.predict(test_df[FEATS], num_iteration=700)
 preds += optimized_weights[2] * model.predict(test_df[FEATS], num_iteration=model.best_iteration)
 test_df[TARGET] = preds
set_predict(test_df[['row_id', TARGET]] )<compute_test_metric>
df_ko[(df_ko['PRED_CLASS'] == 'southern_us')&(df_ko['TRUE_CLASS'] == 'indian')]
if validaten_flg:
y_true = target_df[target_df.content_type_id == 0].answered_correctly
y_pred = pd.concat(predicted ).answered_correctly
print(roc_auc_score(y_true, y_pred))<find_best_params>
df_test_subm = pd.read_json(TEST_FILE, encoding='iso-8859-1')
 df_test_subm.set_index('id', inplace=True)
 df_test_subm.head(20 )
env = riiideducation.make_env()<load_from_csv>
clean_data(df_test_subm)
 X_test_subm_words = df_test_subm['ingredients'].map(words_to_text)
 X_test_subm_vects = Vectorizer_inst.transform(X_test_subm_words )
train = pd.read_csv(
 '/kaggle/input/riiid-test-answer-prediction/train.csv',
usecols=[1, 2, 3, 4, 5, 7, 8, 9],
dtype={
'timestamp': 'int64',
'user_id': 'int32',
'content_id': 'int16',
'content_type_id': 'int8',
'task_container_id': 'int16',
'answered_correctly':'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'boolean'
}
)<load_from_csv>
y_test_subm_encoded = BEST_ESTIMATOR.predict(X_test_subm_vects )
questions_df = pd.read_csv(
 '/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=[0,3],
dtype = {
"question_id":"int64",
"part":"int8"
}
)<load_from_csv>
y_test_subm = [y_encoded_classes_names[i] for i in y_test_subm_encoded]
y_test_subm[:3]
lectures_df = pd.read_csv(
 '/kaggle/input/riiid-test-answer-prediction/lectures.csv',
)<categorify>
df_test_subm['cuisine'] = y_test_subm
df_test_subm.head(20 )
<define_variables><EOS> 
df_test_subm['cuisine'].to_csv(SUBMIT_FILE, header=True )