Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<data_type_conversions>
print("Read Dataset...")
 def read_dataset(path):
	return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json')
 shuffle(train)
 shuffle(train)
 print("Prepare text data of Train and Test...")
 def generate_text(data):
	text_data = [" ".join(doc['ingredients'] ).lower() for doc in data]
	return text_data
train_text = generate_text(train)
 test_text = generate_text(test)
 target = [doc['cuisine'] for doc in train]
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
X = tfidf_features(train_text, flag="train")
 X_test = tfidf_features(test_text, flag="test")
 print("Label Encode the Target Variable...")
 lb = LabelEncoder()
y = lb.fit_transform(target )
<feature_engineering>
print("Label Encode the Target Variable...")
 lb = LabelEncoder()
y = lb.fit_transform(target)
 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)
 return x
X = tfidf_features(train_text, flag="train")
 X_test = tfidf_features(test_text, flag="test")
for f in train_features_real.columns[0:200]:
train_features_real[f+'distance_of_mean'] = train_features_real[f]-train_features_real[f].mean()
real_data[f+'distance_of_mean'] = real_data[f]-real_data[f].mean()
train_features_real[f+'distance_of_mean'] = train_features_real[f+'distance_of_mean']*train_features_real[f+'duplicate']
real_data[f+'distance_of_mean'] = real_data[f+'distance_of_mean']*real_data[f+'duplicate'] 
<set_options>
X=X.toarray().tolist()
X_test = X_test.toarray().tolist()
print(y[1:3] )
del train_features
del test_features
gc.collect()<define_variables>
Y=y
class_weight_ = class_weight.compute_class_weight('balanced',
np.unique(y),
y)
 class_weights={}
for i in range(len(class_weight_)) :
class_weights[i]=100/class_weight_[i]
X=np.array(X)
 X_test=np.array(X_test )
n_splits = 10<init_hyperparams>
model = Sequential()
model.add(Dense(500, activation='relu',input_shape=(3010,)))
 model.add(Dropout(0.25))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(len(Y[0]), activation='softmax'))
sgd = optimizers.SGD(lr=.1, decay=1e-6, momentum=0.9, nesterov=True)
 model.compile(optimizer='nadam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 model.summary()
param = {
'bagging_freq': 5,
'bagging_fraction': 0.33,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.0085,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 12,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<prepare_x_and_y>
<prepare_x_and_y>
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
oof = np.zeros(len(train_features_real))
predictions = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()
features = [c for c in train_features_real.columns if c not in ['ID_code', 'target']]
for i,(train_idx, valid_idx)in enumerate(splits):
print(f'Fold {i + 1}')
 x_train = np.array(train_features_real)
 y_train = np.array(train_target)
 trn_data = lgb.Dataset(x_train[train_idx.astype(int)], label=y_train[train_idx.astype(int)])
 val_data = lgb.Dataset(x_train[valid_idx.astype(int)], label=y_train[valid_idx.astype(int)])
 num_round = 100000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 1000)
 oof[valid_idx] = clf.predict(x_train[valid_idx], num_iteration=clf.best_iteration)
 f
predictions[real_data.index] += clf.predict(real_data, num_iteration=clf.best_iteration)/ n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(train_target, oof)) )<feature_engineering>
model = DecisionTreeClassifier(criterion = "gini", random_state = 1000,
max_depth=1000, min_samples_leaf=1)
 model.fit(X, Y )
predictions[fake_data.index] = 0.5<compute_test_metric>
score = model.score(X[33000:],Y[33000:])
 print('Test accuracy:', score )
print('LightBGM auc = {:<8.5f}'.format(roc_auc_score(train_target, oof)))
<define_variables>
Ans= model.predict(X_test)
 print(Ans[0])
 Ans= lb.inverse_transform(Ans )
<create_dataframe><EOS> 
print("Generate Submission File...")
 test_id = [doc['id'] for doc in test]
sub = pd.DataFrame({'id': test_id, 'cuisine': Ans}, columns=['id', 'cuisine'])
 sub.to_csv('svm_output.csv', index=False )