Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
warnings.filterwarnings("ignore", category=DeprecationWarning)
 sns.set()
%matplotlib inline
pd.set_option('display.float_format', lambda x: '%.3f' % x)
 pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_columns', 100)
 print('Done' )<load_from_csv>
train_data = pd.read_csv(".. /input/titanic/train.csv" )
path = '.. /input/competitive-data-science-predict-future-sales/'
DF_sales = pd.read_csv(path + 'sales_train.csv')
 DF_items = pd.read_csv(path + 'items.csv')
 DF_item_cat = pd.read_csv(path + 'item_categories.csv')
 DF_shops = pd.read_csv(path + 'shops.csv')
 DF_test = pd.read_csv(path + 'test.csv')
 DF_sample_subs = pd.read_csv(path + 'sample_submission.csv' )<count_missing_values>
train_data.drop(columns=['Name', 'Ticket', 'Cabin','PassengerId'], axis=1, inplace=True )
print('---' * 10)
 print('sales_train check
')
 print(DF_sales.isna().sum())
 print('---' * 10)
 print('item_cat check
')
 print(DF_items.isna().sum())
 print('---' * 10)
 print('item check
')
 print(DF_item_cat.isna().sum())
 print('---' * 10)
 print('shops check
')
 print(DF_shops.isna().sum() )<feature_engineering>
train_data['Sex'] = train_data['Sex'].map({'male':0,'female':1} )
DF_sales = DF_sales[DF_sales.item_price<100000]
DF_sales = DF_sales[DF_sales.item_cnt_day<1000]
DF_sales = DF_sales[DF_sales.item_price > 0].reset_index(drop=True)
 DF_sales.loc[DF_sales.item_cnt_day < 0, 'item_cnt_day'] = 0<feature_engineering>
train_data['Age'].isnull().sum()
DF_sales.loc[DF_sales.shop_id == 0, 'shop_id'] = 57
DF_sales.loc[DF_sales.shop_id == 1, 'shop_id'] = 58
DF_sales.loc[DF_sales.shop_id == 11, 'shop_id'] = 10<feature_engineering>
train_data['Embarked'].value_counts()
DF_shops.loc[DF_shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
DF_shops['shop_city'] = DF_shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 DF_shops['shop_cat'] = DF_shops['shop_name'].str.split(' ' ).map(lambda x: x[1])
 DF_shops.head(5 )<categorify>
train_data['Embarked'].fillna(value='S',axis=0, inplace=True )
DF_shops['shop_city'] = LabelEncoder().fit_transform(DF_shops['shop_city'])
 DF_shops['shop_cat'] = LabelEncoder().fit_transform(DF_shops['shop_cat'])
 DF_shops.drop(['shop_name'], axis=1, inplace= True)
 DF_shops.head(5 )<merge>
train_data['Embarked'] = train_data['Embarked'].map({'S':0,'C':1,'Q':2} )
DF_items = pd.merge(DF_items, DF_item_cat, on = 'item_category_id')
 DF_items<feature_engineering>
train_data.fillna(value=train_data['Age'].mean() , axis=0, inplace=True )
DF_items['item_sub_cat_1'] = np.select(
 [DF_items.item_category_id.isin(range(0,8)) ,
DF_items.item_category_id.isin(range(10,18)) ,
DF_items.item_category_id.isin(range(18,32)) ,
DF_items.item_category_id.isin(range(32,37)) ,
DF_items.item_category_id.isin(range(37,42)) ,
DF_items.item_category_id.isin(range(42,55)) ,
DF_items.item_category_id.isin(range(55,61)) ,
DF_items.item_category_id.isin(range(61,73)) ,
DF_items.item_category_id.isin(range(73,79)) ,
DF_items.item_category_id.isin([8,80]),
DF_items.item_category_id==83,
DF_items.item_category_id==9,
DF_items.item_category_id==79,
DF_items.item_category_id.isin([81,82])],
['accessories','consoles','games','payment_cards','Cinema','books','music',
'gifts','programs','tickets','batteries','delivery','office','discs'])
 DF_items<categorify>
train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean()
DF_items['item_sub_cat_1'] = LabelEncoder().fit_transform(DF_items['item_sub_cat_1'])
 DF_items.drop(['item_name','item_category_name'], axis=1, inplace= True)
 DF_items<data_type_conversions>
train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean()
DF_all = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = DF_sales[DF_sales.date_block_num==i]
DF_all.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
DF_all = pd.DataFrame(np.vstack(DF_all), columns=cols)
 DF_all['date_block_num'] = DF_all['date_block_num'].astype(np.int8)
 DF_all['shop_id'] = DF_all['shop_id'].astype(np.int8)
 DF_all['item_id'] = DF_all['item_id'].astype(np.int16)
 DF_all.sort_values(cols,inplace=True)
 DF_all<feature_engineering>
train_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean()
DF_test.drop(['ID'], axis=1, inplace = True)
 DF_test['date_block_num'] = 34
DF_test['date_block_num'] = DF_test['date_block_num']
DF_test['shop_id'] = DF_test['shop_id']
DF_test['item_id'] = DF_test['item_id']
DF_test.head()<merge>
train_data[['SibSp', 'Survived']].groupby('SibSp', as_index=False ).mean()
DF_all = pd.concat([DF_all, DF_test], ignore_index=True, sort=False, keys=cols)
 DF_all = pd.merge(DF_all, DF_shops, on=['shop_id'], how='left')
 DF_all = pd.merge(DF_all, DF_items, on=['item_id'], how='left')
 DF_all.fillna(0, inplace=True)
 DF_all<data_type_conversions>
train_data[['Parch', 'Survived']].groupby('Parch', as_index=False ).mean()
DF_all.date_block_num = DF_all.date_block_num.astype(np.int8)
 DF_all.shop_id = DF_all.shop_id.astype(np.int8)
 DF_all.item_id = DF_all.item_id.astype(np.int16)
 DF_all.shop_city = DF_all.shop_city.astype(np.int8)
 DF_all.shop_cat = DF_all.shop_cat.astype(np.int8)
 DF_all.item_category_id = DF_all.item_category_id.astype(np.int8)
 DF_all.item_sub_cat_1 = DF_all.item_sub_cat_1.astype(np.int8 )<merge>
train_data['AgeBand'] = pd.cut(train_data['Age'], 9 )
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
def fill_na_test(df):
for col in df.columns:
if('_lag' in col)&(df[col].isnull().any()):
print(col)
 if('month' in col):
df[col].fillna(0, inplace=True)
 return df<data_type_conversions>
train_data[['AgeBand','Survived']].groupby('AgeBand', as_index=False ).mean()
temp = DF_sales.groupby(['shop_id','item_id','date_block_num'] ).agg(item_cnt_month=('item_cnt_day',sum))
temp.columns = ['item_cnt_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=cols, how='left')
 DF_all['item_cnt_month'] =(DF_all['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))<merge>
train_data.loc[ train_data['Age'] <= 18, 'Age'] = 0
train_data.loc[(train_data['Age'] > 18)&(train_data['Age'] <= 44), 'Age'] = 1
train_data.loc[(train_data['Age'] > 44)&(train_data['Age'] <= 53), 'Age'] = 2
train_data.loc[(train_data['Age'] > 53)&(train_data['Age'] <= 62), 'Age'] = 3
train_data.loc[ train_data['Age'] > 62, 'Age'] = 4
ts = time.time()
DF_all = lag_feature(DF_all, [1, 2, 3], 'item_cnt_month')
 temp = DF_all.groupby(['date_block_num'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num'], how='left')
 DF_all = lag_feature(DF_all, [1, 2, 3], 'avg_month')
 DF_all.drop(['avg_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_item_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'item_id'], how='left')
 DF_all = lag_feature(DF_all, [1, 2, 3], 'avg_item_month')
 DF_all.drop(['avg_item_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shop_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id'], how='left')
 DF_all = lag_feature(DF_all, [1, 2, 3], 'avg_shop_month')
 DF_all.drop(['avg_shop_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'item_category_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_item_cat_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'item_category_id'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_item_cat_month')
 DF_all.drop(['avg_item_cat_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shops_itemcat_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shops_itemcat_month')
 DF_all.drop(['avg_shops_itemcat_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id', 'item_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shops_item_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id', 'item_id'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shops_item_month')
 DF_all.drop(['avg_shops_item_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id', 'item_sub_cat_1'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shops_item_sub_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id', 'item_sub_cat_1'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shops_item_sub_month')
 DF_all.drop(['avg_shops_item_sub_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_city'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shopscity_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_city'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shopscity_month')
 DF_all.drop(['avg_shopscity_month'], axis=1, inplace= True)
 DF_all
print('Use time:', time.time() - ts)
<categorify>
train_data.drop(labels='AgeBand', axis=1, inplace=True )
DF_all['month'] = DF_all['date_block_num'] % 12
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 DF_all['days'] = DF_all['month'].map(days)
 DF_all['years'] = np.select(
 [DF_all.date_block_num.isin(range(0,12)) ,
DF_all.date_block_num.isin(range(12,25)) ,
DF_all.date_block_num.isin(range(25,35)) ],
['13','14','15'])
 DF_all['month'] = DF_all['month'] + 1
DF_all['days'] = DF_all['days'].astype(np.int8)
 DF_all['month'] = DF_all['month'].astype(np.int8)
 DF_all['years'] = DF_all['years'].astype(np.int8)
 DF_all<categorify>
train_data['FareBand'] = pd.qcut(train_data['Fare'], 5 )
DF_all['item_shop_first_sale'] = \
DF_all['date_block_num'] - DF_all.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 DF_all['item_first_sale'] = \
DF_all['date_block_num'] - DF_all.groupby('item_id')['date_block_num'].transform('min')
 DF_all<count_missing_values>
train_data[['FareBand', 'Survived']].groupby('FareBand', as_index=False ).mean()
DF_all.isnull().any()<correct_missing_values>
train_data.loc[ train_data['Fare'] <= 10, 'Fare'] = 0
train_data.loc[(train_data['Fare'] > 10)&(train_data['Fare'] <= 40), 'Fare'] = 1
train_data.loc[train_data['Fare'] > 40 , 'Fare'] = 2
DF_all = fill_na_test(DF_all)
 DF_all.isnull().any()<load_pretrained>
train_data.drop(labels='FareBand', axis=1, inplace=True )
DF_all.to_pickle('dataset.pkl' )<drop_column>
test_data = pd.read_csv(".. /input/titanic/test.csv" )
del DF_all
del temp
del DF_sales
del DF_items
del DF_item_cat
del DF_shops
gc.collect()<load_pretrained>
test_data.drop(columns=['Name', 'Ticket', 'Cabin'], axis=1, inplace=True )
data = pd.read_pickle('dataset.pkl' )<create_dataframe>
test_data['Sex'] = test_data['Sex'].map({'male':0,'female':1} )
data = data[[
'date_block_num', 'shop_id', 'item_id', 'shop_city', 'shop_cat',
'item_category_id', 'item_sub_cat_1', 'item_cnt_month',
'item_cnt_month_lag_1', 'item_cnt_month_lag_2', 'item_cnt_month_lag_3',
'avg_month_lag_1', 'avg_month_lag_2', 'avg_month_lag_3',
'avg_item_month_lag_1', 'avg_item_month_lag_2', 'avg_item_month_lag_3',
'avg_shop_month_lag_1', 'avg_shop_month_lag_2', 'avg_shop_month_lag_3',
'avg_item_cat_month_lag_1', 'avg_item_cat_month_lag_2',
'avg_shops_itemcat_month_lag_1', 'avg_shops_itemcat_month_lag_2',
'avg_shops_item_month_lag_1', 'avg_shops_item_month_lag_2',
'avg_shops_item_sub_month_lag_1', 'avg_shops_item_sub_month_lag_2',
'avg_shopscity_month_lag_1', 'avg_shopscity_month_lag_2', 'month',
'days', 'years', 'item_shop_first_sale', 'item_first_sale'
]]<prepare_x_and_y>
test_data['Embarked'] = test_data['Embarked'].map({'S':0,'C':1,'Q':2} )
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<create_dataframe>
test_data['Fare'].fillna(value=test_data['Fare'].median() ,axis=0, inplace=True )
lgb_train = lgb.Dataset(X_train, Y_train)
 lgb_eval = lgb.Dataset(X_valid, Y_valid, reference=lgb_train )<find_best_params>
test_data.fillna(value=test_data['Age'].mean() , axis=0, inplace=True )
def objective(trial):
param = {
"objective": "regression",
"metric": "rmse",
"verbosity": -1,
"boosting_type": "gbdt",
"lambda_l1": trial.suggest_float("lambda_l1", 1e-8, 10.0, log=True),
"lambda_l2": trial.suggest_float("lambda_l2", 1e-8, 10.0, log=True),
"num_leaves": trial.suggest_int("num_leaves", 2, 256),
"feature_fraction": trial.suggest_float("feature_fraction", 0.4, 1.0),
"bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 1.0),
"bagging_freq": trial.suggest_int("bagging_freq", 1, 7),
"min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
}
model = lgb.train(param,
lgb_train,
valid_sets=[lgb_train,lgb_eval],
early_stopping_rounds=15,
verbose_eval=1)
 y_pred = model.predict(X_valid)
 accuracy = rmsle(Y_valid, y_pred)
 return accuracy<train_model>
test_data.loc[ test_data['Age'] <= 18, 'Age'] = 0
test_data.loc[(test_data['Age'] > 18)&(test_data['Age'] <= 44), 'Age'] = 1
test_data.loc[(test_data['Age'] > 44)&(test_data['Age'] <= 53), 'Age'] = 2
test_data.loc[(test_data['Age'] > 53)&(test_data['Age'] <= 62), 'Age'] = 3
test_data.loc[ test_data['Age'] > 62, 'Age'] = 4
study = optuna.create_study(direction='minimize')
 study.optimize(objective, n_trials=5)
 print('Number of finished trials:', len(study.trials))
print('Best trial:', study.best_trial.params )<train_model>
test_data.loc[ test_data['Fare'] <= 10, 'Fare'] = 0
test_data.loc[(test_data['Fare'] > 10)&(test_data['Fare'] <= 40), 'Fare'] = 1
test_data.loc[test_data['Fare'] > 40 , 'Fare'] = 2
best_params = study.best_trial.params
print(f'Best trial parameters
{best_params}' )<find_best_params>
submission = pd.read_csv('.. /input/titanic/gender_submission.csv' )
x = {"objective": "regression",
"metric" : "rmse",
"verbosity": -1,
"boosting_type": "gbdt"}
best_params.update(x)
 best_params<train_model>
features = ["Pclass", "Sex", "Age","Fare","SibSp","Parch","Embarked"]
targets = ["Survived"]
X_train, X_test = train_data[features], test_data[features]
y_train, y_test = train_data[targets],submission[targets]
evals_result = {}
model = lgb.train(best_params,
lgb_train,
valid_sets=[lgb_train,lgb_eval],
evals_result=evals_result,
early_stopping_rounds=30,
verbose_eval=1,
)<predict_on_test>
import statsmodels.api as sm
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
y_pred = model.predict(X_valid)
 rmsle(Y_valid, y_pred )<predict_on_test>
lr = LogisticRegression()
model = lr.fit(X_train,y_train)
 predictions_lr = model.predict(X_test )
Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": DF_test.index,
"item_cnt_month": Y_test
})
 submission.head(10 )<save_to_csv>
predictions_lr = model.predict(X_test )
submission.to_csv('submission.csv', index=False )<save_to_csv>
print(pd.crosstab(predictions_lr,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr))
submission.to_csv('submission.csv', index=False )<set_options>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_lr} )
warnings.filterwarnings('ignore')
<data_type_conversions>
output = output.to_csv("My_submission_Logistic.csv", index=False )
def downcast_dtypes(df):
float_cols = [c for c in df if df[c].dtype == "float64"]
int_cols = [c for c in df if df[c].dtype == "int64"]
df[float_cols] = df[float_cols].astype(np.float32)
 df[int_cols] = df[int_cols].astype(np.int32)
 return df<load_from_csv>
from sklearn.neighbors import KNeighborsClassifier
sales = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/sales_train.csv')
 shops = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/shops.csv')
 items = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/items.csv')
 items_categories = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/item_categories.csv')
 sample_submission = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/sample_submission.csv')
 test_data = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv')
<drop_column>
from sklearn.neighbors import KNeighborsClassifier
test_block = sales['date_block_num'].max() + 1
test_data['date_block_num'] = test_block
test_data = test_data.drop(columns=['ID'])
 test_data.head()<merge>
from sklearn.neighbors import KNeighborsClassifier
gb = sales.groupby(index_cols, as_index=False)['item_cnt_day'].sum()
gb = gb.rename(columns={'item_cnt_day': 'target'})
 all_data = pd.merge(grid, gb, how='left', on=index_cols ).fillna(0)
 gb = sales.groupby(['shop_id', 'date_block_num'], as_index=False)['item_cnt_day'].sum()
gb = gb.rename(columns={'item_cnt_day': 'target_shop'})
 all_data = pd.merge(all_data, gb, how='left', on=['shop_id', 'date_block_num'] ).fillna(0)
 gb = sales.groupby(['item_id', 'date_block_num'], as_index=False)['item_cnt_day'].sum()
gb = gb.rename(columns={'item_cnt_day': 'target_item'})
 all_data = pd.merge(all_data, gb, how='left', on=['item_id', 'date_block_num'] ).fillna(0)
 all_data = downcast_dtypes(all_data)
 del grid, gb
gc.collect() ;<merge>
def KNN_1(neighbors):
neighbors = range(1,neighbors+1,1)
 print("Para p = 1")
 for i in neighbors:
KNN = KNeighborsClassifier(n_neighbors=i,p=1)
 model = KNN.fit(X_train,np.ravel(y_train))
results_KNN = model.predict(test_data[features])
 print("Para uma quantidade de vizinhos de:", i,"a precisão do modelo foi de ",round(( model.score(X_train,y_train)) *100,2),"%" )
cols_to_rename = list(all_data.columns.difference(index_cols))
shift_range = [1, 2, 3, 4, 5, 12]
for month_shift in tqdm_notebook(shift_range):
train_shift = all_data[index_cols + cols_to_rename].copy()
train_shift['date_block_num'] = train_shift['date_block_num'] + month_shift
foo = lambda x: '{}_lag_{}'.format(x, month_shift)if x in cols_to_rename else x
train_shift = train_shift.rename(columns=foo)
 all_data = pd.merge(all_data, train_shift, on=index_cols, how='left' ).fillna(0)
 del train_shift<merge>
def KNN_2(neighbors):
neighbors = range(1,neighbors+1,1)
 print("Para p = 2")
 for i in neighbors:
KNN = KNeighborsClassifier(n_neighbors=i,p=2)
 model = KNN.fit(X_train,np.ravel(y_train))
results_KNN = model.predict(test_data[features])
 print("Para uma quantidade de vizinhos de:", i,"a precisão do modelo foi de ",round(( model.score(X_train,y_train)) *100,2),"%" )
all_data = all_data[all_data['date_block_num'] >= 12]
fit_cols = [col for col in all_data.columns if col[-1] in [str(item)for item in shift_range]]
to_drop_cols = ['target_item', 'target_shop', 'target', 'date_block_num']
to_drop_cols = list(set(list(all_data.columns)) -(set(fit_cols)|set(index_cols)))+ ['date_block_num']
item_category_mapping = items[['item_id', 'item_category_id']].drop_duplicates()
all_data = pd.merge(all_data, item_category_mapping, how='left', on='item_id')
 all_data = downcast_dtypes(all_data)
 gc.collect()<filter>
def choosen_KNN(neighbors,p):
KNN = KNeighborsClassifier(n_neighbors=neighbors,p=p)
 model = KNN.fit(X_train,np.ravel(y_train))
results_KNN = model.predict(test_data[features])
 print("Precisão ",round(( model.score(X_train,y_train)) *100,2),"%
")
 output_KNN = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': results_KNN})
 output_KNN.to_csv('my_submission_KNN.csv', index=False)
 print(pd.crosstab(results_KNN,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
dates = all_data['date_block_num']
dates_train = dates[dates < test_block]
dates_test = dates[dates == test_block]<prepare_x_and_y>
from sklearn.tree import DecisionTreeClassifier
X_train = all_data.loc[dates < test_block].drop(to_drop_cols, axis=1)
 X_test = all_data.loc[dates == test_block].drop(to_drop_cols, axis=1)
 y_train = all_data.loc[dates < test_block, 'target'].values
y_test = all_data.loc[dates == test_block, 'target'].values<define_variables>
def DTC(depth):
depth = range(1,depth+1,1)
 for i in depth:
DTC = DecisionTreeClassifier(max_depth=i)
 model = DTC.fit(X_train, y_train)
 predictions_DTC = model.predict(X_test)
 print("Para uma profundidade de:", i,"a precisão do modelo foi de ",round(( model.score(X_train,y_train)) *100,2),"%" )
target_range = [0, 20]
target_range<init_hyperparams>
def choosen_DTC(depth):
DTC = DecisionTreeClassifier(max_depth=depth)
 model = DTC.fit(X_train, y_train)
 predictions_DTC = model.predict(X_test)
 print(pd.crosstab(predictions_DTC, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(submission['Survived'],predictions_DTC))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC})
 output_DTC.to_csv('my_submission_DTC.csv', index=False )
lgb_params = {
'feature_fraction': 0.75,
'metric': 'rmse',
'nthread':1,
'min_data_in_leaf': 2**7,
'bagging_fraction': 0.7,
'learning_rate': 0.04,
'objective': 'mse',
'bagging_seed': 2**7,
'num_leaves': 2**7,
'bagging_freq':1,
'verbose':0
}
model = lgb.train(lgb_params, lgb.Dataset(X_train, label=y_train), 500)
 pred_lgb = model.predict(X_test ).clip(*target_range )<save_to_csv>
from sklearn.model_selection import cross_val_predict,cross_val_score,cross_validate
from sklearn import metrics
submission = pd.DataFrame({'ID': sample_submission.ID, 'item_cnt_month': pred_lgb})
 submission.to_csv('submission.csv', index=False )<set_options>
def DTC_CROSS(depth, folds):
model_DTC = DecisionTreeClassifier(max_depth=depth)
 model_DTC.fit(X_train, y_train)
 cross_validate(model_DTC,X_train,y_train,cv=folds)
 predictions_DTC_cross = model_DTC.predict(X_test)
 accuracy = metrics.accuracy_score(y_test,predictions_DTC_cross)
 print("
")
 print(pd.crosstab(predictions_DTC_cross, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(submission['Survived'],predictions_DTC_cross))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC_cross})
 output_DTC.to_csv('my_submission_DTC_cross.csv', index=False)
 return print("accuracy = ",accuracy*100,"%" )
warnings.filterwarnings("ignore")
<load_from_csv>
from sklearn import svm
df_train = pd.read_csv('.. /input/train.csv' ).astype('float32')
 df_test = pd.read_csv('.. /input/test.csv' )<drop_column>
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test )<feature_engineering>
X_train = train_data[features]
X_test = test_data[features]
X_train.head()
df_train["distance"] = df_train["rideDistance"]+df_train["walkDistance"]+df_train["swimDistance"]
df_train["skill"] = df_train["headshotKills"]+df_train["roadKills"]
df_test["distance"] = df_test["rideDistance"]+df_test["walkDistance"]+df_test["swimDistance"]
df_test["skill"] = df_test["headshotKills"]+df_test["roadKills"]<groupby>
y_train = train_data[targets]
y_test = submission['Survived']
y_train.head()
df_train_size = df_train.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_test_size = df_test.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_train_mean = df_train.groupby(['matchId','groupId'] ).mean().reset_index()
df_test_mean = df_test.groupby(['matchId','groupId'] ).mean().reset_index()
df_train_max = df_train.groupby(['matchId','groupId'] ).max().reset_index()
df_test_max = df_test.groupby(['matchId','groupId'] ).max().reset_index()
df_train_min = df_train.groupby(['matchId','groupId'] ).min().reset_index()
df_test_min = df_test.groupby(['matchId','groupId'] ).min().reset_index() 
<merge>
def choosen_SVM(C, gamma):
model_SVM = svm.SVC(C=C,gamma=gamma,random_state=0)
 model_SVM.fit(X_train,y_train)
 predictions_SVM = model_SVM.predict(X_test)
 print("Para C = ",C,"e gamma = ", gamma," Precisão = ",round(( model_SVM.score(X_train,y_train)) *100,2),"% ")
 print(pd.crosstab(predictions_SVM,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(submission['Survived'],predictions_SVM))
output_SVM = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_SVM})
 output_SVM.to_csv('my_submission_SVM.csv', index=False )
df_train_match_mean = df_train.groupby(['matchId'] ).mean().reset_index()
df_test_match_mean = df_test.groupby(['matchId'] ).mean().reset_index()
df_train = pd.merge(df_train, df_train_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 del df_train_mean
del df_test_mean
df_train = pd.merge(df_train, df_train_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 del df_train_max
del df_test_max
df_train = pd.merge(df_train, df_train_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 del df_train_min
del df_test_min
df_train = pd.merge(df_train, df_train_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 df_test = pd.merge(df_test, df_test_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 del df_train_match_mean
del df_test_match_mean
df_train = pd.merge(df_train, df_train_size, how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_size, how='left', on=['matchId', 'groupId'])
 del df_train_size
del df_test_size
target = 'winPlacePerc'
train_columns = list(df_test.columns )<drop_column>
train_data['Survived'].value_counts()
train_columns.remove("Id")
 train_columns.remove("matchId")
 train_columns.remove("groupId")
 train_columns.remove("Id_mean")
 train_columns.remove("Id_max")
 train_columns.remove("Id_min")
 train_columns.remove("Id_match_mean" )<prepare_x_and_y>
from imblearn.under_sampling import NearMiss
X = df_train[train_columns]
Y = df_test[train_columns]
T = df_train[target]
del df_train 
<normalization>
X = train_data[features]
X_test = test_data[features]
y = train_data[targets]
y_test = submission[targets]
x_train, x_test, t_train, t_test = train_test_split(X, T, test_size = 0.2, random_state = 1234)
 scaler = preprocessing.QuantileTransformer().fit(x_train)
 x_train = scaler.transform(x_train)
 x_test = scaler.transform(x_test)
 Y = scaler.transform(Y)
 print("x_train", x_train.shape, x_train.min() , x_train.max())
 print("x_test", x_test.shape, x_test.min() , x_test.max())
 print("Y", Y.shape, Y.min() , Y.max() )<choose_model_class>
nrm = NearMiss()
model = Sequential()
model.add(Dense(512, kernel_initializer='he_normal', input_dim=x_train.shape[1], activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Dense(256, kernel_initializer='he_normal', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Dense(128, kernel_initializer='he_normal', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) 
<choose_model_class>
X, y = nrm.fit_sample(X,y )
optimizer = optimizers.Adam(lr=0.01, epsilon=1e-8, decay=1e-4, amsgrad=False)
 model.compile(optimizer=optimizer, loss='mse', metrics=['mae'] )<train_model>
lr_us = LogisticRegression()
model = lr_us.fit(X,y)
 predictions_lr_us = model.predict(X_test)
 print(pd.crosstab(predictions_lr_us,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr_us))
history = model.fit(x_train, t_train,
validation_data=(x_test, t_test),
epochs=30,
batch_size=32768,
callbacks=[lr_sched,early_stopping],
verbose=1 )<save_to_csv>
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_lr_us})
 output.to_csv("My_submission_Logistic_US.csv", index=False )
pred = model.predict(Y)
 pred = pred.ravel()
df_test['winPlacePercPred'] = np.clip(pred, a_min=0, a_max=1)
 aux = df_test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
df_test = df_test.merge(aux, how='left', on=['matchId','groupId'])
 submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<import_modules>
from imblearn.over_sampling import SMOTE
from lightgbm import LGBMRegressor
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.metrics import mean_absolute_error
import gc
from sklearn.model_selection import GridSearchCV<load_from_csv>
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
sampling = np.linspace(0.65,1,8)
 sampling
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<concatenate>
def Over_samp(train, test, submisson):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
sampling = np.linspace(0.65,1,16)
 X = train[features]
X_test = test[features]
y = train[targets]
y_test = submission[targets]
for i in sampling:
print("For sampling strategy = ",i*100,"%
")
 smt = SMOTE(sampling_strategy = i)
 X, y = smt.fit_sample(X,y)
 lr_os = LogisticRegression()
model = lr_os.fit(X,y)
 predictions_lr_os = model.predict(X_test)
 print(pd.crosstab(predictions_lr_os,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr_os))
print("
" )
concat = pd.concat([train, test])
 del train
del test
gc.collect()<drop_column>
Over_samp(train_data,test_data, submission )
concat = reduce_mem_usage(concat )<categorify>
X = train_data[features]
X_test = test_data[features]
y = train_data[targets]
y_test = submission[targets]
def count_transform(df, cols):
for c in cols:
df[c + "_count"] = df.groupby(c)[c].transform('count')
 return df 
<categorify>
smt = SMOTE(sampling_strategy = 0.65)
 X, y = smt.fit_sample(X,y)
 lr_os = LogisticRegression()
model = lr_os.fit(X,y)
 predictions_lr_os = model.predict(X_test)
 print(pd.crosstab(predictions_lr_os,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr_os))
output = pd.DataFrame({'PassengerId': test_data['PassengerId'], 'Survived': predictions_lr_os})
 output.to_csv("My_submission_Logistic_OS.csv", index=False )
concat = count_transform(concat, ['groupId', 'matchId'] )<define_variables>
def DTC_US(depth):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
X,y = nrm.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 DTC = DecisionTreeClassifier(max_depth=depth)
 model = DTC.fit(X, y)
 predictions_DTC_US = model.predict(X_test)
 print(pd.crosstab(predictions_DTC_US, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_DTC_US))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC_US})
 output_DTC.to_csv('my_submission_DTC_US.csv', index=False )
per_dist_stats = ['assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'kills',
'teamKills', 'vehicleDestroys', 'weaponsAcquired']<feature_engineering>
def DTC_OS(depth):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
smt = SMOTE(sampling_strategy = 0.70)
 X,y = smt.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 DTC = DecisionTreeClassifier(max_depth=depth, random_state=42)
 model = DTC.fit(X, y)
 predictions_DTC_OS = model.predict(X_test)
 print(pd.crosstab(predictions_DTC_OS, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_DTC_OS})
 output_DTC.to_csv('my_submission_DTC_OS.csv', index=False )
concat['LogWalk'] = np.log1p(concat['walkDistance'] )<feature_engineering>
def SVM_US(C, gamma):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
X,y = nrm.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 sv = svm.SVC(C=C, gamma=gamma)
 model = sv.fit(X, y)
 predictions_SVM_US = model.predict(X_test)
 print(pd.crosstab(predictions_SVM_US, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_SVM_US))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_SVM_US})
 output_DTC.to_csv('my_submission_SVM_US.csv', index=False )
for stat in per_dist_stats:
concat[stat + '_perLogWalk'] = concat[stat] / concat['LogWalk']<feature_engineering>
SVM_US(5,0.01 )
concat['grpSizeMult'] = concat['groupId_count'] /(concat['matchId_count'] / concat['numGroups'] )<define_variables>
def SVM_OS(C,gamma):
features = ['Sex', 'Pclass','Fare','Age','Embarked','SibSp','Parch']
targets = ['Survived']
X_train = train_data[features]
X_test = test_data[features]
y_train = train_data[targets]
y_test = submission[targets]
smt = SMOTE(sampling_strategy = 0.75)
 X,y = smt.fit_sample(X_train, y_train)
 sns.distplot(y, kde=False)
 sv = svm.SVC(C=C, gamma=gamma)
 model = sv.fit(X, y)
 predictions_SVM_OS = model.predict(X_test)
 print(pd.crosstab(predictions_SVM_OS, submission['Survived'], margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_SVM_OS))
output_DTC = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_SVM_OS})
 output_DTC.to_csv('my_submission_SVM_OS.csv', index=False )
match_stats = ['DBNOs',
'assists',
'boosts',
'damageDealt',
'headshotKills',
'heals',
'killPlace',
'killPoints',
'killStreaks',
'kills',
'longestKill',
'revives',
'rideDistance',
'roadKills',
'swimDistance',
'vehicleDestroys',
'walkDistance',
'weaponsAcquired',
'winPoints',
'LogWalk',
'assists_perLogWalk',
'boosts_perLogWalk',
'damageDealt_perLogWalk',
'DBNOs_perLogWalk',
'headshotKills_perLogWalk',
'heals_perLogWalk',
'kills_perLogWalk',
'teamKills_perLogWalk',
'vehicleDestroys_perLogWalk',
'weaponsAcquired_perLogWalk',]<categorify>
SVM_OS(100,0.01 )