Kaggle competition: What's Cooking? (Kernels Only)
%matplotlib inline
mpl.rc('figure', figsize=(15, 12))
plt.figure(figsize=(15, 12))
plt.rcParams['figure.facecolor'] = 'azure'
mpl.style.use('seaborn')
 plt.style.use('seaborn')
 set_matplotlib_formats('retina' )<set_options>
with open('.. /input/train.json')as f:
data = json.load(f)
 train_df = pd.DataFrame(data)
 final=[]
for el in train_df['ingredients']:
test= ' '.join(el ).lower()
final.append(test)
 vectorizer = TfidfVectorizer(binary = True)
 X = vectorizer.fit_transform(final)
 with open('.. /input/test.json')as f:
data_test = json.load(f)
 test_df = pd.DataFrame(data_test)
 final_test=[]
for el in test_df['ingredients']:
test= ' '.join(el ).lower()
final_test.append(test)
 X_test = vectorizer.transform(final_test )
sns.set(rc={'figure.figsize':(15, 12)})
 sns.set(context='notebook', style='darkgrid', font='sans-serif',
font_scale=1.1, rc={'figure.facecolor': 'azure',
'axes.facecolor': 'azure', 'grid.color': 'steelblue'})
 sns.color_palette(mcolors.TABLEAU_COLORS);<import_modules>
y=train_df['cuisine']
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.2 )
import missingno as msno<import_modules>
print('The scikit-learn version is {}.'.format(sklearn.__version__))
import scikitplot as skplt<import_modules>
models=[];accs=[];times=[]
def running(mod):
mod_name=mod[:-2]
print(mod_name)
 if mod_name not in models:
models.append(mod_name)
 start=time.time()
model=eval(mod)
 model.fit(X_train,y_train)
 y_model=model.predict(X_validation)
 end=time.time()
accs.append(metrics.accuracy_score(y_validation, y_model))
times.append(end-start)
 lista=['BernoulliNB() ',
'DecisionTreeClassifier() ',
'ExtraTreeClassifier() ',
'ExtraTreesClassifier() ',
'LogisticRegression() ',
'LinearSVC() ',
'RidgeClassifier() ',
'RandomForestClassifier() ',
'NearestCentroid() ']
rrun=False
if rrun==True:
for el in lista:
running(el)
 for_df = [('Model', models), 
('Accuracy', accs), 
('Time', times)]
df_models = pd.DataFrame.from_items(for_df)
 print(df_models )
cross_val_predict, cross_val_score
StratifiedKFold, GridSearchCV
RobustScaler, MaxAbsScaler, Normalizer
classification_report
precision_recall_fscore_support 
<import_modules>
rrun=True
if rrun==True:
start=time.time()
sdg = SGDClassifier().fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, sdg), 'in', end-start,'sec')
from sklearn.cluster import KMeans
from sklearn.ensemble import RandomForestClassifier<import_modules>
rrun=False
if rrun==True:
start=time.time()
test = XGBClassifier().fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, test), 'in', end-start,'sec')
from scipy import stats<import_modules>
rrun=False
if rrun==True:
start=time.time()
mod=LinearSVC()
linsvc = mod.fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, linsvc), 'in', end-start,'sec')
import xgboost as xgb
from xgboost import XGBClassifier, plot_tree, plot_importance<import_modules>
rrun=False
if rrun==True:
clf1 = LogisticRegression()
clf2 = LinearSVC()
clf3 = SGDClassifier()
eclf = VotingClassifier(estimators=[('lr', clf1),('LinearSVC', clf2),('SGD', clf3)], voting='hard')
 for clf, label in zip([clf1, clf2, clf3, eclf], ['Logistic Regression', 'LinearSVC', 'SGDClassifier', 'Ensemble']):
scores = cross_val_score(clf, X_train, y_train, cv=5, scoring='accuracy')
 print("Accuracy: %0.2f(+/- %0.2f)[%s]" %(scores.mean() , scores.std() , label))
import lightgbm as lgbm
from lightgbm import LGBMClassifier<import_modules>
rrun=False
if rrun==True:
param_grid = {'C':[1,50,75,100],'gamma':[1], 'kernel':['rbf']}
grid = GridSearchCV(SVC() ,param_grid,refit = True, verbose=2)
 grid.fit(X_train,y_train)
 grid.best_params_
predic = grid.predict(X_validation)
 print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, predic))
import catboost
from catboost import CatBoostClassifier<import_modules>
rrun=True
if rrun==True:
start=time.time()
svc=SVC(C=50,gamma=1,kernel='rbf')
 onevsall=OneVsRestClassifier(svc ).fit(X_train,y_train ).predict(X_validation)
 end=time.time()
print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, onevsall), 'in', end-start,'sec')
 conf_mat = confusion_matrix(y_validation, onevsall)
 fig, ax = plt.subplots(figsize=(10,10))
sns.heatmap(conf_mat, annot=True, fmt='d',xticklabels=set(list(y_validation)) , yticklabels=set(list(y_validation)))
 plt.ylabel('Actual')
 plt.xlabel('Predicted')
 plt.show()
print(metrics.classification_report(y_validation, onevsall, target_names=set(list(y_validation))))
import skopt
from skopt import BayesSearchCV<import_modules>
y_val_ch=[]
for el in y_validation:
if el=='russian':
y_val_ch.append(1)
 else:
y_val_ch.append(0)
 y_train_ch=[]
for el in y_train:
if el=='russian':
y_train_ch.append(1)
 else:
y_train_ch.append(0)
 ridge_ch=RidgeClassifier().fit(X_train,y_train_ch ).predict(X_validation)
 print(metrics.classification_report(y_val_ch, ridge_ch))
import imblearn
from imblearn.over_sampling import SMOTE, SMOTENC, RandomOverSampler
from imblearn.combine import SMOTEENN, SMOTETomek<import_modules>
y_val_vt=[]
for el in y_validation:
if el=='thai':
y_val_vt.append(1)
 else:
y_val_vt.append(0)
 y_train_vt=[]
for el in y_train:
if el=='thai':
y_train_vt.append(1)
 else:
y_train_vt.append(0)
 ridge_vt=RidgeClassifier().fit(X_train,y_train_vt ).predict(X_validation)
 print(metrics.classification_report(y_val_vt, ridge_vt))
print('Operating system version........ ', platform.platform())
 print('Python version is...............%s.%s.%s' % sys.version_info[:3])
 print('scikit-learn version is.........', sklearn.__version__)
 print('pandas version is...............', pd.__version__)
 print('numpy version is................ ', np.__version__)
 print('matplotlib version is...........', mpl.__version__)
 print('seaborn version is.............. ', sns.__version__)
 print('scikit-plot version is.......... ', skplt.__version__)
 print('missingno version is............ ', msno.__version__)
 print('xgboost version is.............. ', xgb.__version__)
 print('catboost version is.............', catboost.__version__)
 print('lightgbm version is.............', lgbm.__version__)
 print('scikit-optimize version is...... ', skopt.__version__)
 print('imblearn version is.............', imblearn.__version__ )<load_from_csv>
y_val_gr=[]
for el in y_validation:
if el=='moroccan':
y_val_gr.append(1)
 else:
y_val_gr.append(0)
 y_train_gr=[]
for el in y_train:
if el=='moroccan':
y_train_gr.append(1)
 else:
y_train_gr.append(0)
 ridge_gr=RidgeClassifier().fit(X_train,y_train_gr ).predict(X_validation)
 print(metrics.classification_report(y_val_gr, ridge_gr))
def getDatasetInformation(csv_filepath, is_corr_required=True):

 dataset_tmp = pd.read_csv(csv_filepath)
 dataset_tmp_shape = pd.DataFrame(list(dataset_tmp.shape),
index=['No of Rows', 'No of Columns'], columns=['Total'])
 dataset_tmp_shape = dataset_tmp_shape.reset_index()
dataset_tmp_dtypes = dataset_tmp.dtypes.reset_index()
dataset_tmp_dtypes.columns = ['Column Names', 'Column Data Types']
dataset_tmp_desc = pd.DataFrame(dataset_tmp.describe())
 dataset_tmp_desc = dataset_tmp_desc.transpose()
dataset_tmp_unique = dataset_tmp.nunique().reset_index()
dataset_tmp_unique.columns = ['Column Name', 'Unique Value(s)Count'
]
dataset_tmp_missing = dataset_tmp.isnull().sum(axis=0 ).reset_index()
dataset_tmp_missing.columns = ['Column Names',
'NULL value count per Column']
dataset_tmp_missing = \
dataset_tmp_missing.sort_values(by='NULL value count per Column'
, ascending=False)
 if is_corr_required:
dataset_tmp_corr = dataset_tmp.corr(method='spearman')
 else:
dataset_tmp_corr = pd.DataFrame()
return [
dataset_tmp,
dataset_tmp_shape,
dataset_tmp_dtypes,
dataset_tmp_desc,
dataset_tmp_unique,
dataset_tmp_missing,
dataset_tmp_corr,
]<feature_engineering>
final_res=[]
for i,j,ai,k,l,m in zip(ridge_ch,ridge_vt,ridge_gr,onevsall,logreg,sdg):
if i==1 and j==0 and ai==0:
if k=='southern_us' or k=='indian':
res='russian'
elif j==1 and i==0 and ai==0:
if k=='italian' or k=='vietnamese' or k=='british':
res='thai'
elif j==0 and i==0 and ai==1:
if k=='southern_us' or k=='indian' or k=='filipino' or k=='cajun_creole':
res='moroccan'
else:
res=k
final_res.append(res)
 print("The Model achieve Accuracy:",metrics.accuracy_score(y_validation, final_res))
print(metrics.classification_report(y_validation, final_res, target_names=set(list(y_validation))))
<filter><EOS>