Kaggle competition: What's Cooking? (Kernels Only)
def make_submission(name, path):
pd.DataFrame({'Path': path} ).to_csv(f'{name}.csv', index=False )<compute_test_metric>
def load_data(filename, flag):
with open(filename,'r')as fp:
json_data = json.load(fp)
 id_list = []
ingredients_list = []
cuisine_list = []
for row in json_data:
id_list.append(row['id'])
 ingredients_list.append(row['ingredients'])
 if(flag == 'train'):
cuisine_list.append(row['cuisine'])
 if(flag == 'train'):
df = pd.DataFrame(columns = ['id','ingredients','cuisine'])
 df['id'] = pd.Series(id_list)
 df['ingredients'] = pd.Series(ingredients_list)
 df['cuisine'] = pd.Series(cuisine_list)
 else:
df = pd.DataFrame(columns = ['id','ingredients'])
 df['id'] = pd.Series(id_list)
 df['ingredients'] = pd.Series(ingredients_list)
 return df
make_submission(score_path(path), path )<import_modules>
train = load_data('.. /input/train.json','train')
 test = load_data('.. /input/test.json','test')
 len(train), len(test)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sympy.ntheory.primetest import isprime
from numba import jit
from math import sqrt<load_from_csv>
def removespaces(ingredients):
ingreds = []
for ingred in ingredients:
ingred_mod = ingred.replace(" ","_")
 ingreds.append(ingred_mod)
 return ingreds
def removebracs(ingredients):
ingreds = ""
for ingred in ingredients:
ingred_mod = ingred.replace(" ","_")
 ingreds = ingreds + ingred_mod + ","
return ingreds[:-1]
train['ingredients_tfidf'] = train['ingredients'].apply(removebracs)
 test['ingredients_tfidf'] = test['ingredients'].apply(removebracs)
 train.head()
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv')
 cities['IsPrime'] = cities['CityId'].apply(isprime )<compute_test_metric>
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(train['ingredients_tfidf'])
 X_test = vectorizer.transform(test['ingredients_tfidf'] )
def compute_score(path):
path = path[ : len(path)-1]
cities_path = cities.reindex(path)
 isPrime = cities_path.IsPrime.values
coord = cities_path[['X', 'Y']].values
return compute_score_fast(coord, isPrime )<compute_test_metric>
lb = LabelEncoder()
y = lb.fit_transform(train['cuisine'])
 C = 1.0
svc = svm.SVC(kernel='linear', C=C)
 ovr = OneVsRestClassifier(svc)
 model = ovr.fit(X_train, y)
 y_preds=model.predict(X_test)
 preds = lb.inverse_transform(y_preds)
<compute_test_metric><EOS> 
sub = pd.DataFrame({'id': test['id'], 'cuisine': preds}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False)