Kaggle competition: What's Cooking?
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?<set_options>
%matplotlib inline
DEBUG = False
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<set_options>
if(DEBUG):
train = pd.read_json('./train.json')
 test = pd.read_json('./test.json')
 else:
train = pd.read_json('.. /input/train.json')
 test = pd.read_json('.. /input/test.json')
 train.ingredients = train.ingredients.apply(lambda l: ", ".join(l))
test.ingredients = test.ingredients.apply(lambda l: ", ".join(l))
train.head()
print('Make sure cuda is installed:', torch.cuda.is_available())
 print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )<define_variables>
vectorizerIngr = CountVectorizer(analyzer = "word", tokenizer = None, preprocessor = None, stop_words = None,\
max_features = 50000, binary = False, token_pattern=r' (.+? )(?:,\s|$)')
 vectorizerIngr.fit(train['ingredients'])
 bagOfWords = vectorizerIngr.transform(train['ingredients'])
 bagOfWordsTest = vectorizerIngr.transform(test['ingredients'])
 vectorizerWords = CountVectorizer(analyzer = "word", tokenizer = None, preprocessor = None, stop_words = None,\
max_features = 50000, binary = False, token_pattern=r'(\w+? )(?:,\s|\s|$)')
 vectorizerWords.fit(train['ingredients'])
 bagOfWordsWords = vectorizerWords.transform(train['ingredients'])
 bagOfWordsTestWords = vectorizerWords.transform(test['ingredients'])
 vectorizerTfDf = TfidfVectorizer(analyzer = "word", tokenizer = None, preprocessor = None, stop_words = None,\
max_features = 50000, binary = False, token_pattern=r'(\w+? )(?:,\s|\s|$)')
 vectorizerTfDf.fit(train['ingredients'])
 bagOfWordsTfDf = vectorizerTfDf.transform(train['ingredients'])
 bagOfWordsTestTfDf = vectorizerTfDf.transform(test['ingredients'] )
hack_path = Path('.. /input' )<load_from_csv>
import warnings, sklearn
import scipy.sparse as sparse
from sklearn import *
train_df = pd.read_csv(hack_path/'train.csv')
 test_df = pd.read_csv(hack_path/'sample_submission.csv' )<load_pretrained>
bagOfWords = bagOfWords.astype('float')
 bagOfWordsWords = bagOfWordsWords.astype('float')
 kolvoWords = np.sum(bagOfWordsWords, axis=1)
 kolvoIngr = np.sum(bagOfWords, axis=1 )
def get_data(bs, size):
data = ImageDataBunch.from_df(df=train_df, path=hack_path/'train', folder='train',
bs=bs, size=size, valid_pct=0.1,
resize_method=ResizeMethod.SQUISH,
ds_tfms=get_transforms(do_flip=True, flip_vert=True,
max_lighting=0.2, max_zoom=1.1,
max_warp=0.2, max_rotate=10))
test_data = ImageList.from_df(test_df, path=hack_path/'test', folder='test')
 data.add_test(test_data)
 data.normalize(imagenet_stats)
 return data<define_variables>
lambdaKolvoIngr = 0.0606
lambdaKolvoWords = 0.05
lambdaTfDf = 1
lambdaBagOfW = 0.2121
lambdaWords = 0.116
data1 = sparse.hstack(( lambdaTfDf * bagOfWordsTfDf, \
bagOfWords * lambdaBagOfW, \
bagOfWordsWords * lambdaWords, \
kolvoWords*lambdaKolvoWords, \
kolvoIngr*lambdaKolvoIngr))
SVM = svm.LinearSVC(C=0.3)
 SVM.fit(data1, train['cuisine'] )
data = get_data(bs=64, size=128 )<define_variables>
bagOfWordsTest = bagOfWordsTest.astype('float')
 bagOfWordsTestWords = bagOfWordsTestWords.astype('float')
 kolvoWordsTest = np.sum(bagOfWordsTestWords, axis=1)
 kolvoIngrTest = np.sum(bagOfWordsTest, axis=1)
 dataTest = sparse.hstack(( lambdaTfDf * bagOfWordsTestTfDf, \
bagOfWordsTest * lambdaBagOfW, \
bagOfWordsTestWords * lambdaWords, \
kolvoWordsTest*lambdaKolvoWords, \
kolvoIngrTest*lambdaKolvoIngr))
class FuncCreator:
def __init__(self):
self.dict_cuisine = dict()
self.ind = 0
def __call__(self, it):
x = it.lower()
if not(x in self.dict_cuisine.keys()):
self.dict_cuisine[x] = self.ind
self.ind = self.ind + 1
return it
def getNameFromId(self, id):
for key, val in self.dict_cuisine.items() :
if(val == id):
return key
return "UNKNOW"
class MySVM:
def __init__(self, C=0.3):
self.SVMS = dict()
self.rep = FuncCreator()
self.keys=[]
self.C=C
def learn(self, data1, y, res, ar=[]):
self.rep = FuncCreator()
y.apply(self.rep)
 self.keys=np.array(list(self.rep.dict_cuisine.keys()))
i = 0
for key in self.keys:
ndata=y==key
if(i >= len(ar)) :
SVM = svm.LinearSVC(C=self.C)
 else:
SVM = svm.LinearSVC(C=ar[i])
 SVM.fit(data1, ndata)
 self.SVMS[key] = SVM
i = i + 1
def predict(self, data, ar=[]):
arr = np.zeros(( data.shape[0],len(self.keys)))
 i = 0
for key in self.keys:
if(len(ar)== 0 or(( len(ar)) < len(self.rep.dict_cuisine.keys()))):
arr[:,i]=self.SVMS[key].decision_function(data)
 else:
arr[:,i]=ar[i] * self.SVMS[key].decision_function(data)
 i = i+1
return self.keys[np.argmax(arr, axis=1)]
SVM = MySVM(C=0.29)
 SVM.learn(data1, train['cuisine'], 0, [0.25, 0.4, 0.25, 0.25, 0.1, 0.3, 0.3, 0.3, 0.2, 0.35, 0.35, 0.5, 0.25, 0.3, 0.3, 0.35, 0.6, 0.4, 0.55, 0.45])
 res = SVM.predict(dataTest )
<choose_model_class><EOS> 
f =open("svm_output.csv", "w")
 f.write("id,cuisine
")
 i = 0
for item in test["id"]:
f.write(str(item))
f.write(",")
 f.write(res[i])
 f.write("
")
 i= i +1
f.close()