Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
!pip install albumentations
!pip install pretrainedmodels
!pip install torchsummary
%matplotlib inline
train_on_gpu = True
batch_size = 64
batch_size_test = 64
num_workers = 4
target_names = ['class 0', 'class 1']<categorify>
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))
class MyDataset(Dataset):
def __init__(self, datatype='train', df=None,transform=None ,augument_=True,dataloc=".. /input/histopathologic-cancer-detection/train/"):
self.datatype = datatype
self.df = df
self.augument = augument_
self.transform = transform
self.dataloc = dataloc
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
label = self.df[idx][1]
img_name = self.df[idx][0]+'.tif'
img_dir = os.path.join(self.dataloc,img_name)
 img = cv2.imread(img_dir)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 img = self.transform(image=img)
 return img['image'],label<set_options>
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )
data_transforms = albumentations.Compose([
albumentations.Resize(96, 96),
albumentations.RandomRotate90(p=0.5),
albumentations.Transpose(p=0.5),
albumentations.Flip(p=0.5),
albumentations.OneOf([
albumentations.CLAHE(clip_limit=2), albumentations.IAASharpen() , albumentations.IAAEmboss() ,
albumentations.RandomBrightness() , albumentations.RandomContrast() ,
albumentations.JpegCompression() , albumentations.Blur() , albumentations.GaussNoise() ], p=0.5),
albumentations.HueSaturationValue(p=0.5),
albumentations.ShiftScaleRotate(shift_limit=0.15, scale_limit=0.15, rotate_limit=45, p=0.5),
albumentations.Normalize() ,
AT.ToTensor()
])
 data_transforms_test = albumentations.Compose([
albumentations.Resize(96, 96),
albumentations.Normalize() ,
AT.ToTensor()
] )<choose_model_class>
null_cnt = train.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 train.dropna(inplace=True )
class Densenet169(nn.Module):
def __init__(self, pretrained=True):
super(Densenet169, self ).__init__()
self.model = models.densenet169(num_classes=1000,pretrained=pretrained)
 self.linear = nn.Linear(1000+2, 16)
 self.bn = nn.BatchNorm1d(16)
 self.dropout = nn.Dropout(0.2)
 self.elu = nn.ELU()
self.out = nn.Linear(16, 1)
 self.sig = nn.Sigmoid()
def forward(self, x):
out = self.model(x)
 batch = out.shape[0]
max_pool, _ = torch.max(out, 1, keepdim=True)
 avg_pool = torch.mean(out, 1, keepdim=True)
 out = out.view(batch, -1)
 conc = torch.cat(( out, max_pool, avg_pool), 1)
 conc = self.linear(conc)
 conc = self.elu(conc)
 conc = self.bn(conc)
 conc = self.dropout(conc)
 res = self.out(conc)
 res = self.sig(res)
 return res
model_conv = Densenet169(pretrained=False)
 model_conv.cuda()<load_from_disk>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
model_conv.load_state_dict(torch.load(".. /input/des-model1/model(4 ).pt")) 
<load_from_csv>
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )
test = pd.read_csv('.. /input/histopathologic-cancer-detection/sample_submission.csv')
 test_dataset = MyDataset(datatype='test',df=test.values,transform=data_transforms_test,augument_=False,dataloc=".. /input/histopathologic-cancer-detection/test/")
 test_loader = DataLoader(test_dataset, batch_size=128, num_workers=num_workers, pin_memory=True,shuffle=False )<feature_engineering>
print(group['players in group'].nlargest(5))
del match,group
Sig = nn.Sigmoid()
model_conv.eval()
preds = []
with torch.no_grad() :
for batch_i,(data, target)in tqdm(enumerate(test_loader),total=len(test_loader)) :
data, target = data.cuda() , target.cuda()
output = model_conv(data)
 pr = output.detach().cpu().numpy()
for i in pr:
preds.append(i[0] )<prepare_output>
subset = train[train['matchId']=='41a634f62f86b7']
sub_grp = subset[subset['groupId']=='128b07271aa012']
print('matchId=='41a634f62f86b7' & groupId=='128b07271aa012'')
 print('-'*50)
 print('players:',len(subset))
print('groups:',subset['groupId'].nunique())
 print('numGroups:',subset['numGroups'].unique())
 print('maxPlace:',subset['maxPlace'].unique())
 print('-'*50)
 print('max-group players:',len(sub_grp))
print('max-group winPlacePerc:',sub_grp['winPlacePerc'].unique())
 print('-'*50)
 print('winPlacePerc:',subset['winPlacePerc'].sort_values().unique() )
a = np.array(preds)
<merge>
train.groupby('matchId' ).agg({'matchDuration':'var'} ).fillna(0 ).query('matchDuration != 0' )
sub = pd.read_csv('.. /input/histopathologic-cancer-detection/sample_submission.csv')
 test_preds = pd.DataFrame({'imgs': test["id"].values.tolist() , 'preds': a})
 test_preds['imgs'] = test_preds['imgs'].apply(lambda x: x.split('.')[0])
 sub = pd.merge(sub, test_preds, left_on='id', right_on='imgs')
 sub<save_to_csv>
cols = ['killPoints','rankPoints','winPoints']
querys = ['rankPoints <= 0 & killPoints != 0', 'rankPoints <= 0 & winPoints != 0',
'killPoints == 0 & rankPoints != 0', 'killPoints == 0 & winPoints != 0',
'winPoints == 0 & rankPoints != 0', 'winPoints == 0 & killPoints != 0']
for q in querys:
print('count(%s):' % q, len(train[cols].query(q)) )
sub = sub[['id', 'preds']]
sub.columns = ['id', 'label']
sub.to_csv('sub.csv', index=False)
<import_modules>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg<load_from_csv>
print('match count:', train['matchId'].nunique())
 maxPlacePerc = train.groupby('matchId')['winPlacePerc'].max()
print('match [not contains 1st place]:', len(maxPlacePerc[maxPlacePerc != 1]))
del maxPlacePerc
edge = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(edge.groupby('matchId')))
 print(' - unique winPlacePerc:', edge['winPlacePerc'].unique() )
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train.shape<prepare_x_and_y>
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1 )<feature_engineering>
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
X_train = X_train / 255.0
test = test / 255.0<train_model>
match = all_data.groupby('matchId')
 all_data['killPlacePerc'] = match['kills'].rank(pct=True ).values
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<import_modules>
distance =(all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance'])
 all_data['zombi'] =(( distance == 0)&(all_data['kills'] == 0)
 &(all_data['weaponsAcquired'] == 0)
 &(all_data['matchType'].str.contains('solo')) ).astype(int)
 all_data['cheater'] =(( all_data['kills'] / distance >= 1)
 |(all_data['kills'] > 30)|(all_data['roadKills'] > 10)).astype(int)
 del distance
pd.concat([all_data['zombi'].value_counts() , all_data['cheater'].value_counts() ], axis=1 ).T
from keras.utils.np_utils import to_categorical<categorify>
all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )
Y_train = to_categorical(Y_train, num_classes = 10 )<import_modules>
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )
from sklearn.model_selection import train_test_split<import_modules>
all_data['_totalDistance'] = all_data['rideDistance'] + all_data["walkDistance"] + all_data["swimDistance"]
all_data["_specialKills"] = all_data["headshotKills"] + all_data["roadKills"]
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
all_data['_killsOverDistance'] = all_data['kills'] / all_data['_totalDistance']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
fillInf(all_data, 0 )
from sklearn.model_selection import train_test_split<split>
agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','matchDuration','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col )
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=10 )<import_modules>
def grouping(df):
group = df.groupby(['matchId','groupId','matchType'])
 groupCount = group.size().to_frame('member')
 groupMean = group.mean()
groupMax = group[agg_col].max().rename(columns=lambda s: '_max.' + s)
 groupMin = group[agg_col].min().rename(columns=lambda s: '_min.' + s)
 return pd.concat([groupCount, groupMean, groupMax, groupMin], axis=1 ).reset_index()
all_data = grouping(all_data)
 all_data = reduce_mem_usage(all_data)
 print(all_data.shape )
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
import tensorflow as tf<choose_model_class>
cols = np.r_[agg_col,['matchId']]
match = all_data[cols].groupby('matchId' )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.Conv2D(128,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(128,(3, 3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(192,(3, 3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(192,(3, 3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.5),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ] )<choose_model_class>

 cols = agg_col
matchSum = match[cols].sum().rename(columns=lambda s: '_sum.' + s ).reset_index()
all_data = pd.merge(all_data, matchSum)
 print(all_data.shape)
 del matchSum
gc.collect()
for c in cols:
all_data['_percSum.' + c] = all_data[c] / all_data['_sum.' + c]
fillInf(all_data, 0 )
model.compile(optimizer = keras.optimizers.RMSprop(learning_rate=0.001, rho=0.9), loss = "categorical_crossentropy", metrics=["accuracy"] )<train_model>

 cols = agg_col
matchMean = match[cols].mean().rename(columns=lambda s: '_matchMean.' + s ).reset_index()
all_data = pd.merge(all_data, reduce_mem_usage(matchMean))
print(all_data.shape)
 del matchMean
gc.collect()
for c in cols:
all_data['_percMean.' + c] = all_data[c] / all_data['_matchMean.' + c]
all_data.drop(['_matchMean.' + c], axis=1, inplace=True)
 fillInf(all_data, 0 )
model.fit(X_train, Y_train, batch_size = 86, epochs =50, validation_data =(X_val, Y_val), verbose = 1 )<save_to_csv>

 killMinorRank = all_data[['matchId','_min.kills','_max.killPlace']].copy()
killMinorRank['_rank.minor.killPlace'] = killMinorRank.groupby(['matchId','_min.kills'] ).rank().values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_fourth.csv",index=False )<load_from_csv>
null_cnt = all_data.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 all_data.head()
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
mapper = lambda x: 'solo' if('solo' in x)else 'duo' if('duo' in x)or('crash' in x)else 'squad'
all_data['matchTypeCat'] = all_data['matchType'].map(mapper)
 print(all_data['matchTypeCat'].value_counts() )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )
X_train = train.iloc[:, 1:].values.reshape(-1,28,28,1)
 Y_train = train.iloc[:, 0]
x_test = test.values.reshape(-1,28,28,1 )<set_options>
cols = [col for col in all_data.columns if col not in ['Id','matchId','groupId']]
for i, t in all_data.loc[:, cols].dtypes.iteritems() :
if t == object:
all_data[i] = pd.factorize(all_data[i])[0]
seed_value= 1998
os.environ['PYTHONHASHSEED']=str(seed_value)
 random.seed(seed_value)
 np.random.seed(seed_value)
 tf.set_random_seed(seed_value)
 config = tf.ConfigProto(
 device_count = {'GPU': 1, 'CPU': 100}, 
)
 set_session(tf.Session(config=config))
print(tensorflow_backend._get_available_gpus() )<feature_engineering>
X_train = all_data[all_data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = all_data[all_data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del all_data
gc.collect()
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId','groupId']].copy()
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True)
 print(X_train.shape, X_test.shape )
X_train = X_train/255
x_test = x_test/255<split>
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:10] )
KFold = 5
Folds = StratifiedKFold(n_splits=KFold, shuffle=True ).split(X_train, Y_train )<categorify>
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 31,
'verbose': 0,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
mts = list()
fis = list()
pred = np.zeros(X_test.shape[0])
 for mt in X_train['matchTypeCat'].unique() :
idx = X_train[X_train['matchTypeCat'] == mt].index
reg = lgb.LGBMRegressor(**params, n_estimators=1000)
 reg.fit(X_train.loc[idx], Y_train.loc[idx])
 idx = X_test[X_test['matchTypeCat'] == mt].index
pred[idx] = reg.predict(X_test.loc[idx], num_iteration=reg.best_iteration_)
 mts.append(mt)
 fis.append(reg.feature_importances_ )
classifiers = []
for train_idx, valid_idx in Folds:
FOLD = len(classifiers)+1
print("STARTED TRAINING ON FOLD-%d"%(FOLD))
x_train, x_valid = X_train[train_idx], X_train[valid_idx]
y_train, y_valid = to_categorical(Y_train[train_idx], 10), to_categorical(Y_train[valid_idx], 10)
 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False 
)
 datagen.fit(x_train)
 callbacks = [
EarlyStopping(monitor='val_loss', patience=15, verbose=1, mode='auto', restore_best_weights=True),
ModelCheckpoint(filepath='MNIST_DigitRecognizer_CNN_Fold%d.hdf5'%(FOLD), monitor='val_loss', save_best_only=True, verbose=1),
ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.5, min_lr=0.000001)
 ]
clf = Sequential([
Conv2D(32, kernel_size=(3,3), input_shape=(28,28,1), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
Conv2D(32, kernel_size=(3,3), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
MaxPooling2D() ,
Conv2D(64, kernel_size=(3,3), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
Conv2D(64, kernel_size=(3,3), activation='relu', padding='same', kernel_initializer='glorot_uniform'),
MaxPooling2D() ,
Dropout(rate=0.15),
Flatten() ,
Dense(512, activation='relu', kernel_initializer='glorot_uniform'),
Dropout(rate=0.5),
Dense(10, activation='softmax', kernel_initializer='glorot_uniform')
 ])
 clf.compile(loss='categorical_crossentropy', optimizer=adam(lr=0.001))
train_history = clf.fit_generator(
 datagen.flow(x_train, y_train, batch_size=32),
validation_data=(x_valid, y_valid),
steps_per_epoch=len(x_train)/32,
epochs=50,
workers=6,
use_multiprocessing=True,
callbacks = callbacks,
verbose=0 
)
 plot_eval_result(train_hist = train_history)
 classifiers.append(clf)
<predict_on_test>
X_test_grp['winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1)
 sub_match = X_test_grp[['matchId','_rank.winPlacePerc']].groupby(['matchId'])
 sub_group = group.count().reset_index() ['matchId'].to_frame()
X_test = pd.merge(X_test, sub_group )
final_result = sum(clf.predict(x_test)for clf in classifiers)/KFold
final_result = np.argmax(final_result, axis=1 )<save_to_csv>
fullgroup =(X_test['numGroups'] == X_test['maxPlace'])
 subset = X_test.loc[fullgroup]
X_test.loc[fullgroup, 'winPlacePerc'] =(subset['_rank.winPlacePerc'].values - 1)/(subset['maxPlace'].values - 1)
 subset = X_test.loc[~fullgroup]
gap = 1.0 /(subset['maxPlace'].values - 1)
 new_perc = np.around(subset['winPlacePerc'].values / gap)* gap
X_test.loc[~fullgroup, 'winPlacePerc'] = new_perc
y_pred = pd.DataFrame(data = final_result, index=list(range(1, test.shape[0]+1)) , columns = ['Label'])
 y_pred.to_csv("output.csv", index=True, index_label='ImageId' )<import_modules>
X_test.loc[~fullgroup, '_pred.winPlacePerc'] = np.around(X_test.loc[~fullgroup, 'winPlacePerc'].values / gap)+ 1
_=
X_test.loc[~fullgroup &(X_test['matchId'] == '000b598b79aa5e'),
['matchId','groupId','winPlacePerc','maxPlace','numGroups','_pred.winPlacePerc','_rank.winPlacePerc']
].sort_values(['matchId','_pred.winPlacePerc'] )
import numpy as np
import pandas as pd
from sklearn import ensemble
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from time import time<load_from_csv>
_=
train = pd.read_csv('.. /input/titanic/train.csv')
 X_test = pd.read_csv('.. /input/titanic/test.csv')
 id_for_subm = X_test['PassengerId'].copy()
train.head()<prepare_x_and_y>
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0
X_test['winPlacePerc'].describe()
<categorify><EOS> 
test = pd.read_csv('.. /input/test_V2.csv')
 submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )