Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
%matplotlib inline
sns.set_style('dark')
 sns.set_palette('Set2')
sales.loc[sales.shop_id == 0, 'shop_id'] = 57
testd.loc[testd.shop_id == 0, 'shop_id'] = 57
sales.loc[sales.shop_id == 1, 'shop_id'] = 58
testd.loc[testd.shop_id == 1, 'shop_id'] = 58
sales.loc[sales.shop_id == 10, 'shop_id'] = 11
testd.loc[testd.shop_id == 10, 'shop_id'] = 11
sales.loc[sales.shop_id == 39, 'shop_id'] = 40
testd.loc[testd.shop_id == 39, 'shop_id'] = 40<feature_engineering>
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 data = pd.concat([train_data, test_data] ).reset_index().drop(['index'], axis=1 )
shops.loc[shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['shop_category'] = shops['shop_name'].str.split(' ' ).map(lambda x:x[1] ).astype(str)
 categories = ['Орджоникидзе,', 'ТЦ', 'ТРК', 'ТРЦ','ул.', 'Магазин', 'ТК', 'склад']
shops.shop_category = shops.shop_category.apply(lambda x: x if(x in categories)else 'etc')
 shops.shop_category.unique()<groupby>
data['Surname'] = data['Name'].apply(lambda x: x.split(',')[0] )
shops.groupby(['shop_category'] ).sum()<categorify>
data['Ticket_id'] = 'new_col'
def ticket_id(row):
row['Ticket_id'] = str(row.Pclass)+ '-' + str(row.Ticket)[:-1] + '-' + str(row.Fare)+ '-' + str(row.Embarked)
 return row
data = data.apply(ticket_id, axis='columns' )
category = ['ТЦ', 'ТРК', 'ТРЦ', 'ТК']
shops.shop_category = shops.shop_category.apply(lambda x: x if(x in category)else 'etc')
 print('Category Distribution', shops.groupby(['shop_category'] ).sum())
 shops['shop_category_code'] = LabelEncoder().fit_transform(shops['shop_category'] )<categorify>
data['Group_id'] = 'new_col2'
def group_id(row):
row['Group_id'] = str(row.Surname)+ '-' + str(row.Ticket_id)
 return row
data = data.apply(group_id, axis='columns' )
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'
shops['city_code'] = LabelEncoder().fit_transform(shops['city'])
 shops = shops[['shop_id','city_code', 'shop_category_code']]
shops.head()<count_unique_values>
data['Title'] = 'man'
data.loc[data.Sex == 'female', 'Title'] = 'woman'
data.loc[data['Name'].str.contains('Master'), 'Title'] = 'boy'
print(len(catgs.item_category_name.unique()))
catgs.item_category_name.unique()<feature_engineering>
data.loc[data.Title == 'man', 'Group_id'] = 'noGroup'
data['WC_count'] = data.loc[data.Title != 'man'].groupby('Group_id')['Group_id'].transform('count')
 data.loc[data.WC_count <=1, 'Group_id'] = 'noGroup'
catgs['type'] = catgs.item_category_name.apply(lambda x: x.split(' ')[0] ).astype(str)
 catgs.loc[(catgs.type == 'Игровые')|(catgs.type == 'Аксессуары'), 'category'] = 'Игры'
catgs.loc[catgs.type == 'PC', 'category'] = 'Музыка'
category = ['Игры', 'Карты', 'Кино', 'Книги','Музыка', 'Подарки', 'Программы', 'Служебные', 'Чистые', 'Аксессуары']
catgs['type'] = catgs.type.apply(lambda x: x if(x in category)else 'etc')
 print(catgs.groupby(['type'] ).sum())
 catgs['type_code'] = LabelEncoder().fit_transform(catgs['type'])
 catgs['split'] = catgs.item_category_name.apply(lambda x: x.split('-'))
catgs['subtype'] = catgs['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 catgs['subtype_code'] = LabelEncoder().fit_transform(catgs['subtype'])
 catgs = catgs[['item_category_id','type_code', 'subtype_code']]
catgs.head()<feature_engineering>
cols = ['PassengerId', 'Survived', 'Name', 'Title', 'Ticket_id','Group_id']
data.loc[(data.Ticket_id == '1-1696-134.5-C')&(data.Title != 'man'), cols]
sales['date'] = pd.to_datetime(sales['date'], format='%d.%m.%Y')
 sales['month'] = sales['date'].dt.month
sales['year'] = sales['date'].dt.year
sales = sales.drop(columns=['date'])
 to_append = testd[['shop_id', 'item_id']].copy()
to_append['date_block_num'] = sales['date_block_num'].max() + 1
to_append['year'] = 2015
to_append['month'] = 11
to_append['item_cnt_day'] = 0
to_append['item_price'] = 0
sales = pd.concat([sales, to_append], ignore_index=True, sort=False)
 sales.head()<feature_engineering>
indices = []
count = 0
for i in range(0,1309):
if(data.loc[i,'Title'] != 'man')&(data.loc[i,'Group_id'] == 'noGroup'):
data.loc[i,'Group_id'] = data.loc[(data['Ticket_id'] == data.loc[i, 'Ticket_id'])&(data.Title != 'man'), 'Group_id'].iloc[0]
if(data.loc[i, 'Group_id'] != 'noGroup'):
indices.append(i)
 count += 1
print('{:d} passengers were added to an existing group'.format(count))
period = sales[['date_block_num', 'year', 'month']].drop_duplicates().reset_index(drop=True)
 period['days'] = period.apply(lambda r: monthrange(r.year, r.month)[1], axis=1)
 sales = sales.drop(columns=['month', 'year'])
 period.head()<merge>
cols = ['PassengerId', 'Survived', 'Name', 'Title', 'Group_id']
data.loc[indices, cols]
data = pd.merge(grid, shops, on='shop_id')
 data = pd.merge(data, items, on='item_id')
 data = pd.merge(data, catgs, on='item_category_id')
 data = pd.merge(data, period, on='date_block_num')
 data = data[['date_block_num', 'year', 'month', 'days', 'city_code', 'shop_category_code', 'shop_id', 'item_category_id', 'type_code', 'subtype_code', 'item_id']]
for c in ['date_block_num', 'month', 'days', 'city_code', 'shop_category_code', 'shop_id', 'item_category_id', 'type_code', 'subtype_code']:
data[c] = data[c].astype(np.int8)
 data['item_id'] = data['item_id'].astype(np.int16)
 data['year'] = data['year'].astype(np.int16)
 del grid, shops, items, catgs, to_append
data.head()<data_type_conversions>
number_of_groups = data.loc[data.Group_id != 'noGroup', 'Group_id'].nunique()
print('Number of groups found: {:d}'.format(number_of_groups))
number_of_WCG_passengers = data.loc[data.Group_id != 'noGroup', 'Group_id'].count()
print('
Number of passengers in a group: {:d}'.format(number_of_WCG_passengers))
composition = data.loc[data.Group_id != 'noGroup','Title'].value_counts()
print('
Composition of the groups:')
 print(composition.to_string() )
aux = sales\ 
.groupby(['date_block_num', 'shop_id', 'item_id'], as_index=False)\ 
.agg({'item_cnt_day' : 'sum', 'item_price' : 'mean'})\ 
.rename(columns= {'item_cnt_day' : 'item_cnt_month', 'item_price' : 'item_price_month'})
 aux['item_cnt_month'] = aux['item_cnt_month'].astype(np.float16)
 aux['item_price_month'] = aux['item_price_month'].astype(np.float16)
 month_summary = pd.merge(data, aux, how='left', on=['date_block_num', 'shop_id', 'item_id'])\ 
.fillna(0.0 ).sort_values(by=['shop_id', 'item_id', 'date_block_num'])
 del data, aux
month_summary.head()<feature_engineering>
data['WCSurvived'] = data.loc[(data.Title != 'man')&(data.Group_id != 'noGroup')].groupby('Group_id' ).Survived.transform('mean' )
month_summary['item_cnt_month'] = month_summary['item_cnt_month'].clip(0,20 )<categorify>
cols = ['PassengerId', 'Survived', 'WCSurvived', 'Name', 'Title', 'Group_id']
data.loc[data.Group_id == 'Sage-3-CA.234-69.55-S', cols]
def agg_by(month_summary, group_cols, new_col, target_col = 'item_cnt_month', agg_func = 'mean'):
aux = month_summary\ 
.groupby(group_cols, as_index=False)\ 
.agg({target_col : agg_func})\ 
.rename(columns= {target_col : new_col})
 aux[new_col] = aux[new_col].astype(np.float16)
 return pd.merge(month_summary, aux, how='left', on=group_cols)
 def lag_feature(df, col, lags=[1,2,3,6,12]):
tmp = df[['date_block_num','shop_id','item_id', col]]
for i in lags:
shifted = tmp.copy()
cols = ['date_block_num','shop_id','item_id', '{}_lag_{}'.format(col, i)]
shifted.columns = cols
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left' ).fillna(value={(cols[-1]): 0.0})
 return df
def agg_by_and_lag(month_summary, group_cols, new_col, lags=[1,2,3,6,12], target_col = 'item_cnt_month', agg_func = 'mean'):
tmp = agg_by(month_summary, group_cols, new_col, target_col, agg_func)
 tmp = lag_feature(tmp, new_col, lags)
 return tmp.drop(columns=[new_col] )<groupby>
data.loc[(data.WCSurvived==0.75)|(data.WCSurvived==0.5), cols].sort_values(by='Group_id' )
month_summary = agg_by_and_lag(month_summary, ['date_block_num'], 'date_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_id'], 'date_item_avg_item_cnt', [1,2,3,6,12])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code'], 'date_city_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id'], 'date_shop_avg_item_cnt', [1,2,3,6,12])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_category_id'], 'date_cat_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'type_code'], 'date_type_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'subtype_code'], 'date_subtype_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code'], 'date_shop_category_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'item_category_id'], 'date_shop_cat_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'type_code'], 'date_shop_type_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'subtype_code'], 'date_shop_subtype_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code', 'subtype_code'], 'date_shop_category_subtype_avg_item_cnt', [1])
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code', 'item_id'], 'date_item_city_avg_item_cnt', [1] )<groupby>
data.loc[data.Group_id.isin(test_groups), 'WCSurvived'] = 0
data.loc[(data.Group_id.isin(test_groups)) &(data.Pclass != 3), 'WCSurvived'] = 1
month_summary = agg_by_and_lag(month_summary, ['date_block_num'], 'date_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_id'], 'date_item_avg_item_price', [1,2,3,6,12], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code'], 'date_city_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id'], 'date_shop_avg_item_price', [1,2,3,6,12], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'item_category_id'], 'date_cat_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'type_code'], 'date_type_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'subtype_code'], 'date_subtype_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code'], 'date_shop_category_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'item_category_id'], 'date_shop_cat_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'type_code'], 'date_shop_type_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_id', 'subtype_code'], 'date_shop_subtype_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'shop_category_code', 'subtype_code'], 'date_shop_category_subtype_avg_item_price', [1], 'item_price_month')
 month_summary = agg_by_and_lag(month_summary, ['date_block_num', 'city_code', 'item_id'], 'date_item_city_avg_item_price', [1], 'item_price_month' )<categorify>
print('WCSurvived test values:')
 print(data[891:1309].WCSurvived.value_counts().to_string() )
month_summary['item_shop_first_sale'] = month_summary['date_block_num'] - month_summary.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 month_summary['item_first_sale'] = month_summary['date_block_num'] - month_summary.groupby('item_id')['date_block_num'].transform('min' )<load_pretrained>
data.loc[891:1308, 'Predict'] = 0
data.loc[891:1308, 'Predict'][(data.Sex == 'female')] = 1
data.loc[891:1308,'Predict'][(data.Sex == 'female')&(data['WCSurvived'] == 0)] = 0
data.loc[891:1308, 'Predict'][(data.Title == 'boy')&(data['WCSurvived'] == 1)] = 1
month_summary = pd.read_pickle('month_summary.pkl' )<prepare_x_and_y>
print('The following 8 males are predicted to live:')
 cols = ['PassengerId', 'Name', 'Title', 'Group_id']
data[891:1309][cols].loc[(data.Title == 'boy')&(data.Predict == 1)]
def generate_subsample(month_summary, target='item_cnt_month'):
X_test = month_summary[month_summary['date_block_num'] == 34]
X_test = X_test.drop(columns=[target])
 X_val = month_summary[month_summary['date_block_num'] == 33]
y_val = X_val[target]
X_val = X_val.drop(columns=[target])
 X_train = month_summary[(month_summary['date_block_num'] >= 12)&(month_summary['date_block_num'] < 33)]
y_train = X_train[target]
X_train = X_train.drop(columns=[target])
 return X_train, y_train, X_val, y_val, X_test<prepare_x_and_y>
print('The following 15 females are predicted to die:')
 data[891:1309][cols].loc[(data.Title == 'woman')&(data.Predict == 0)]
X_train, y_train, X_val, y_val, X_test = generate_subsample(month_summary.drop(columns=['item_price_month']), 'item_cnt_month')
 del month_summary<train_model>
print('The remaining 258 males are predicted to die')
 print('and the remaining 137 females are predicted to live' )
def train_gbmodel(X_train, y_train, X_val, y_val):
RAND_SEED = 42
lgb_params = {'num_leaves': 2**8, 'max_depth': 19, 'max_bin': 107,
'bagging_freq': 1, 'bagging_fraction': 0.7135681370918421,
'feature_fraction': 0.49446461478601994, 'min_data_in_leaf': 2**8,
'learning_rate': 0.015980721586917768, 'num_threads': 2,
'min_sum_hessian_in_leaf': 6,
'random_state' : RAND_SEED,
'bagging_seed' : RAND_SEED,
'boost_from_average' : 'true',
'boost' : 'gbdt',
'metric' : 'rmse',
'verbose' : 1}
lgb_train = lgb.Dataset(X_train, label=y_train)
 lgb_val = lgb.Dataset(X_val, label=y_val)
 return lgb.train(lgb_params, lgb_train,
num_boost_round=300,
valid_sets=[lgb_train, lgb_val],
early_stopping_rounds=20 )<save_to_csv>
output = pd.DataFrame({'PassengerId': data[891:1309].PassengerId, 'Survived': data[891:1309].Predict.astype('int')})
 output.to_csv('WCG_gender.csv', index=False)
 print('WCG_gender submission was successfully saved!')
 print('Submission is loading...you scored 81,6%!' )
y_pred = gbm_model.predict(X_test ).clip(0, 20)
 result = pd.merge(testd, X_test.assign(item_cnt_month=y_pred), how='left', on=['shop_id', 'item_id'])[['ID', 'item_cnt_month']]
result.to_csv('submission.csv', index=False )<import_modules>
def fix_fare(row):
if row.Fare == 0:
row.Fare = np.NaN
return row
print('The following {:d} passengers have a zero Fare:'.format(data[data.Fare==0].shape[0]))
cols = ['PassengerId', 'Survived', 'Pclass','Fare', 'Name']
data.loc[data.Fare==0, cols]
RANDOM_SEED = 42<load_from_csv>
data['Ticket_freq'] = data.groupby('Ticket')['Ticket'].transform('count')
 data['Pfare'] = data['Fare'] / data['Ticket_freq']
PATH = '.. /input/competitive-data-science-predict-future-sales'
items = pd.read_csv(PATH + '/items.csv')
 shops = pd.read_csv(PATH + '/shops.csv')
 cats = pd.read_csv(PATH + '/item_categories.csv')
 train = pd.read_csv(PATH + '/sales_train.csv')
 test = pd.read_csv(PATH + '/test.csv' ).set_index('ID' )<create_dataframe>
train_male = data[0:891].loc[(data.Sex=='male')&(data.WCSurvived.isnull())]
test_male = data[891:1309].loc[(data.Sex=='male')&(data.WCSurvived.isnull())]
summary_stats_table(train )<feature_engineering>
cols = ['PassengerId', 'Name', 'Pfare', 'Pclass', 'Embarked']
y_m = train_male['Survived']
features = ['Pfare', 'Pclass', 'Embarked']
X_m = train_male[features]
numerical_cols = ['Pfare']
categorical_cols = ['Pclass', 'Embarked']
numerical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer()), 
('scaler', StandardScaler())
 ])
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ])
 precision_m = []
recall_m = []
for k in range(1,18):
pipeline1 = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('model', KNeighborsClassifier(n_neighbors=k))
])
 precision_m.append(cross_val_score(pipeline1, X_m, y_m, cv=15, n_jobs=-1, scoring='precision' ).mean())
 recall_m.append(cross_val_score(pipeline1, X_m, y_m, cv=15, n_jobs=-1, scoring='recall' ).mean())
 k_range = range(1,18)
 plt.figure(figsize=(7,5))
plt.plot(k_range, precision_m, label='15-fold precision')
 plt.plot(k_range, recall_m, label='15-fold recall')
 plt.axhline(y=0.5, color='r')
 plt.xlabel('Value of k for KNN')
 plt.title('Precision and recall by number of neighbors', fontsize=14)
 plt.legend()
plt.show()
train = train[train.item_price<100000]
train = train[train.item_cnt_day<1000]
train = train[train.item_price > 0].reset_index(drop=True)
 train.loc[train.item_cnt_day < 0, 'item_cnt_day'] = 0
train.loc[train.shop_id == 0, 'shop_id'] = 57
test.loc[test.shop_id == 0, 'shop_id'] = 57
train.loc[train.shop_id == 1, 'shop_id'] = 58
test.loc[test.shop_id == 1, 'shop_id'] = 58
train.loc[train.shop_id == 11, 'shop_id'] = 10
test.loc[test.shop_id == 11, 'shop_id'] = 10
train.loc[train.shop_id == 40, 'shop_id'] = 39
test.loc[test.shop_id == 40, 'shop_id'] = 39 
<feature_engineering>
m1 = KNeighborsClassifier(n_neighbors=1)
 m2 = KNeighborsClassifier(n_neighbors=3)
 m3 = KNeighborsClassifier(n_neighbors=7)
 male_pipeline = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('voting',VotingClassifier([ 
('m1', m1),('m2', m2),('m3', m3)]))
])
 print('15-fold precision of the ensemble: {:.3f}'.format(
 cross_val_score(male_pipeline, X_m, y_m, cv=15, n_jobs=-1, scoring='precision' ).mean()))
print('15-fold recall of the ensemble: {:.3f}'.format(
 cross_val_score(male_pipeline, X_m, y_m, cv=15, n_jobs=-1, scoring='recall' ).mean()))
print('15-fold accuracy of the ensemble: {:.3f}'.format(
 cross_val_score(male_pipeline, X_m, y_m, cv=15, n_jobs=-1 ).mean()))
male_pipeline.fit(X_m, y_m)
 learn_train_m = male_pipeline.predict(X_m)
 X_test_m = test_male[features]
predictions_m = male_pipeline.predict(X_test_m)
 print('
The following 9 adult males are predicted to live:')
 test_male.loc[(predictions_m==1), cols]
shops.loc[shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shops['city'] = shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shops['category'] = shops['shop_name'].str.split(' ' ).map(lambda x:x[1] ).astype(str)
 shops.loc[shops.city == '!Якутск', 'city'] = 'Якутск'
category = ['ТЦ', 'ТРК', 'ТРЦ', 'ТК']
shops.category = shops.category.apply(lambda x: x if(x in category)else 'etc')
 shops.groupby(['category'] ).sum()
shops['shop_city'] = shops.city
shops['shop_category'] = shops.category
shops['shop_city'] = LabelEncoder().fit_transform(shops['shop_city'])
 shops['shop_category'] = LabelEncoder().fit_transform(shops['shop_category'])
 shops = shops[['shop_id','shop_city', 'shop_category']]
shops.head() 
<categorify>
data.loc[891:1308, 'Predict'][(data.Sex=='male')&(data.WCSurvived.isnull())] = predictions_m
output = pd.DataFrame({'PassengerId': data[891:1309].PassengerId, 'Survived': data[891:1309].Predict.astype('int')})
 output.to_csv('WCG_male.csv', index=False)
 print('WCG_male submission was successfully saved!')
 print('Submission is loading...you scored 82,3%!' )
cats['type_code'] = cats.item_category_name.apply(lambda x: x.split(' ')[0] ).astype(str)
 cats.loc[(cats.type_code == 'Игровые')|(cats.type_code == 'Аксессуары'), 'type_code'] = 'Игры'
cats.loc[cats.type_code == 'PC', 'type_code'] = 'Музыка'
category = ['Игры', 'Карты', 'Кино', 'Книги','Музыка', 'Подарки', 'Программы', 'Служебные', 'Чистые']
cats['type_code'] = cats.type_code.apply(lambda x: x if(x in category)else 'etc')
 cats['type_code'] = LabelEncoder().fit_transform(cats['type_code'])
 cats['split'] = cats.item_category_name.apply(lambda x: x.split('-'))
cats['subtype'] = cats['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats['subtype_code'] = LabelEncoder().fit_transform(cats['subtype'])
 cats = cats[['item_category_id','type_code', 'subtype_code']] 
<feature_engineering>
train_female = data[0:891].loc[(data.Sex=='female')&(data.WCSurvived.isnull())]
test_female = data[891:1309].loc[(data.Sex=='female')&(data.WCSurvived.isnull())]
items['name_1'], items['name_2'] = items['item_name'].str.split('[', 1 ).str
items['name_1'], items['name_3'] = items['item_name'].str.split('(', 1 ).str
items['name_2'] = items['name_2'].str.replace('[^A-Za-z0-9А-Яа-я]+', ' ' ).str.lower()
items['name_3'] = items['name_3'].str.replace('[^A-Za-z0-9А-Яа-я]+', ' ' ).str.lower()
items = items.fillna('0')
 result_1 = Counter(' '.join(items['name_2'].values.tolist() ).split(' ')).items()
result_1 = sorted(result_1, key=itemgetter(1))
result_1 = pd.DataFrame(result_1, columns=['feature', 'count'])
 result_1 = result_1[(result_1['feature'].str.len() > 1)&(result_1['count'] > 200)]
result_2 = Counter(' '.join(items['name_3'].values.tolist() ).split(" ")).items()
result_2 = sorted(result_2, key=itemgetter(1))
result_2 = pd.DataFrame(result_2, columns=['feature', 'count'])
 result_2 = result_2[(result_2['feature'].str.len() > 1)&(result_2['count'] > 200)]
result = pd.concat([result_1, result_2])
 result = result.drop_duplicates(subset=['feature'] ).reset_index(drop=True)
 print('Most common aditional features:', result)
 items['type'] = items.name_2.apply(lambda x: x[0:8] if x.split(' ')[0] == 'xbox' else x.split(' ')[0])
 items.loc[(items.type == 'x360')|(items.type == 'xbox360'), 'type'] = 'xbox 360'
items.loc[items.type == '', 'type'] = 'mac'
items.type = items.type.apply(lambda x: x.replace(' ',''))
items.loc[(items.type == 'pc')|(items.type == 'pс')|(items.type == 'рс'), 'type'] = 'pc'
items.loc[(items.type == 'рs3'), 'type'] = 'ps3'
group_sum = items.groupby('type' ).sum()
drop_list = group_sum.loc[group_sum.item_category_id < 200].index
print('drop list:', drop_list)
 items.name_2 = items.type.apply(lambda x: 'etc' if x in drop_list else x)
 items = items.drop(['type'], axis=1)
 print(items.groupby('name_2' ).count() [['item_id']])
 items['name_2'] = LabelEncoder().fit_transform(items['name_2'] ).astype(np.int8)
 items['name_3'] = LabelEncoder().fit_transform(items['name_3'] ).astype(np.int16)
 items.drop(['item_name', 'name_1'], axis=1, inplace=True )<data_type_conversions>
custom_precision = make_scorer(precision_score, pos_label=0, zero_division=0)
 custom_recall = make_scorer(recall_score, pos_label=0 )
ts = time.time()
matrix = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train[train.date_block_num==i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix['date_block_num'] = matrix['date_block_num'].astype(np.int8)
 matrix['shop_id'] = matrix['shop_id'].astype(np.int8)
 matrix['item_id'] = matrix['item_id'].astype(np.int16)
 matrix.sort_values(cols,inplace=True)
 print('Use time:', time.time() - ts)
<merge>
f1 = KNeighborsClassifier(n_neighbors=4)
 f2 = KNeighborsClassifier(n_neighbors=9)
 f3 = KNeighborsClassifier(n_neighbors=11)
 female_pipeline = Pipeline(steps=[ 
('preprocessor', preprocessor), 
('voting', VotingClassifier([ 
('f1', f1),('f2', f2),('f3', f3)]))
])
 print('9-fold precision of the ensemble: {:.3f}'.format(
 cross_val_score(female_pipeline, X_f, y_f, cv=9, scoring=custom_precision ).mean()))
print('9-fold recall of the ensemble: {:.3f}'.format(
 cross_val_score(female_pipeline, X_f, y_f, cv=9, scoring=custom_recall ).mean()))
print('9-fold accuracy of the ensemble: {:.3f}'.format(
 cross_val_score(female_pipeline, X_f, y_f, cv=9 ).mean()))
female_pipeline.fit(X_f, y_f)
 learn_train_f = female_pipeline.predict(X_f)
 X_test_f = test_female[features]
predictions_f = female_pipeline.predict(X_test_f)
 print('
The following 6 non-WCG females are predicted to die:')
 test_female.loc[(predictions_f==0), cols]
train['revenue'] = train['item_price'] * train['item_cnt_day']
ts = time.time()
group = train.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=cols, how='left')
 matrix['item_cnt_month'] =(matrix['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))
print('Use time:', time.time() - ts)
<data_type_conversions>
data.loc[891:1308, 'Predict'][(data.Sex=='female')&(data.WCSurvived.isnull())] = predictions_f
output = pd.DataFrame({'PassengerId': data[891:1309].PassengerId, 'Survived': data[891:1309].Predict.astype('int')})
 output.to_csv('WCG_male_female.csv', index=False)
 print('WCG_male_female was successfully saved!')
 print('Submission is loading...you scored 82,8%!' )