Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<save_to_csv>
%matplotlib inline
sns.set_context('notebook' )
submission.to_csv('lightgbm2.csv',index=False )<load_from_csv>
df_train_0 = pd.read_json('.. /input/train.json')
 df_test_0 = pd.read_json('.. /input/test.json')
 display(df_train_0.head() )
train_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
 submit_data = pd.read_csv('.. /input/sample_submission.csv')
 features = [c for c in train_data.columns if c not in ['ID_code', 'target']]
target = train_data['target']
print("Data is ready!" )<define_variables>
print(df_train_0.isnull().sum())
 print(df_test_0.isnull().sum())
 df_train = df_train_0.copy()
df_test = df_test_0.copy()
tar = target<drop_column>
display(df_train.loc[df_train.num_ing < 2] )
train_data = train_data.drop(["ID_code"], axis=1)
 test_data = test_data.drop(["ID_code"], axis=1 )<filter>
display(df_train.loc[df_train.num_ing > 60] )
target0 = train_data[target==0]
target1 = train_data[target==1] 
<categorify>
gtr_60_ind = df_train[df_train['num_ing'] > 60].index.values
print(df_train.ingredients[gtr_60_ind].tolist() )
for i in range(0,199):
ch = 'var_' + str(i)
 target0[ch] = np.random.permutation(target0[ch])
 for i in range(0,199):
ch = 'var_' + str(i)
 target1[ch] = np.random.permutation(target1[ch] )<concatenate>
display(df_train.loc[df_train.num_ing > 40])
 print(df_train.ingredients[26103] )
target0 = target0.append(target1 )<concatenate>
df_train = df_train[df_train['num_ing'] < 32]
train_data = train_data.append(target0 )<prepare_x_and_y>
df_train.reset_index(inplace=True, drop=True )
target = train_data['target']<drop_column>
lemmatizer = nltk.WordNetLemmatizer()
print(lemmatizer.lemmatize('tomatoes'))
train_data = train_data.drop(["target"], axis=1)
<create_dataframe>
def ingredients(df, i):
ing_list = df['ingredients'].iloc[i].copy()
ing_concat = []
for ing in ing_list:
ing = u"".join([c for c in unicodedata.normalize('NFKD', ing)if not unicodedata.combining(c)])
 ing = re.sub(r'[^a-zA-Z]',' ', ing)
 if 'fish sauce' in ing:
ing = 'fishsauce'
if 'corn starch' in ing:
ing = 'cornstarch'
if 'sour cream' in ing:
ing = 'sourcream'
word_list = ing.split()
for word in word_list:
word = word.strip()
lem = lemmatizer.lemmatize(word)
 ing_concat.append(lem)
 ing_concat = list(np.unique(ing_concat))
df.at[i, 'ingredients_concat'] = ' '.join(ing_concat)
%%time
train_data = pd.DataFrame(scale(train_data.values), columns=train_data.columns, index=train_data.index)
 test_data = pd.DataFrame(scale(test_data.values), columns=test_data.columns, index=test_data.index )<init_hyperparams>
for i in range(df_train.shape[0]):
ingredients(df_train, i)
 for i in range(df_test.shape[0]):
ingredients(df_test, i )
params = {}
params['bagging_freq'] = 5
params['bagging_fraction'] = 0.0331
params['random_state'] = 42
params['learning_rate'] = 0.0123
params['boost_from_average'] = False
params['boosting_type'] = 'gbdt'
params['feature_fraction'] = 0.045
params['objective'] = 'binary'
params['metric'] = 'auc'
params['min_data_in_leaf'] = 80
params['num_leaves'] = 13
params['num_threads'] = 8
params['tree_learner'] = 'serial'
params['max_depth'] = -1
params['min_sum_hessian_in_leaf'] = 10.0
params['verbosity'] = 1
params['bagging_seed'] = 42
params['seed'] = 42<create_dataframe>
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(df_train.ingredients_concat)
 X_test_counts = count_vect.transform(df_test.ingredients_concat)
 print('A total of', X_train_counts.shape[0], 'recipes')
 print('A total of', X_train_counts.shape[1], 'unique ingredient words' )
num_folds = 10
features = [c for c in test_data.columns if c not in ['ID_code', 'target']]
folds = StratifiedKFold(n_splits=num_folds,shuffle=True, random_state=42)
 oof = np.zeros(len(train_data))
getVal = np.zeros(len(train_data))
predictions = np.zeros(len(tar))
print(predictions.shape)
 feature_importance_df = pd.DataFrame()<prepare_x_and_y>
ingredient_names = np.array(count_vect.get_feature_names())
 ingredient_counts = dict(zip(ingredient_names, X_train_counts.sum(axis=0 ).tolist() [0]))
ingredient_counts = pd.DataFrame(list(ingredient_counts.items()), columns=['ingredients', 'counts'])
 ingredient_counts['counts_frac'] = ingredient_counts['counts'].apply(lambda x: x/X_train_counts.shape[0] )
%%time
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_data.values, target.values)) :
X_train, y_train = train_data.iloc[trn_idx][features], target.iloc[trn_idx]
X_valid, y_valid = train_data.iloc[val_idx][features], target.iloc[val_idx]
X_tr, y_tr = 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(params, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
 predictions += clf.predict(test_data[features], num_iteration=clf.best_iteration)/ folds.n_splits<save_to_csv>
ingredient_counts.sort_values(by='counts', ascending=False ).head(n=10 )
submit_data['target'] = pd.DataFrame(predictions)
 submit_data.to_csv("LGBM.csv", index=False )<set_options>
tfidf = TfidfTransformer()
X_train_tfidf = tfidf.fit_transform(X_train_counts)
 X_test_tfidf = tfidf.transform(X_test_counts)
 print(X_train_tfidf.shape )
print(os.listdir(".. /input"))
warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings('ignore')
 plt.style.use('seaborn')
 sns.set(font_scale=1 )<load_from_csv>
mlb = preprocessing.MultiLabelBinarizer()
y_train_mlb = mlb.fit_transform(df_train.cuisine.values.reshape(-1,1))
le = preprocessing.LabelEncoder()
y_train_le = le.fit_transform(df_train.cuisine )
random_state = 42
np.random.seed(random_state)
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<drop_column>
X_train_tfidf_pd = pd.DataFrame(X_train_tfidf.A, columns=ingredient_names)
 X_test_tfidf_pd = pd.DataFrame(X_test_tfidf.A, columns=np.array(count_vect.get_feature_names()))
train_X = df_train.drop(['ID_code', 'target'], axis = 1)
 test_X = df_test.drop(['ID_code'], axis = 1 )<categorify>
var = X_train_tfidf_pd.var()
var.sort_values(ascending=False ).head(n=10 )
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<normalization>
rfr = RandomForestRegressor(max_depth=5, n_estimators=10)
 rfr.fit(X_train_tfidf, y_train_le )
mmscale = MinMaxScaler()
train_X_scaled = mmscale.fit_transform(train_X)
 test_X_scaled = mmscale.transform(test_X )<normalization>
pca = PCA(n_components=1000)
 X_train_pca = pca.fit_transform(X_train_tfidf_pd)
 X_test_pca = pca.transform(X_test_tfidf_pd)
 X_train_pca_pd = pd.DataFrame(X_train_pca )
pca = PCA()
factors_train = pca.fit_transform(train_X_scaled)
 factors_test = pca.transform(test_X_scaled )<merge>
pca_columns_name = ["pca_" + str(col)for col in range(0, 200)]
factors_train = pd.DataFrame(factors_train, columns = pca_columns_name)
 factors_test = pd.DataFrame(factors_test, columns = pca_columns_name)
 train_pca = df_train.merge(factors_train, left_index = True, right_index = True)
 test_pca = df_test.merge(factors_test, left_index = True, right_index = True )<define_variables>
SVC_tuned_clf = SVC(kernel='rbf', **best_params)
 SVC_tuned_clf.fit(X_train_pca, y_train_le)
 y_true_pred_le = SVC_tuned_clf.predict(X_test_pca ).astype('int')
 y_true_pred = le.inverse_transform(y_true_pred_le)
 X_true_test_withpred = pd.DataFrame({'id': df_test_0.id, 'cuisine': y_true_pred})
 X_true_test_withpred.to_csv('svc_gscv_prediction.csv', mode='w', index=False )
important_factors = ['pca_2', 'pca_7', 'pca_14', 'pca_19']
vars_to_exclude = ['var_9', 'var_94', 'var_109', 'var_151']
df_train_columns = list(df_train.columns)
 df_test_columns = list(df_test.columns)
 train_columns_with_factors = important_factors + df_train_columns
test_columns_with_factors = important_factors + df_test_columns
train_main_pca = train_pca[train_columns_with_factors].drop(vars_to_exclude, axis = 1)
 test_main_pca = test_pca[test_columns_with_factors].drop(vars_to_exclude, axis = 1 )<split>
X_train_0, X_test_0, y_train_0, y_test_0 = model_selection.train_test_split(X_train_tfidf, y_train_mlb, test_size=0.2)
 num_cuisines = y_train_mlb.shape[1]
num_feats = X_train_tfidf.shape[1]
<init_hyperparams><EOS> 
inputs = keras.Input(shape=(num_feats,))
X = keras.layers.Dense(2000, activation=None )(inputs)
 X = keras.layers.BatchNormalization()(X)
 X = keras.layers.Activation('relu' )(X)
 X = keras.layers.Dropout(0.8 )(X)
 X = keras.layers.Dense(2000, activation=None )(inputs)
 X = keras.layers.BatchNormalization()(X)
 X = keras.layers.Activation('relu' )(X)
 X = keras.layers.Dropout(0.8 )(X)
 X = keras.layers.Dense(num_cuisines, activation=None )(X)
 X = keras.layers.BatchNormalization()(X)
 Y = keras.layers.Activation('softmax' )(X)
 model = keras.models.Model(inputs=inputs, outputs=Y)
 model.compile(optimizer=keras.optimizers.Adam(lr=0.001), loss='categorical_crossentropy',
metrics=['accuracy'])
 cb = keras.callbacks.ModelCheckpoint('cooking.weights.best.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 es = keras.callbacks.EarlyStopping(monitor='val_loss', patience=4)
 model.fit(X_train_0, y_train_0, epochs=20, batch_size=64, validation_data=(X_test_0, y_test_0), callbacks=[cb, es] )