Kaggle competition: What's Cooking?
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<predict_on_test>
train_arc = zipfile.ZipFile('/kaggle/input/whats-cooking/train.json.zip','r')
 train_data = pd.read_json(train_arc.read('train.json'))
train_data.head()
stacked_averaged_models.fit(train.values, target)
 stacked_train_pred = stacked_averaged_models.predict(train.values)
 stacked_pred = np.expm1(stacked_averaged_models.predict(test.values))
print(rmsle(target, stacked_train_pred))<data_type_conversions>
test_arc = zipfile.ZipFile('/kaggle/input/whats-cooking/test.json.zip','r')
 test_data = pd.read_json(test_arc.read('test.json'))
test_data.head()
od = train.columns[train.dtypes == 'object']
for col in od:
train[col] = train[col].astype('float')
 test[col] = test[col].astype('float' )<predict_on_test>
train_data['cuisine'].value_counts()
model_xgb.fit(train, target)
 xgb_train_pred = model_xgb.predict(train)
 xgb_pred = np.expm1(model_xgb.predict(test))
print(rmsle(target, xgb_train_pred))<predict_on_test>
train_ingredients_count = {}
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
if j in train_ingredients_count.keys() :
train_ingredients_count[j]+=1
else:
train_ingredients_count[j] = 1
model_lgb.fit(train, target)
 lgb_train_pred = model_lgb.predict(train)
 lgb_pred = np.expm1(model_lgb.predict(test.values))
print(rmsle(target, lgb_train_pred))<compute_test_metric>
train_ingredients_count['romaine lettuce']
print('RMSLE score on train data:')
 print(rmsle(target,stacked_train_pred*0.70 +
xgb_train_pred*0.15 + lgb_train_pred*0.15)) 
<define_variables>
test_ingredients_count = {}
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
if j in test_ingredients_count.keys() :
test_ingredients_count[j]+=1
else:
test_ingredients_count[j] = 1
ensemble = stacked_pred*0.70 + xgb_pred*0.15 + lgb_pred*0.15 
<save_to_csv>
train_ingred_miss = []
for i in test_ingredients_count.keys() :
if i not in train_ingredients_count.keys() :
train_ingred_miss.append(i )
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = ensemble
sub.to_csv('ensemble.csv',index=False )<load_from_csv>
for i in train_ingred_miss:
train_ingredients_count[i] = 0
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 sub = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv" )<set_options>
test_ingred_miss = []
for i in train_ingredients_count.keys() :
if i not in test_ingredients_count.keys() :
test_ingred_miss.append(i )
pd.set_option('display.max_columns', None)
 train.head()<concatenate>
for i in test_ingred_miss:
test_ingredients_count[i] = 0
len(test_ingredients_count )
n_train = len(train)
 all_features = pd.concat([train, test], sort=False ).reset_index(drop=True )<data_type_conversions>
for i in train_ingredients_count.keys() :
train_data[i] = np.zeros(len(train_data))
all_features['HasWoodDeck'] =(all_features['WoodDeckSF'] == 0)* 1
all_features['HasOpenPorch'] =(all_features['OpenPorchSF'] == 0)* 1
all_features['HasEnclosedPorch'] =(all_features['EnclosedPorch'] == 0)* 1
all_features['Has3SsnPorch'] =(all_features['3SsnPorch'] == 0)* 1
all_features['HasScreenPorch'] =(all_features['ScreenPorch'] == 0)* 1
all_features['YearsSinceRemodel'] = all_features['YrSold'].astype(int)- all_features['YearRemodAdd'].astype(int)
 all_features['YearsSinceBuilt'] = all_features['YrSold'].astype(int)- all_features['YearBuilt'].astype(int)
 all_features['Total_Home_Quality'] = all_features['OverallQual'] + all_features['OverallCond']
all_features = all_features.drop(['Utilities', 'Street', 'PoolQC',], axis=1)
 all_features['TotalSF'] = all_features['TotalBsmtSF'] + all_features['1stFlrSF'] + all_features['2ndFlrSF']
all_features['Total_sqr_footage'] =(all_features['BsmtFinSF1'] + all_features['BsmtFinSF2'] +
all_features['1stFlrSF'] + all_features['2ndFlrSF'])
 all_features['Total_Bathrooms'] =(all_features['FullBath'] +(0.5 * all_features['HalfBath'])+
all_features['BsmtFullBath'] +(0.5 * all_features['BsmtHalfBath']))
all_features['Total_porch_sf'] =(all_features['OpenPorchSF'] + all_features['3SsnPorch'] +
all_features['EnclosedPorch'] + all_features['ScreenPorch'] +
all_features['WoodDeckSF'])
 all_features['TotalBsmtSF'] = all_features['TotalBsmtSF'].apply(lambda x: np.exp(6)if x <= 0.0 else x)
 all_features['2ndFlrSF'] = all_features['2ndFlrSF'].apply(lambda x: np.exp(6.5)if x <= 0.0 else x)
 all_features['GarageArea'] = all_features['GarageArea'].apply(lambda x: np.exp(6)if x <= 0.0 else x)
 all_features['GarageCars'] = all_features['GarageCars'].apply(lambda x: 0 if x <= 0.0 else x)
 all_features['LotFrontage'] = all_features['LotFrontage'].apply(lambda x: np.exp(4.2)if x <= 0.0 else x)
 all_features['MasVnrArea'] = all_features['MasVnrArea'].apply(lambda x: np.exp(4)if x <= 0.0 else x)
 all_features['BsmtFinSF1'] = all_features['BsmtFinSF1'].apply(lambda x: np.exp(6.5)if x <= 0.0 else x)
 all_features['haspool'] = all_features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 all_features['has2ndfloor'] = all_features['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 all_features['hasgarage'] = all_features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 all_features['hasbsmt'] = all_features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 all_features['hasfireplace'] = all_features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
<prepare_x_and_y>
for i in test_ingredients_count.keys() :
test_data[i] = np.zeros(len(test_data))
test = all_features[n_train:].reset_index(drop=True)
 train = all_features[:n_train]<categorify>
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
train_data[j].iloc[i] = 1
def label_encoding(train: pd.DataFrame, test: pd.DataFrame, col_definition: list):
n_train = len(train)
 train = pd.concat([train, test], sort=False ).reset_index(drop=True)
 for column in col_definition:
try:
lbl = preprocessing.LabelEncoder()
train[column] = lbl.fit_transform(list(train[column].values))
except:
print(column)
 test = train[n_train:].reset_index(drop=True)
 train = train[:n_train]
return train, test<categorify>
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
test_data[j].iloc[i] = 1
categorical_cols = list(train.select_dtypes(include=['object'] ).columns)
 print(categorical_cols)
 train, test = label_encoding(train, test, categorical_cols )<import_modules>
test_data=test_data[train_data.drop('cuisine',axis=1 ).columns]
import optuna.integration.lightgbm as lgb<prepare_x_and_y>
from sklearn.linear_model import LogisticRegression
from sklearn import preprocessing
X = train.drop(['Id', 'SalePrice'], axis=1)
 y = np.log1p(train['SalePrice'])
 X_test = test.drop(['Id','SalePrice',], axis=1 )<drop_column>
X = train_data.drop(['id','cuisine','ingredients'],axis =1)
 Y = train_data['cuisine']
outliers = [30, 88, 462, 631, 1322]
X = X.drop(X.index[outliers])
 X.reset_index(inplace=True, drop=True)
 y = y.drop(y.index[outliers])
 y.reset_index(inplace=True, drop=True )<split>
X_train,X_val,y_train,y_val = train_test_split(X,Y,random_state =42 )
y_preds = []
models = []
oof_train = np.zeros(( len(X),))
cv = KFold(n_splits=10, shuffle=True, random_state=0)
 params = {
'num_leaves': 24,
'max_depth': 6,
'objective': 'regression',
'metric': 'rmse',
'learning_rate': 0.05
}
for fold_id,(train_index, valid_index)in enumerate(cv.split(X)) :
X_tr = X.loc[train_index, :]
X_val = X.loc[valid_index, :]
y_tr = y[train_index]
y_val = y[valid_index]
lgb_train = lgb.Dataset(X_tr,
y_tr,
categorical_feature=categorical_cols)
 lgb_eval = lgb.Dataset(X_val,
y_val,
reference=lgb_train,
categorical_feature=categorical_cols)
 model = lgb.train(params,
lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=10,
num_boost_round=1000,
early_stopping_rounds=10)
 oof_train[valid_index] = model.predict(X_val,
num_iteration=model.best_iteration)
 y_pred = model.predict(X_test,
num_iteration=model.best_iteration)
 y_preds.append(y_pred)
 models.append(model )<compute_test_metric>
lr = LogisticRegression(solver='liblinear')
 lr.fit(X_train,y_train )
print(f'CV: {np.sqrt(mean_squared_error(y, oof_train)) }' )<save_to_csv>
lr.score(X_val,y_val )
y_sub = sum(y_preds)/ len(y_preds)
 y_sub = np.expm1(y_sub)
 sub['SalePrice'] = y_sub
sub.to_csv('submission.csv', index=False)
 sub.head()<load_from_csv>
test_data['cuisine'] = lr.predict(test_data.drop(['id','ingredients'],axis=1))
data = pd.read_csv('/kaggle/input/santander-customer-transaction-prediction/train.csv')
 data.info()<count_missing_values>
Submission=test_data[['id','cuisine']]
Submission.set_index('id',inplace=True )
missing = False
for i in data.columns:
if len(pd.notnull(data[i])==True)!=len(data):
print(i, ": ",len(pd.notnull(data[i])))
 missing = True
if not missing:
print("No missing Data in train data." )<load_from_csv>
Submission.to_csv('Submission.csv' )