Kaggle competition: Titanic - Machine Learning from Disaster
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 oof = np.zeros(len(train_df))
predictions_lgb_cv = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()<split>
dataset = pd.read_csv('/kaggle/input/titanic/train.csv')
 X = dataset.iloc[:, [2, 4, 5, 6, 7]].values
y = dataset.iloc[:, 1].values
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df,train_df['price'].values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 trn_data = lgb.Dataset(train_df.iloc[trn_idx][predictors], label=train_df.iloc[trn_idx][target])
 val_data = lgb.Dataset(train_df.iloc[val_idx][predictors], label=train_df.iloc[val_idx][target])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 100)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][predictors], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = predictors
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions_lgb_cv += clf.predict(test_df[predictors], num_iteration=clf.best_iteration)/ folds.n_splits
strRMSE = "RMSE: {}".format(rmse(oof, train_df[target]))
print(strRMSE )<save_to_csv>
test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data_X = test_data.iloc[:, [1, 3, 4, 5, 6]].values
submission = pd.read_csv(PATH+"sample_submission.csv")
 submission['price'] = predictions_lgb_cv
submission.to_csv('submission_cv.csv', index=False )<define_variables>
imputer_X = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
 imputer_X = imputer_X.fit(X[:, 2:3])
 X[:, 2:3] = imputer_X.transform(X[:, 2:3] )
predictions_blending = predictions_RF * 0.55 + predictions_lgb_cv * 0.45<save_to_csv>
imputer_test_data_X = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
 imputer_test_data_X = imputer_test_data_X.fit(test_data_X[:, 2:3])
 test_data_X[:, 2:3] = imputer_test_data_X.transform(test_data_X[:, 2:3] )
submission = pd.read_csv(PATH+"sample_submission.csv")
 submission['price'] = predictions_blending
submission.to_csv('submission_blending.csv', index=False )<import_modules>
labelencoder_X = LabelEncoder()
X[:, 1] = labelencoder_X.fit_transform(X[:, 1] )
from pathlib import Path
import json
import re
import pandas as pd
import numpy as np
from tqdm import tqdm_notebook<define_variables>
labelencoder_test_data_X = LabelEncoder()
test_data_X[:, 1] = labelencoder_test_data_X.fit_transform(test_data_X[:, 1] )
DATA_DIR = Path('.. /input/ndsc-advanced')
 BEAUTY_TRAIN_CSV = DATA_DIR / 'beauty_data_info_train_competition.csv'
FASHION_TRAIN_CSV = DATA_DIR / 'fashion_data_info_train_competition.csv'
MOBILE_TRAIN_CSV = DATA_DIR / 'mobile_data_info_train_competition.csv'
BEAUTY_TEST_CSV = DATA_DIR / 'beauty_data_info_val_competition.csv'
FASHION_TEST_CSV = DATA_DIR / 'fashion_data_info_val_competition.csv'
MOBILE_TEST_CSV = DATA_DIR / 'mobile_data_info_val_competition.csv'
LAST_SUBMITTED_CSV = Path('.. /input/aoeul-solution-step-3-linearsvc-dl-model/Ensembled_SVC_DL_predictions.csv' )<load_from_csv>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0 )
beauty_train_df = pd.read_csv(BEAUTY_TRAIN_CSV)
 fashion_train_df = pd.read_csv(FASHION_TRAIN_CSV)
 mobile_train_df = pd.read_csv(MOBILE_TRAIN_CSV)
 beauty_test_df = pd.read_csv(BEAUTY_TEST_CSV)
 fashion_test_df = pd.read_csv(FASHION_TEST_CSV)
 mobile_test_df = pd.read_csv(MOBILE_TEST_CSV)
 prev_subm_df = pd.read_csv(LAST_SUBMITTED_CSV )<define_variables>
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
 X_test = sc_X.transform(X_test)
 test_data_X = sc_X.transform(test_data_X )
categories = ['beauty', 'fashion', 'mobile']
train_dfs = [beauty_train_df, fashion_train_df, mobile_train_df]
test_dfs = [beauty_test_df, fashion_test_df, mobile_test_df]<merge>
classifier = SVC(kernel = 'rbf', random_state = 0)
 classifier.fit(X_train, y_train )
matched_label_df = pd.DataFrame()
for cat, train_df, test_df in zip(categories, train_dfs, test_dfs):
print(f'Matching {cat} training and test data...')
 df_train = train_df.rename(columns={'title': 'title_train', 'itemid': 'itemid_train'})
 df_test = test_df[['itemid', 'title']].rename(columns={'title': 'title_test', 'itemid': 'itemid_test'})
 df_combined = pd.merge(df_train, df_test, how='inner', left_on=['title_train'], right_on=['title_test'])
 df_combined.drop(['image_path','itemid_train', 'title_train'], axis=1, inplace=True)
 df_clean = df_combined.drop_duplicates()
print('Finding test data with exact same title as train data with unique labels')
 single_instance=[]
for title in tqdm_notebook(df_clean.title_test.unique()):
if(len(df_clean[df_clean.title_test==title])== 1):
single_instance.append(title)
 print(f'{len(single_instance)} test-train title pair matches found in {cat} category')
 single_inst_df = df_clean[df_clean.title_test.isin(single_instance)]
feat_cols = single_inst_df.columns.drop(['itemid_test', 'title_test'])
 id_label_list=[]
for _, row in single_inst_df.iterrows() :
for feat in feat_cols:
if not np.isnan(row[feat]):
itemid = '_'.join([str(row['itemid_test']), str(feat)])
 answer = str(int(row[feat]))
id_label_list.append(( itemid, answer))
prev_subm_df['first_pred'] = prev_subm_df['tagging'].apply(lambda x: x.split(' ')[0])
 list_new_id = []
list_new_ans = []
for id_ans in tqdm_notebook(id_label_list):
subm_first_label = int(prev_subm_df.loc[prev_subm_df.id==id_ans[0]]['first_pred'])
 if subm_first_label != int(id_ans[1]):
print(f'Test id: {id_ans[0]:28} Submitted label: {subm_first_label:<5} Train data label: {id_ans[1]}')
 list_new_id.append(id_ans[0])
 list_new_ans.append(id_ans[1])
 label_df = pd.DataFrame(
 {'id': list_new_id, 'tagging': list_new_ans},
columns = ['id', 'tagging'])
 matched_label_df = pd.concat([matched_label_df, label_df], axis=0)
 print()<save_to_csv>
y_pred = classifier.predict(test_data_X )
<import_modules><EOS> 
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_pred})
 output.to_csv('my_submission.csv', index=False )