Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<choose_model_class>
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
import pandas as pd
import json
model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'] )<train_model>
def read_dataset(path):
return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json' )
history = model.fit_generator(
 train_generator,
epochs = 50,
steps_per_epoch = 15,
validation_data = val_generator,
validation_steps = 7
)<predict_on_test>
def generate_text(data):
text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
return text_data
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
 )<prepare_output>
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
data = {'id': image_ids, 'has_cactus':predictions}
submission = pd.DataFrame(data)
 print(submission.head() )<save_to_csv>
print("TF-IDF on text data...")
 tfidf = TfidfVectorizer(binary=True)
 def tfidf_features(txt, flag):
if flag == "train":
x = tfidf.fit_transform(txt)
 else:
x = tfidf.transform(txt)
 x = x.astype('float16')
 return x
submission.to_csv("submission.csv", index=False )<save_to_csv>
X = tfidf_features(train_text, flag="train")
 X_test = tfidf_features(test_text, flag="test" )
submission.to_csv("submission.csv", index=False )<train_model>
lb = LabelEncoder()
y = lb.fit_transform(target )
base_dir=".. /input/"
train_dir=os.path.join(base_dir,"train/train")
 test_dir=os.path.join(base_dir,"test/test")
 print("Training images :
{}".format(os.listdir(train_dir)[:10]), end='
')
 print("Testing images :
{}".format(os.listdir(test_dir)[:10]))
testing_dir=os.path.join(base_dir,"test" )<load_from_csv>
classifier = 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)
 model = OneVsRestClassifier(classifier, n_jobs=4)
 model.fit(X, y )
<import_modules><EOS> 
print("Label Encode the Target Variable...")
 print("Train the model...")
 print("Predict on test data...")
 y_test = model.predict(X_test)
 y_pred = lb.inverse_transform(y_test)
 print("Generate Submission File...")
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': y_pred}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False )