Kaggle competition: Digit Recognizer
matrix = lag_feature(matrix, [1, 2, 3], ['item_cnt_month'] )<merge>
import matplotlib.pyplot as plt
from collections import Counter
group =(
 matrix 
.groupby('date_block_num')
.agg({
'item_cnt_month' : 'mean'
})
)
 group.columns = ['date_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on='date_block_num', how="left")
 matrix['date_avg_item_cnt'] = matrix['date_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_avg_item_cnt'])
 matrix.drop(['date_avg_item_cnt'], axis=1, inplace=True )<merge>
fg_unet = pd.read_csv("/kaggle/input/mnist-w-fgunet-output/submission.csv")
 vgg = pd.read_csv("/kaggle/input/mnist-w-vgg16-output/submission.csv")
 resnet = pd.read_csv("/kaggle/input/mnist-w-resnet-output/submission.csv" )
group =(
 matrix 
.groupby(['date_block_num', 'item_id'])
.agg({
'item_cnt_month': 'mean'
})
)
 group.columns = ['date_item_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id'], how='left')
 matrix['date_item_avg_item_cnt'] = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1, 2, 3], ['date_item_avg_item_cnt'])
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True )<merge>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 sub = pd.read_csv("/kaggle/input/mnist-w-resnet-output/submission.csv" )
group =(
 matrix 
.groupby(['date_block_num', 'shop_id'])
.agg({
'item_cnt_month': 'mean'
})
)
 group.columns = ['date_shop_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id'], how='left')
 matrix['date_shop_avg_item_cnt'] = matrix['date_shop_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1, 2, 3], ['date_shop_avg_item_cnt'])
 matrix.drop(['date_shop_avg_item_cnt'], axis=1, inplace=True )<merge>
majority = sub.copy()
no_maj = []
unequal = []
for i in range(len(vgg)) :
lst = [fg_unet.iloc[i].Label, vgg.iloc[i].Label, resnet.iloc[i].Label]
if not all(ele == lst[0] for ele in lst):
unequal.append(i)
 count = Counter(lst ).most_common()
if len(count)==len(lst):
no_maj.append(i)
 majority.iloc[i].Label = lst[-1]
img = test.iloc[i,:].values
img = img.reshape(28, 28)
 print(i, lst)
 plt.imshow(img)
 plt.show()
else:
majority.iloc[i].Label = count[0][0]
else:
majority.iloc[i].Label = lst[0]
print("Number of rows not all equal: ", len(unequal))
print("Number of rows with no majority: ", len(no_maj))
majority.to_csv("submission_maj.csv", index=False )
group =(
 matrix 
.groupby(['date_block_num', 'shop_id', 'item_id'])
.agg({
'item_cnt_month': 'mean'
})
)
 group.columns = ['date_shop_item_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'item_id'], how='left')
 matrix['date_shop_item_avg_item_cnt'] = matrix['date_shop_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1, 2, 3], ['date_shop_item_avg_item_cnt'])
 matrix.drop(['date_shop_item_avg_item_cnt'], axis=1, inplace=True )<merge>
from keras.models import load_model
from keras.utils import to_categorical
group =(
 matrix 
.groupby(['date_block_num', 'shop_id', 'subtype_code'])
.agg({
'item_cnt_month': 'mean'
})
)
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix['date_shop_subtype_avg_item_cnt'] = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_shop_subtype_avg_item_cnt'])
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True )<data_type_conversions>
fg_unet = load_model("/kaggle/input/mnist-w-fgunet-output/best_model.h5")
 vgg = load_model("/kaggle/input/mnist-w-vgg16-output/best_model.h5")
 resnet = load_model("/kaggle/input/mnist-w-resnet-output/best_model.h5" )
group =(
 matrix 
.groupby(['date_block_num', 'shop_city'])
.agg({
'item_cnt_month': 'mean'
})
)
 group.columns = ['date_city_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_city'], how='left')
 matrix['date_city_avg_item_cnt'] = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_city_avg_item_cnt'])
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True )<merge>
X = [train.iloc[i,1:].values for i in range(len(train)) ]
X = [x.reshape(28,28)for x in X]
X_28 = [x.reshape(28,28,1,1)for x in X]
X_28 = np.array(X_28)
 X = [np.pad(x, 2)for x in X]
X = np.array(X)
 X = X.reshape(X.shape[0],X.shape[1], X.shape[2],1)
 X = np.repeat(X, 3, axis=-1 )
group =(
 matrix 
.groupby(['date_block_num', 'item_id', 'shop_city'])
.agg({
'item_cnt_month': 'mean'
})
)
 group.columns = ['date_item_city_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'shop_city'], how='left')
 matrix['date_item_city_avg_item_cnt'] = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_item_city_avg_item_cnt'])
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True )<merge>
X_test = [test.iloc[i,:].values for i in range(len(test)) ]
X_test = [x.reshape(28,28)for x in X_test]
X_test_28 = [x.reshape(28,28,1,1)for x in X_test]
X_test_28 = np.array(X_test_28)
 X_test = [np.pad(x, 2)for x in X_test]
X_test = np.array(X_test)
 X_test = X_test.reshape(X_test.shape[0],X_test.shape[1], X_test.shape[2],1)
 X_test = np.repeat(X_test, 3, axis=-1 )
group =(
 train 
.groupby('item_id')
.agg({
'item_price': 'mean'
})
)
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = matrix.merge(group, on='item_id', how='left')
 matrix['item_avg_item_price'] = matrix['item_avg_item_price'].astype(np.float16)
 group =(
 train 
.groupby(['date_block_num', 'item_id'])
.agg({
'item_price': 'mean'
})
)
 group.columns = ['date_item_avg_item_price']
group.reset_index(inplace=True)
 matrix = matrix.merge(group, on=['date_block_num', 'item_id'], how='left')
 matrix['date_item_avg_item_price'] = matrix['date_item_avg_item_price'].astype(np.float16)
 lags = [1, 2, 3]
matrix = lag_feature(matrix, lags, ['date_item_avg_item_price'])
 for i in lags:
matrix['delta_price_lag_' + str(i)] =(
 matrix['date_item_avg_item_price_lag_' + str(i)] -\
matrix['item_avg_item_price'] 
)/ matrix['item_avg_item_price']
def select_trends(row):
for i in lags:
if row['delta_price_lag_' + str(i)]:
return row['delta_price_lag_' + str(i)]
return 0
matrix['delta_price_lag_'] = matrix.apply(select_trends, axis=1)
 matrix['delta_price_lag_'] = matrix['delta_price_lag_'].astype(np.float16)
 matrix['delta_price_lag_'].fillna(0, inplace=True)
 features_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
features_to_drop.append('date_item_avg_item_price_lag_' + str(i))
features_to_drop.append('delta_price_lag_' + str(i))
matrix.drop(features_to_drop, axis=1, inplace=True )<merge>
f_y_train = fg_unet.predict(X_28, verbose=1)
 v_y_train = vgg.predict(X, verbose=1)
 r_y_train = resnet.predict(X, verbose=1 )
group =(
 train 
.groupby(['date_block_num', 'shop_id'])
.agg({
'revenue': 'sum'
})
)
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = matrix.merge(group, on=['date_block_num', 'shop_id'], how='left')
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group =(
 group 
.groupby('shop_id')
.agg({
'date_block_num': 'mean'
})
)
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = matrix.merge(group, on='shop_id', how='left')
 matrix['shop_avg_revenue'] = matrix['shop_avg_revenue'].astype(np.float32)
 matrix['delta_revenue'] =(
 matrix['date_shop_revenue'] - matrix['shop_avg_revenue'] 
)/ matrix['shop_avg_revenue']
matrix['delta_revenue'] = matrix['delta_revenue'].astype(np.float32)
 matrix = lag_feature(matrix, [1], ['delta_revenue'])
 matrix['delta_revenue_lag_1'] = matrix['delta_revenue_lag_1'].astype(np.float32)
 matrix.drop(
 ['date_shop_revenue', 'shop_avg_revenue', 'delta_revenue'],
axis=1, inplace=True
)<categorify>
f_y_test = fg_unet.predict(X_test_28, verbose=1)
 v_y_test = vgg.predict(X_test, verbose=1)
 r_y_test = resnet.predict(X_test, verbose=1 )
matrix['month'] = matrix['date_block_num'] % 12
days = pd.Series([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8 )<categorify>
n_classes = 10
y = [train.iloc[i,0] for i in range(len(train)) ]
y = np.array(y)
 print(np.unique(y, return_counts=True))
y = to_categorical(y, num_classes=n_classes)
 y.shape
matrix['item_shop_first_sale'] =(
 matrix['date_block_num'] - matrix.groupby(['item_id', 'shop_id'])['date_block_num'].transform('min')
)
 matrix['item_first_sale'] =(
 matrix['date_block_num'] - matrix.groupby(['item_id'])['date_block_num'].transform('min')
 )<drop_column>
X = np.hstack([f_y_train, v_y_train, r_y_train])
 rid = Ridge()
rid.fit(X, y )
matrix = matrix[matrix['date_block_num'] >= 4]
matrix.head().T<create_dataframe>
X_pred = np.hstack([f_y_test, v_y_test, r_y_test] )
data = matrix.copy()<prepare_x_and_y>
y_pred = rid.predict(X_pred)
 y_pred = np.argmax(y_pred, axis=-1)
 print(y_pred.shape )
X_train = data[data.date_block_num <= 32].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num <= 32]['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 )<train_model>
ridge = sub.copy()
for i in range(len(y_pred)) :
ridge.iloc[i].Label = y_pred[i]
ridge.to_csv("submission_ridge.csv", index=False )
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
seed=42 
)
 model.fit(
 X_train,
Y_train,
eval_metric='rmse',
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds=20
)<save_to_csv>
init = Constant (.333)
 X = Input(shape=(10,3))
out = Conv1D(1, 1, use_bias=False, kernel_initializer=init, kernel_regularizer='l2' )(X)
 out = Flatten()(out)
 out = Softmax()(out)
 model = Model(X, out)
 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])
 model.summary()
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
'ID': test.index,
'item_cnt_month': Y_test
})
 submission.to_csv('xgb_submission.csv', index=False )<import_modules>
X = np.stack([f_y_train, v_y_train, r_y_train], axis=-1)
 X_pred = np.stack([f_y_test, v_y_test, r_y_test], axis=-1)
 X.shape, X_pred.shape
import numpy as np
import pandas as pd
import joblib
import gc<load_from_csv>
model.fit(X, y, epochs=10 )
test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv')
 test.set_index(['SK_ID_CURR'], inplace=True)
 test.shape<load_pretrained>
y_pred = model.predict(X_pred)
 print(y_pred[0])
 y_pred = np.argmax(y_pred, axis=-1)
 print(y_pred.shape )
preprocessor = joblib.load('.. /input/wk6-default/wk6default_preprocessor.joblib')
 LGBM_model = joblib.load('.. /input/wk6-default/wk6_LGBM_default_model.joblib' )<load_from_csv>
weighted = sub.copy()
for i in range(len(y_pred)) :
weighted.iloc[i].Label = y_pred[i]
weighted.to_csv("submission_weighted.csv", index=False )
bureau_bal = pd.read_csv('.. /input/home-credit-default-risk/bureau_balance.csv')
 bureau = pd.read_csv('.. /input/home-credit-default-risk/bureau.csv')
 bb = pd.merge(bureau, bureau_bal, on = 'SK_ID_BUREAU', how = 'left')
 bb['REMAIN_CRED'] = bb['AMT_CREDIT_SUM'] - bb['AMT_CREDIT_SUM_DEBT'] - bb['AMT_CREDIT_SUM_LIMIT']
bb['AC_RATIO'] = bb['AMT_ANNUITY'] / bb['AMT_CREDIT_SUM']
bb.columns = ['BU_'+column if column !=('SK_ID_CURR')
 else column for column in bb.columns]
bur_cat = pd.get_dummies(bb.select_dtypes('object'))
bur_cat['SK_ID_CURR'] = bb['SK_ID_CURR']
bur_cat = bur_cat.groupby(by = ['SK_ID_CURR'] ).agg(['mean'])
 bur_num = bb.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 bureau_rev = bur_cat.merge(bur_num, on = ['SK_ID_CURR'], how = 'left')
 test = test.merge(bureau_rev, on = ['SK_ID_CURR'], how = 'left')
 del bur_cat
del bur_num
del bureau
del bureau_bal
gc.collect()<feature_engineering>
weights = model.layers[1].get_weights()
weights = weights/np.sum(weights)
 weights
cc_bal = pd.read_csv('.. /input/home-credit-default-risk/credit_card_balance.csv')
 cc_bal['DRAW_RATIO'] = cc_bal['AMT_DRAWINGS_CURRENT'] / cc_bal['CNT_DRAWINGS_CURRENT']
cc_bal['RECEIVE_RATIO'] = cc_bal['AMT_RECIVABLE'] / cc_bal['AMT_RECEIVABLE_PRINCIPAL']
cc_bal['RECEIVE_PER'] = cc_bal['AMT_RECIVABLE'] / cc_bal['AMT_TOTAL_RECEIVABLE']
cc_bal.columns = ['CC_'+ column if column !='SK_ID_CURR'
else column for column in cc_bal.columns]
cc_cat = pd.get_dummies(cc_bal.select_dtypes('object'))
cc_cat['SK_ID_CURR'] = cc_bal['SK_ID_CURR']
cc_cat = cc_cat.groupby(by = ['SK_ID_CURR'] ).mean()
cc_num = cc_bal.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 test = test.merge(cc_cat, on = ['SK_ID_CURR'], how = 'left')
 test = test.merge(cc_num, on = ['SK_ID_CURR'], how = 'left')
 del cc_bal
del cc_cat
del cc_num
gc.collect()<feature_engineering>
!pip install pyrankvote
install = pd.read_csv('.. /input/home-credit-default-risk/installments_payments.csv')
 install['PAY_PERCENT'] = install['AMT_INSTALMENT'] / install['AMT_PAYMENT']
install['PAY_DIFF'] = install['AMT_INSTALMENT'] - install['AMT_PAYMENT']
install['DPD'] = install['DAYS_ENTRY_PAYMENT'] - install['DAYS_INSTALMENT']
install['DPD'] = install['DPD'].apply(lambda x: x if x>0 else 0)
 install['DBD'] = install['DAYS_INSTALMENT'] - install['DAYS_ENTRY_PAYMENT']
install['DBD'] = install['DBD'].apply(lambda x: x if x>0 else 0)
 install.columns = ['IP_'+ column if column !='SK_ID_CURR'
else column for column in install.columns]
inst_num = install.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean'] ).astype('float32')
 test = test.merge(inst_num, on = 'SK_ID_CURR', how='left')
 del install
del inst_num
gc.collect()<load_from_csv>
import pyrankvote
from pyrankvote import Candidate, Ballot
pos = pd.read_csv('.. /input/home-credit-default-risk/POS_CASH_balance.csv')
 pos.columns = ['PC_'+ column if column !='SK_ID_CURR'
else column for column in pos.columns]
pos_num = pos.groupby(by = ['SK_ID_CURR'] ).agg(['max', 'mean', 'sum'] ).astype('float32')
 test = test.merge(pos_num, on = ['SK_ID_CURR'], how = 'left')
 del pos
del pos_num
gc.collect() 
<feature_engineering>
candidates = [Candidate(i)for i in range(10)]
<feature_engineering><EOS> 
single = sub.copy()
runoff = sub.copy()
for i in range(len(X_pred)) :
ballots = []
for j in range(3):
ballot = np.argsort(X_pred[i,:,j])
 ballot = np.flip(ballot)
 ballots.append(Ballot(ranked_candidates=[candidates[i] for i in ballot]))
election_result = pyrankvote.single_transferable_vote(candidates, ballots, number_of_seats=1)
 winners = election_result.get_winners()
single.iloc[i].Label = winners[0].name
election_result = pyrankvote.instant_runoff_voting(candidates, ballots)
 winners = election_result.get_winners()
runoff.iloc[i].Label = winners[0].name
single.to_csv("submission_single.csv", index=False)
 runoff.to_csv("submission_runoff.csv", index=False )