Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<create_dataframe>
warnings.filterwarnings("ignore")
 def load_data(filename):
with open(filename, 'r')as f:
data = json.load(f)
 df = pd.DataFrame(data)
 return df
train=load_data(".. /input/train.json")
 test=load_data(".. /input/test.json")
 train.sample(5 )
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 oof = df_train[['ID_code', 'target']]
oof['predict'] = 0
predictions = df_test[['ID_code']]
val_aucs = []
feature_importance_df = pd.DataFrame()<prepare_x_and_y>
tqdm.pandas()
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text=" ".join(ingredients)
 ingredients_text=ingredients_text.lower()
ingredients_text=ingredients_text.replace("-",'')
 ingredients_text=ingredients_text.replace("'",'')
 sentence=[]
for word in ingredients_text.split() :
if re.findall('[0-9]',word): continue
if len(word)<=2: continue
word=lemmatizer.lemmatize(word)
 sentence.append(word)
 return " ".join(sentence)
 train['ingredients_text']=train['ingredients'].progress_apply(lambda x: preprocess(x))
train=train.drop(['ingredients'],axis=1)
 train["ingredients_len"]=train['ingredients_text'].apply(lambda x: len(x.split()))
test['ingredients_text']=test['ingredients'].progress_apply(lambda x: preprocess(x))
test=test.drop(['ingredients'],axis=1)
 test["ingredients_len"]=test['ingredients_text'].apply(lambda x: len(x.split()))
features = [col for col in df_train.columns if col not in ['target', 'ID_code']]
X_test = df_test[features].values<split>
vectorizer=make_pipeline(
 TfidfVectorizer() ,
FunctionTransformer(lambda x: x.astype('float16'), validate=False)
)
 X_train=vectorizer.fit_transform(train['ingredients_text'].values)
 X_test=vectorizer.transform(test['ingredients_text'].values)
 X_train.shape
for fold,(trn_idx, val_idx)in enumerate(skf.split(df_train, df_train['target'])) :
X_train, y_train = df_train.iloc[trn_idx][features], df_train.iloc[trn_idx]['target']
X_valid, y_valid = df_train.iloc[val_idx][features], df_train.iloc[val_idx]['target']
N = 3
p_valid,yp = 0,0
for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values)
 X_t = pd.DataFrame(X_t)
 X_t = X_t.add_prefix('var_')
 trn_data = lgb.Dataset(X_t, label=y_t)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(lgb_params,
trn_data,
100000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=3000,
verbose_eval = 500,
evals_result=evals_result 
)
 p_valid += lgb_clf.predict(X_valid)
 yp += lgb_clf.predict(X_test)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = lgb_clf.feature_importance()
fold_importance_df["fold"] = fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 oof['predict'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp/N<compute_test_metric>
LabelEncoder=LabelEncoder()
y_train=LabelEncoder.fit_transform(train['cuisine'].values)
 label_dict=dict(zip(LabelEncoder.classes_,LabelEncoder.transform(LabelEncoder.classes_)))
 print(label_dict )
mean_auc = np.mean(val_aucs)
 std_auc = np.std(val_aucs)
 all_auc = roc_auc_score(oof['target'], oof['predict'])
 print("Mean auc: %.9f, std: %.9f.All auc: %.9f." %(mean_auc, std_auc, all_auc))<save_to_csv>
def model_explore(X, y):
models = [
RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),
SVC() ,
LinearSVC() ,
MultinomialNB() ,
LogisticRegression(random_state=0),
SGDClassifier(max_iter=200),
]
CV = 5
cv_df = pd.DataFrame(index=range(CV * len(models)))
 entries = []
for model in models:
model_name = model.__class__.__name__
accuracies = cross_val_score(model, X, y, scoring='accuracy', cv=CV)
 for fold_idx, accuracy in enumerate(accuracies):
entries.append(( model_name, fold_idx, accuracy))
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'])
 sns.boxplot(x='model_name', y='accuracy', data=cv_df)
 sns.stripplot(x='model_name', y='accuracy', data=cv_df,
size=5, jitter=True, edgecolor="gray", linewidth=2)
 plt.show()
return cv_df
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 predictions.to_csv('lgb_all_predictions.csv', index=None)
 sub_df = pd.DataFrame({"ID_code":df_test["ID_code"].values})
 sub_df["target"] = predictions['target']
sub_df.to_csv("lgb_submission.csv", index=False)
 oof.to_csv('lgb_oof.csv', index=False )<set_options>
cv_baseline=model_explore(X_train, y_train)
 cv_baseline.groupby("model_name" ).accuracy.mean()
warnings.filterwarnings('ignore' )<load_from_csv>
classifier_SVC = SVC(C=200,
kernel='rbf',
degree=3,
gamma=1,
coef0=1,
shrinking=True,
tol=0.001,
probability=False,
cache_size=1000,
class_weight=None,
verbose=False,
max_iter=-1,
decision_function_shape=None,
random_state=None)
 model_SVC_OVR = OneVsRestClassifier(classifier_SVC)
 scores_SVC = cross_validate(model_SVC_OVR, X_train, y_train, cv=3)
 print("The test accuracy of SVC is {}".format(scores_SVC['test_score'].mean()))
df = pd.read_csv('.. /input/train.csv')
 x_data = df.drop(['ID_code','target'],axis=1 ).values
y_data = df['target'].values
df_test = pd.read_csv('.. /input/test.csv')
 x_test = df_test.drop('ID_code',axis=1 ).values<compute_test_metric>
SGD=SGDClassifier(max_iter=1000)
 model_SGD_OVR = OneVsRestClassifier(SGD)
 scores_SGD = cross_validate(model_SGD_OVR, X_train, y_train, cv=3)
 print("The test accuracy of SGD is {}".format(scores_SGD['test_score'].mean()))
np.max(np.triu(np.corrcoef(x_data,rowvar=False),k=1))<compute_test_metric>
Linear_SVC=LinearSVC()
model_LinearSVC_OVR = OneVsRestClassifier(Linear_SVC)
 scores_LinearSVC = cross_validate(model_LinearSVC_OVR, X_train, y_train, cv=3)
 print("The test accuracy of LinearSVC is {}".format(scores_LinearSVC['test_score'].mean()))
np.max(np.triu(np.corrcoef(x_data[y_data==1],rowvar=False),k=1))<compute_train_metric>
LogisticRegression=LogisticRegression(random_state=2019)
 model_LogisticRegression_OVR = OneVsRestClassifier(LogisticRegression)
 scores_LogisticRegression = cross_validate(model_LogisticRegression_OVR, X_train, y_train, cv=3)
 print("The test accuracy of LogisticRegression is {}".format(scores_LogisticRegression['test_score'].mean()))
<compute_test_metric><EOS> 
model_SVC_OVR.fit(X_train,y_train)
 y_pred=model_SVC_OVR.predict(X_test)
 y_pred=LabelEncoder.inverse_transform(y_pred)
 test['cuisine']=y_pred
test[['id','cuisine']].to_csv("submission.csv",index=False)