Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<count_missing_values>
pd.options.display.max_colwidth = 110
print(train.isnull().sum() )<count_missing_values>
df = pd.read_json('.. /input/train.json' ).set_index('id')
 df_test = pd.read_json('.. /input/test.json' ).set_index('id')
 df.head(10)
 df['cuisine'].unique()
df['cuisine'].value_counts()
df[df['ingredients'].str.len() < 2]
print(test.isnull().sum() )<feature_engineering>
df = df.drop(df[df['ingredients'].str.len() < 2].index, axis=0)
 df = df.drop(df[df['ingredients'].str.len() > 30].index, axis=0 )
train['max'] = train.max(axis=1)
 train['min'] = train.min(axis=1)
 train['mean'] = train.mean(axis=1)
 train['sd'] = train.std(axis=1)
 test['max'] = test.max(axis=1)
 test['min'] = test.min(axis=1)
 test['mean'] = test.mean(axis=1)
 test['sd'] = test.std(axis=1 )<count_duplicates>
dfX = df['ingredients'].str.join(' ' ).str.lower()
dfX_test = df_test['ingredients'].str.join(' ' ).str.lower()
@jit
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y<init_hyperparams>
dfy = df['cuisine']
lbe = LabelEncoder()
y = lbe.fit_transform(dfy.values )
param = {
'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': -1
}<define_variables>
tfidf = TfidfVectorizer(stop_words='english')
 X = tfidf.fit_transform(dfX.values)
 X_test = tfidf.transform(dfX_test.values )
num_folds = 11
features = [c for c in train.columns if c not in ['ID_code', 'target']]
folds = KFold(n_splits=num_folds, random_state=2319)
 oof = np.zeros(len(train))
getVal = np.zeros(len(train))
predictions = np.zeros(len(train.target))
feature_importance_df = pd.DataFrame()<split>
clf = SVC(C=100,
kernel='rbf',
degree=3,
gamma=1,
coef0=1,
shrinking=True,
tol=0.001,
probability=False,
cache_size=200,
class_weight=None,
verbose=False,
max_iter=-1,
decision_function_shape=None,
random_state=None)
 clf.fit(X, y )
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, train.target.values)) :
X_train, y_train = train.iloc[trn_idx][features], train.target.iloc[trn_idx]
X_valid, y_valid = train.iloc[val_idx][features], train.target.iloc[val_idx]
X_tr, y_tr = augment(X_train.values, y_train.values)
 X_tr = pd.DataFrame(X_tr)
 print("Fold idx:{}".format(fold_ + 1))
trn_data = lgb.Dataset(X_tr, label=y_tr)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
 oof[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 getVal[val_idx]+= clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test[features], num_iteration=clf.best_iteration)/ folds.n_splits<compute_test_metric>
y_test = clf.predict(X_test)
 y_pred = lbe.inverse_transform(y_test)
 cross_val_score(clf, X, y, cv=3 )
print("
>> CV score: {:<8.5f}".format(roc_auc_score(train.target, oof)) )<save_to_csv>
sub = pd.DataFrame({'id':df_test.index, 'cuisine':y_pred})
 sub.to_csv('cuisine_output.csv', index=False )