Kaggle competition: What's Cooking? (Kernels Only)
total=df_train.isnull().sum().sort_values(ascending=False)
 percent=(df_train.isnull().sum() /df_train.isnull().count() ).sort_values(ascending=False)
 missing_data=pd.concat([total,percent], axis=1, keys=['Total','Percent'])
 missing_data.loc[missing_data['Total']>0].head(len(df_train.columns))<load_from_csv>
train_df = pd.read_json('.. /input/whats-cooking-kernels-only/train.json')
 train_df.columns = ['target', 'id', 'ingredients']
train_df["num_ingredients"] = train_df['ingredients'].apply(lambda x: len(x))
train_df = train_df[(train_df['num_ingredients'] > 1)]
print(train_df.shape)
 train_df_start = train_df.shape[0]
train_df.head()
y_bk = list(train_df['target'].copy().values )
df_test= pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
 total=df_test.isnull().sum().sort_values(ascending=False)
 percent=(df_test.isnull().sum() /df_test.isnull().count() ).sort_values(ascending=False)
 missing_data2=pd.concat([total,percent], axis=1, keys=['Total','Percent'])
 missing_data2.loc[missing_data2['Total']>0].head(len(df_test.columns))<define_variables>
test_df = pd.read_json('.. /input/whats-cooking-kernels-only/test.json')
 test_df['cousine']=np.nan
test_df=test_df[['cousine','id','ingredients']]
test_df.columns = ['target', 'id', 'ingredients']
test_df["num_ingredients"] = test_df['ingredients'].apply(lambda x: len(x))
print(test_df.shape)
 test_df.head()
print('Number of houses that were apparently remodelled in 1950:')
 l=list(df_train['YearBuilt'].loc[df_train['YearRemodAdd']==1950])
 print(len(l))
print('Number of houses that were apparently build in 1900:')
 l=list(df_train['YearRemodAdd'].loc[df_train['YearBuilt']==1900])
 print(len(l))
print('Number of houses that were apparently built in 1900 and remodelled in 1950:')
 print(len([year for year in l if year==1950]))
print('Fraction of houses that were remodelled:')
 print(len(list(df_train['YearRemodAdd'].loc[df_train['YearBuilt']!=df_train['YearRemodAdd']])) /len(df_train['YearRemodAdd']))<feature_engineering>
test_ids_for_sub = test_df['id'].values
test_ids_for_sub
df_train['FloorsSF']=pd.Series(len(df_train['YearBuilt']), index=df_train.index)
 df_train['FloorsSF']=df_train['1stFlrSF']+df_train['TotalBsmtSF']+df_train['2ndFlrSF']+df_train['GrLivArea']
medianG=df_train.loc[df_train['GarageCars']==1,'GarageArea'].median()
df_train['Garage']=pd.Series(len(df_train['YearBuilt']), index=df_train.index)
 df_train['Garage']=df_train['GarageCars']+1*df_train['GarageArea']/medianG
df_test['FloorsSF']=pd.Series(len(df_test['YearBuilt']), index=df_test.index)
 df_test['FloorsSF']=df_test['1stFlrSF']+df_test['TotalBsmtSF']+df_test['2ndFlrSF']+df_test['GrLivArea']
medianG=df_test.loc[df_test['GarageCars']==1,'GarageArea'].median()
df_test['Garage']=pd.Series(len(df_train['YearBuilt']), index=df_test.index)
 df_test['Garage']=df_test['GarageCars']+1*df_test['GarageArea']/medianG
corrmat=df_train.corr()
k = 11
cols = corrmat.nlargest(k, 'SalePrice')['SalePrice'].index
cm = np.corrcoef(df_train[cols].values.T)
 sns.set(font_scale=1)
 f1, ax1 = plt.subplots(figsize=(9,9))
sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=cols.values, xticklabels=cols.values)
 plt.show()<drop_column>
print("Combine Train and Submission")
 df = pd.concat([train_df, test_df],axis=0,ignore_index=True)
 del train_df, test_df
gc.collect()
print(df.shape)
 df.head()
df_train = df_train.drop(( missing_data[missing_data['Total'] > 0] ).index,1)
 df_test = df_test.drop(( missing_data[missing_data['Total'] > 0] ).index,1)
 index_list_noncat_exclude=['GrLivArea','1stFlrSF','GarageCars','GarageArea','TotalBsmtSF','2ndFlrSF','HouseStyle', 'Utilities', 'Exterior2nd', 'Condition2', 'Heating', 'Exterior1st', 'RoofMatl']
df_train = df_train.drop(index_list_noncat_exclude,1)
 df_test = df_test.drop(index_list_noncat_exclude,1)
 print('Print zero, if no data is missing in the new training set: ')
 df_train.isnull().sum().max() 
<feature_engineering>
lemmatizer = WordNetLemmatizer()
def preprocess(ingredients):
ingredients_text = ' '.join(ingredients)
 ingredients_text = ingredients_text.lower()
ingredients_text = ingredients_text.replace('-', ' ')
 words = []
for word in ingredients_text.split() :
if re.findall('[0-9]', word): continue
if len(word)<= 2: continue
if '’' in word: continue
word = lemmatizer.lemmatize(word)
 if len(word)> 0: words.append(word)
 return ' '.join(words)
 for ingredient, expected in [ 
('Eggs', 'egg'), 
('all-purpose flour', 'all purpose flour'), 
('purée', 'purée'), 
('1% low-fat milk', 'low fat milk'), 
('half & half', 'half half'), 
('safetida(powder)', 'safetida(powder)')
 ]:
actual = preprocess([ingredient])
 assert actual == expected, f'"{expected}" is excpected but got "{actual}"'
df_train['Centenarian']=pd.Series(len(df_train['YearBuilt']), index=df_train.index)
 df_train['Centenarian']=0
df_train.loc[df_train['YearBuilt']<1920,'Centenarian']=1
df_test['Centenarian']=pd.Series(len(df_test['YearBuilt']), index=df_test.index)
 df_test['Centenarian']=0
df_test.loc[df_test['YearBuilt']<1920,'Centenarian']=1
df_train['Remodelled']=pd.Series(len(df_train['YearBuilt']), index=df_train.index)
 df_train['Remodelled']=0
df_train.loc[df_train['YearBuilt']!=df_train['YearRemodAdd'],'Remodelled']=1
df_test['Remodelled']=pd.Series(len(df_test['YearBuilt']), index=df_test.index)
 df_test['Remodelled']=0
df_test.loc[df_test['YearBuilt']!=df_test['YearRemodAdd'],'Remodelled']=1
f1, ax1 = plt.subplots(figsize=(10, 10))
fig = sns.boxplot(x='Centenarian', y="SalePrice", data=df_train)
 f3, ax3 = plt.subplots(figsize=(10, 10))
fig = sns.boxplot(x='Remodelled', y="SalePrice", data=df_train )<train_on_grid>
df['ingredients'] = df['ingredients'].apply(lambda ingredients: preprocess(ingredients))
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, train, y, scoring="neg_mean_squared_error", cv = 10))
return(rmse)
 df_test = df_test.fillna(df_test.mean())
 df_train = pd.get_dummies(df_train)
 test = pd.get_dummies(df_test)
 cols=list(df_train.columns)
 cols.remove('SalePrice')
 train=df_train[cols]
y=df_train.SalePrice
model_lasso = LassoCV(alphas = [0.5, 0.005, 0.0005] ).fit(train, y)
 print('RMSE on the train set:')
 rmse_cv(model_lasso ).mean()<find_best_model_class>
print("TF-IDF on text data...")
 tfidf = TfidfVectorizer(binary=True)
 print("combine other features with text vectors")
 text_features = tfidf.fit_transform(df['ingredients'])
 print(text_features.shape)
 df_1 = pd.DataFrame(text_features.toarray())
 del text_features
gc.collect()
print(df_1.shape)
 print(df.shape)
 df_1.head()
dtrain = xgb.DMatrix(train, label = y)
 dtest = xgb.DMatrix(test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=200)
 model_xgb = xgb.XGBRegressor(n_estimators=360, max_depth=2, learning_rate=0.1)
 model_xgb.fit(train, y)
 xgb_preds = np.expm1(model_xgb.predict(test))
lasso_preds = np.expm1(model_lasso.predict(test))
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )<create_dataframe>
df = pd.concat([df, df_1, df_2],axis=1)
 del df_1
del df_2
gc.collect()
print(df.shape )
preds = 0.5*lasso_preds + 0.5*xgb_preds
solution = pd.DataFrame({"id":df_test.Id, "SalePrice":preds} )<save_to_csv>
df.columns =['target', 'id', 'ingredients', 'num_ingredients']+['TfidfV_'+str(n)for n in range(2867)]+['Ngram_'+str(n)for n in range(2000)]
solution.to_csv('result-with-best.csv', index=False )<import_modules>
df.drop('ingredients',inplace=True,axis=True)
 df.head()
import numpy as np
import pandas as pd<load_from_csv>
train_df = df[df['target'].notnull() ]
target = train_df['target']
print("Label Encode the Target Variable...")
 lb = LabelEncoder()
y = lb.fit_transform(target)
 len_y = len(set(y))

 bayes_trials = Trials()
tpe_algorithm = tpe.suggest
final_selection = [n for n in train_df.columns if n not in ['target', 'id']]
train_set = lgb.Dataset(train_df[final_selection], y)
 print(train_set)
 min_value = 100000
iteration = 0
def objective(params, n_folds = 5):
global iteration
iteration+=1

 params['num_leaves']=int(params['num_leaves'])
 params['min_data_in_leaf']=int(params['min_data_in_leaf'])
 params['max_depth']=int(params['max_depth'])
 params['min_child_weight']=int(params['min_child_weight'])
 params['colsample_bytree']=round(params['colsample_bytree'],5)
 params['reg_alpha']=round(params['reg_alpha'],5)
 params['reg_lambda']=round(params['reg_lambda'],5)
 params['subsample']=round(params['subsample'],5)
 params['learning_rate']=round(params['learning_rate'],5)
 params['learning_rate']=round(params['learning_rate'],5)
 cv_results = lgb.cv(params,
train_set,
nfold = n_folds,
num_boost_round = 10000,
early_stopping_rounds = 200,
metrics='multi_logloss',
seed = 50,
stratified=False)
 best_score = min(cv_results['multi_logloss-mean'])
 optimal_rounds =len(cv_results['multi_logloss-mean'])
 global min_value
if str(best_score)== '-inf':
best_score = min_value+1
if best_score < 0:
best_score = min_value+1
if best_score < min_value:
min_value = best_score
print(f'Round: {iteration}
the score is: {best_score:.3f}
the best score so far {min_value:.3f}')
 loss= best_score
of_connection = open(out_file, 'a')
 writer = csv.writer(of_connection)
 writer.writerow([loss, params, best_score, optimal_rounds])
 of_connection.close()
return {'loss': loss, 'params': params, 'status': STATUS_OK}
space = {
'subsample': hp.uniform('subsample', 0.2, 1),
'num_leaves': hp.quniform('num_leaves', 5, 150, 1),
'learning_rate': hp.loguniform('learning_rate', np.log(0.001), np.log(0.9)) ,
'min_child_samples': hp.quniform('min_child_samples', 1, 100, 1),
'min_data_in_leaf': hp.quniform('min_data_in_leaf', 1, 100, 1),
'reg_alpha': hp.uniform('reg_alpha', 0.0, 1.0),
'reg_lambda': hp.uniform('reg_lambda', 0.0, 1.0),
'colsample_bytree': hp.uniform('colsample_bytree', 0.2, 1.0),
'max_depth':hp.uniform('max_depth', 2, 15),
'min_child_weight':hp.quniform('min_child_weight', 1, 100, 1),
}
out_file = 'gbm_trials_essential.csv'
of_connection = open(out_file, 'w')
 writer = csv.writer(of_connection)
 writer.writerow(['loss', 'params', 'best_score', 'optimal_rounds'])
 of_connection.close()
MAX_EVALS = 40
best = fmin(fn = objective,
space = space,
algo = tpe.suggest,
max_evals = MAX_EVALS,
trials = bayes_trials)
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 y = train["SalePrice"]
X = train.drop(labels=["SalePrice"], axis=1)
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<define_variables>
params = {}
params['num_leaves']=int(best['num_leaves'])
 params['min_data_in_leaf']=int(best['min_data_in_leaf'])
 params['max_depth']=int(best['max_depth'])
 params['min_child_weight']=int(best['min_child_weight'])
 params['colsample_bytree']=round(best['colsample_bytree'],5)
 params['reg_alpha']=round(best['reg_alpha'],5)
 params['reg_lambda']=round(best['reg_lambda'],5)
 params['subsample']=round(best['subsample'],5)
 params['learning_rate']=round(best['learning_rate'],5)
cat_features = ["MSZoning", "MSSubClass", "Street", "Alley",
"LotShape", "LandContour", "Utilities", "LotConfig",
"LandSlope", "Neighborhood","Condition1",
"Condition2", "BldgType", "HouseStyle",
"RoofStyle", "RoofMatl", "Exterior1st", "Exterior2nd",
"MasVnrType", "ExterQual", "ExterCond", "Foundation",
"BsmtQual", "BsmtCond", "BsmtExposure", "BsmtFinType1",
"BsmtFinType2", "Heating", "HeatingQC", "CentralAir",
"Electrical", "Functional", "FireplaceQu", "GarageType",
"GarageFinish", "GarageQual", "GarageCond", "PavedDrive",
"SaleType", "SaleCondition", "PoolQC", "Fence", "MiscFeature",
"KitchenQual"]<define_variables>
len(target.unique() )
ordinal = ["OverallQual", "OverallCond", "YearBuilt", "YearRemodAdd",
"GarageYrBlt", "GarageCars", "MoSold", "YrSold"]<define_variables>
def kfold_lightgbm(train_df, test_df,
num_folds, lr = 0.02,
stratified = True, params={},
n_estimators=5000000, early_stopping_rounds= 200):
print("Starting LightGBM.Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
gc.collect()
if stratified:
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=1001)
 else:
folds = KFold(n_splits= num_folds, shuffle=True, random_state=1001)
 nclasses = len(train_df['target'].unique())
 oof_preds = np.zeros(( train_df.shape[0],nclasses))
sub_preds = np.zeros(( test_df.shape[0],nclasses))
feature_importance_df = pd.DataFrame()
feats = [f for f in train_df.columns if f not in ['target', 'id']]
print('len_feat', len(feats))
feature_importance_df['f']=feats
y = train_df['target']
train_df = train_df[feats]
for n_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df, y)) :
train_x, train_y = train_df.iloc[train_idx], y.iloc[train_idx]
valid_x, valid_y = train_df.iloc[valid_idx], y.iloc[valid_idx]
clf = LGBMClassifier(
 nthread=10,
n_estimators=n_estimators,
silent=-1,
objective='multiclass',
num_leaves=params['num_leaves'],
min_data_in_leaf=params['min_data_in_leaf'],
max_depth=params['max_depth'],
min_child_weight=params['min_child_weight'],
colsample_bytree=params['colsample_bytree'],
reg_alpha=params['reg_alpha'],
reg_lambda=params['reg_lambda'],
subsample=params['subsample'],
learning_rate=params['learning_rate'] 
)
 clf.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)],
verbose= 100, early_stopping_rounds= early_stopping_rounds)
 oof_preds[valid_idx] = clf.predict_proba(valid_x, num_iteration=clf.best_iteration_)
 preds = clf.predict_proba(test_df[feats], num_iteration=clf.best_iteration_)
 sub_preds += preds
feature_importance_df["fold"+str(n_fold)] = clf.feature_importances_
del train_x, train_y, valid_x, valid_y
gc.collect()
sub_preds = sub_preds/float(folds.n_splits)
 return feature_importance_df, clf, sub_preds, oof_preds
absolute = ["LotFrontage", "LotArea", "MasVnrArea", "BsmtFinSF1",
"BsmtFinSF2", "BsmtUnfSF", "TotalBsmtSF", "1stFlrSF", "2ndFlrSF",
"LowQualFinSF", "BsmtFullBath", "BsmtHalfBath", "FullBath",
"HalfBath", "BedroomAbvGr", "KitchenAbvGr", "TotRmsAbvGrd",
"Fireplaces", "GarageArea", "WoodDeckSF", "OpenPorchSF", "EnclosedPorch",
"3SsnPorch", "ScreenPorch", "PoolArea"]<count_missing_values>
test_df = df[df['target'].isnull() ]
del df
gc.collect()
test_df.head()
X[absolute].isnull().sum()<count_missing_values>
X[cat_features].isnull().sum()<count_missing_values>
feat_importance, clf, sub_preds, oof_preds = kfold_lightgbm(train_df, test_df, num_folds= 5, lr=0.01,
stratified= True, params=params, 
)
 feat_importance.to_csv('fimp_all.csv' )
X[ordinal].isnull().sum()<install_modules>
train_df['target'] = train_df['target'].sample(frac=1,random_state=1976 ).values
feat_importance_random, clf, sub_preds, oof_preds = kfold_lightgbm(train_df, test_df, num_folds= 5, lr=0.01,
stratified= True, params=params, 
)
 feat_importance_random.to_csv('fimp_all_random.csv' )
!pip install category_encoders<categorify>
cb_enc = ce.CatBoostEncoder(cols=cat_features, verbose=1)
 X = X.drop(labels=cat_features, axis=1 ).join(cb_enc.fit_transform(X[cat_features], y))
test = test.drop(labels=cat_features, axis=1 ).join(cb_enc.transform(test[cat_features]))
print(len(test))<create_dataframe>
feat_importance.set_index('f',inplace=True)
 feat_importance_random.set_index('f',inplace=True )
knn_imp = KNNImputer()
simp_imp = SimpleImputer(strategy="median")
 X = pd.DataFrame(simp_imp.fit_transform(X), columns=X.columns)
 test = pd.DataFrame(simp_imp.fit_transform(test), columns=test.columns )<count_missing_values>
importance = pd.DataFrame()
statistics = []
pvalues = []
for f in tqdm(feat_importance.index.values):
statistic, pvalue = stats.ks_2samp(feat_importance.loc[f], feat_importance_random.loc[f])
 statistics.append(statistic)
 pvalues.append(pvalue)
 importance['sum']=feat_importance.sum(axis=1 ).values
importance['random_sum']=feat_importance_random.sum(axis=1 ).values
importance['f']=feat_importance.index.values
importance.set_index('f',inplace=True)
 importance['sum']=importance['sum']+importance['random_sum'].min()
importance['random_sum']=importance['random_sum']+importance['random_sum'].min()
importance['fc']=(importance['sum']+1)/(importance['random_sum']+1)
 importance['pval']=pvalues
importance['ks_stat']=statistics
padj = multipletests(importance['pval'], method='bonferroni')
 importance['padj']=padj[1]
importance.sort_values('pval' ).head()
X[absolute].isnull().sum()<count_missing_values>
importance.loc['num_ingredients']
X[ordinal].isnull().sum()<import_modules>
feature_identified = list(importance[importance['fc']>1].index.values)
 print(len(feature_identified))
from vecstack import StackingTransformer
from sklearn.pipeline import Pipeline<import_modules>
train_set = lgb.Dataset(train_df[feature_identified], y)
 print(train_set)
 min_value = 100000
iteration = 0
def objective(params, n_folds = 5):
global iteration
iteration+=1

 params['num_leaves']=int(params['num_leaves'])
 params['min_data_in_leaf']=int(params['min_data_in_leaf'])
 params['max_depth']=int(params['max_depth'])
 params['min_child_weight']=int(params['min_child_weight'])
 params['colsample_bytree']=round(params['colsample_bytree'],5)
 params['reg_alpha']=round(params['reg_alpha'],5)
 params['reg_lambda']=round(params['reg_lambda'],5)
 params['subsample']=round(params['subsample'],5)
 params['learning_rate']=round(params['learning_rate'],5)
 params['learning_rate']=round(params['learning_rate'],5)
 cv_results = lgb.cv(params,
train_set,
nfold = n_folds,
num_boost_round = 1000000,
early_stopping_rounds = 200,
metrics='multi_logloss',
seed = 50,
stratified=False)
 best_score = min(cv_results['multi_logloss-mean'])
 optimal_rounds =len(cv_results['multi_logloss-mean'])
 global min_value
if str(best_score)== '-inf':
best_score = min_value+1
if best_score < 0:
best_score = min_value+1
if best_score < min_value:
min_value = best_score
print(f'Round: {iteration}
the score is: {best_score:.3f}
the best score so far {min_value:.3f}')
 loss= best_score
of_connection = open(out_file, 'a')
 writer = csv.writer(of_connection)
 writer.writerow([loss, params, best_score, optimal_rounds])
 of_connection.close()
return {'loss': loss, 'params': params, 'status': STATUS_OK}
space = {
'subsample': hp.uniform('subsample', 0.2, 1),
'num_leaves': hp.quniform('num_leaves', 5, 150, 1),
'learning_rate': hp.loguniform('learning_rate', np.log(0.001), np.log(0.9)) ,
'min_child_samples': hp.quniform('min_child_samples', 1, 100, 1),
'min_data_in_leaf': hp.quniform('min_data_in_leaf', 1, 100, 1),
'reg_alpha': hp.uniform('reg_alpha', 0.0, 1.0),
'reg_lambda': hp.uniform('reg_lambda', 0.0, 1.0),
'colsample_bytree': hp.uniform('colsample_bytree', 0.2, 1.0),
'max_depth':hp.uniform('max_depth', 2, 15),
'min_child_weight':hp.quniform('min_child_weight', 1, 100, 1),
}
out_file = 'gbm_trials_essential.csv'
of_connection = open(out_file, 'w')
 writer = csv.writer(of_connection)
 writer.writerow(['loss', 'params', 'best_score', 'optimal_rounds'])
 of_connection.close()
MAX_EVALS = 150
best = fmin(fn = objective,
space = space,
algo = tpe.suggest,
max_evals = MAX_EVALS,
trials = bayes_trials)
 print(best )
from catboost import CatBoostRegressor
from xgboost import XGBRegressor
from sklearn.ensemble import RandomForestRegressor<choose_model_class>
params = {}
params['num_leaves']=int(best['num_leaves'])
 params['min_data_in_leaf']=int(best['min_data_in_leaf'])
 params['max_depth']=int(best['max_depth'])
 params['min_child_weight']=int(best['min_child_weight'])
 params['colsample_bytree']=round(best['colsample_bytree'],5)
 params['reg_alpha']=round(best['reg_alpha'],5)
 params['reg_lambda']=round(best['reg_lambda'],5)
 params['subsample']=round(best['subsample'],5)
 params['learning_rate']=round(best['learning_rate'],5 )
d1 = 3
d2 = 7
d3 = 11
d4 = 13
rs = 42
L1_stack = StackingTransformer(
 estimators=[ 
(f'CatBoost depth={d1}', CatBoostRegressor(verbose=False, eval_metric="RMSE", random_seed=rs, depth=d1)) , 
(f'CatBoost depth={d2}', CatBoostRegressor(verbose=False, eval_metric="RMSE", random_seed=rs, depth=d2)) , 
(f'CatBoost depth={d3}', CatBoostRegressor(verbose=False, eval_metric="RMSE", random_seed=rs, depth=d3)) , 
(f'XGBoost depth={d1}', XGBRegressor(seed=rs, max_depth=d1)) , 
(f'XGBoost depth={d2}', XGBRegressor(seed=rs, max_depth=d2)) , 
(f'XGBoost depth={d3}', XGBRegressor(seed=rs, max_depth=d3)) , 
(f'RandomForest depth={d1}', RandomForestRegressor(random_state=rs, max_depth=d1)) , 
(f'RandomForest depth={d2}', RandomForestRegressor(random_state=rs, max_depth=d2)) , 
(f'RandomForest depth={d3}', RandomForestRegressor(random_state=rs, max_depth=d3))
],
regression=True,
verbose=2,
random_state=42,
n_folds=5,
variant="A"
)<choose_model_class>
train_df['target']=y_bk
feat_importance, clf, sub_preds, oof_preds = kfold_lightgbm(train_df[feature_identified+['target']],
test_df[feature_identified+['target']],
num_folds= 5, lr=0.01, stratified= True,params=params, 
)
 feat_importance.to_csv('fimp_final.csv' )
final_estimator = LinearRegression()<choose_model_class>
print(clf.classes_ )
model = Pipeline(
 steps=[ 
("L1_stack", L1_stack), 
("Final_estimator", final_estimator)
 ]
)<train_model>
sub = pd.DataFrame()
for index,c in enumerate(clf.classes_):
sub[c]=sub_preds[:,index]
cuisine = []
for item in sub.index.values:
cuisine.append(np.argmax(sub.loc[item]))
sub['cuisine1']=cuisine
sub['id']=test_ids_for_sub
model.fit(X, y )<predict_on_test>
temp_1 = pd.read_csv('.. /input/svm-12jul18/svm_output_None.csv')
 temp_2 = pd.read_csv('.. /input/let-s-cook-model/submission.csv')
 sub['cuisine2']=temp_1['cuisine']
sub['cuisine3']=temp_2['cuisine']
y_pred = model.predict(test )<save_to_csv>
sub['diff'] = [1 if a != b else 0 for a,b in zip(sub['cuisine2'], sub['cuisine3'])]
sub['diff'].value_counts()
sub[sub['diff']==1][['cuisine1','cuisine2','cuisine3']].head(50 )
submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 submission["SalePrice"] = y_pred
submission.to_csv('submission.csv', index=False )<import_modules>
cuisine = []
for index in sub.index.values:
temp = sub[['cuisine1', 'cuisine2', 'cuisine3']].iloc[index].value_counts()
temp_score = temp[0]
temp_res = temp.index.values[0]
if temp_score ==1:
temp_res = sub[['cuisine3']].iloc[index].values[0]
cuisine.append(temp_res)
 sub['cuisine']=cuisine
sub[['id','cuisine']].head()
<import_modules><EOS> 
sub[['id','cuisine']].to_csv('finalsub.csv',index=False )