Kaggle competition: What's Cooking? (Kernels Only)
<SOS> metric: CategorizationAccuracy Kaggle data source: whats-cooking?-(kernels-only)<create_dataframe>
from sklearn.model_selection import GridSearchCV
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
train_datagen = ImageDataGenerator(
 rescale=1/255,
validation_split=0.10,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest' 
)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = train_dataframe,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(75,75),
subset="training",
batch_size=250,
shuffle=True,
class_mode="binary" 
)
 valid_generator = train_datagen.flow_from_dataframe(
 dataframe = train_dataframe,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(75,75),
subset="validation",
batch_size=125,
shuffle=True,
class_mode="binary"
)<choose_model_class>
print("Read Dataset...")
 def read_dataset(path):
	return json.load(open(path))
train = read_dataset('.. /input/train.json')
 test = read_dataset('.. /input/test.json')
 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 )
pretrained_net = EfficientNetB3(
 input_shape=(75,75,3),
include_top=False,
pooling='max', 
)
 model = Sequential()
model.add(pretrained_net)
 model.add(Dense(units = 120, activation='relu'))
model.add(Dense(units = 84, activation = 'relu'))
model.add(Dense(units = 1, activation='sigmoid'))<choose_model_class>
print("Train the model...")
 parameters = {'kernel':('linear', 'rbf', 'poly'), 'C':[1, 100]}
classifier = SVC(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 = GridSearchCV(classifier, parameters)
 model.fit(X, y )
model.compile(optimizer=keras.optimizers.Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'] )<train_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 )