Kaggle competition: What's Cooking? (Kernels Only)
plt.style.use('bmh')
 plt.rcParams['figure.figsize'] =(10, 10)
 title_config = {'fontsize': 20, 'y': 1.05}<load_from_csv>
training_data=pd.read_json('.. /input/train.json')
 test_data=pd.read_json('.. /input/test.json' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
training_data.head()
ingredients=[]
cuisines=[]
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<data_type_conversions>
for ingredient in training_data['ingredients']:
for element in ingredient:
if element not in ingredients:
ingredients.append(element )
X_train = train.iloc[:, 2:].values.astype('float64')
 y_train = train['target'].values
X_test = test.iloc[:, 1:].values.astype('float64' )<train_model>
for cuisine in training_data['cuisine']:
if cuisine not in cuisines:
cuisines.append(cuisine )
class RadiusNeighborsNB(BaseEstimator, ClassifierMixin):
def __init__(self, radius=1.0, steps=100, threshold=500):
self.radius = radius
self.steps = steps
self.threshold = threshold
def fit(self, X, y):
X =(X - X.mean(axis=0)) / X.std(axis=0)
 self.log_prior_ = np.log(np.bincount(y)) - np.log(len(y))
self.grid_ = np.linspace(-5, 5, self.steps)
 shape =(self.steps, X.shape[1], len(self.log_prior_))
self.log_prob_grid_ = np.full(shape, self.log_prior_)
 for i in range(shape[0]):
for j in range(shape[1]):
mask = np.abs(X[:, j] - self.grid_[i])< self.radius
total = mask.sum()
if total >= self.threshold:
self.log_prob_grid_[i, j] =(np.log(np.bincount(y[mask]))
- np.log(total))
return self
def predict_proba(self, X):
X =(X - X.mean(axis=0)) / X.std(axis=0)
 shape =(*X.shape, len(self.log_prior_))
log_prob = np.empty(shape)
 for j in range(shape[1]):
lookup = np.searchsorted(self.grid_, X[:, j])
 lookup[lookup == len(self.grid_)] -= 1
log_prob[:, j] = self.log_prob_grid_[lookup, j]
log_posterior = log_prob.sum(axis=1)-(X.shape[1] - 1)* self.log_prior_
return np.exp(log_posterior - logsumexp(log_posterior))
def predict(self, X):
return self.predict_proba(X ).argmax(axis=1 )<split>
X_train=training_data.copy()
X_test=test_data.copy()
i_train, i_valid = next(StratifiedShuffleSplit(n_splits=1 ).split(X_train, y_train))<train_model>
for element in ingredients:
X_train[element]=0
X_test[element]=0
model = RadiusNeighborsNB(radius=0.35, threshold=30)
 model.fit(X_train[i_train], y_train[i_train])
 print(f'Training AUC is {roc_auc_score(y_train[i_train], model.predict_proba(X_train[i_train])[:, 1])}.')
 print(f'Validation AUC is {roc_auc_score(y_train[i_valid], model.predict_proba(X_train[i_valid])[:, 1])}.' )<train_model>
index =0
for row in X_train['ingredients']:
for element in row:
X_train.at[index,element]=1
index = index + 1
model.fit(X_train, y_train )<train_model>
index =0
for row in X_test['ingredients']:
for element in row:
X_test.at[index,element]=1
index = index + 1
model.fit(X_train, y_train )<save_to_csv>
X_test= X_test.dropna(axis='columns')
 X_test= X_test.drop(['id','ingredients'],axis=1)
 X_train=X_train.drop(['id','ingredients','cuisine'],axis=1 )
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = model.predict_proba(X_test)[:, 1]
submission.to_csv('submission.csv', index=False )<import_modules>
Y_train=training_data.copy()
Y_train=Y_train.drop(['id','ingredients'],axis=1)
 le=LabelEncoder()
Y_train=le.fit_transform(Y_train)
 Y_train= to_categorical(Y_train )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
from scipy.stats import norm
from scipy import stats<load_from_csv>
prediction_deep=[]
for x in Y_pred_DL:
prediction_deep.append(np.argmax(x))
prediction_deep=le.inverse_transform(prediction_deep )
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 corrmat=df_train.corr()
f, ax = plt.subplots(figsize=(10,10))
sns.heatmap(corrmat, vmax=1, square=True )<sort_values>
submission_DL=test_data.copy()
submission_DL=submission_DL.drop(['ingredients'],axis=1)
 submission_DL['cuisine']=prediction_deep
submission_DL.to_csv('sample_submission.csv',index=False )