Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
punct_mapping = {"_":" ", "`":" "}<categorify>
def reduce_mem_usage_func(df):

 start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df
def get_sampled_data(filename, sample_size):
n = sum(1 for line in open(filename)) - 1
skip = sorted(random.sample(range(1,n+1),n-sample_size))
df = pd.read_csv(filename, skiprows=skip)
 return df
def read_fast(filename, sample=True, sample_size=200000, reduce_mem_usage=True):
start_time = time.time()
df = get_sampled_data(filename, sample_size)if sample else pd.read_csv(filename)
 new_df = reduce_mem_usage_func(df)if reduce_mem_usage else df
elapsed_time = int(time.time() - start_time)
 print('Time to get data frame: {:02d}:{:02d}:{:02d}'.format(
 elapsed_time // 3600, 
(elapsed_time % 3600 // 60),
elapsed_time % 60))
return new_df
def clean_special_chars(text, punct, mapping):
for p in mapping:
text = text.replace(p, mapping[p])
 for p in punct:
text = text.replace(p, f' {p} ')
 return text<feature_engineering>
warnings.filterwarnings('ignore')
 % matplotlib inline
df['comment_text'] = df['comment_text'].apply(lambda x: clean_special_chars(x, punct, punct_mapping))<compute_train_metric>
data = read_fast(".. /input/train_V2.csv", sample = False)
 data.head(10 )
vocab = build_vocab(df['comment_text'])
 oov = check_coverage(vocab, embeddings_index)
<set_options>
test_data = read_fast(".. /input/test_V2.csv", sample = False )
del(vocab,oov)
 gc.collect()<define_variables>
data[data['winPlacePerc'].isnull() ]
swear_words = [
' 4r5e ',
' 5h1t ',
' 5hit ',
' a55 ',
' anal ',
' anus ',
' ar5e ',
' arrse ',
' arse ',
' ass ',
' ass-fucker ',
' asses ',
' assfucker ',
' assfukka ',
' asshole ',
' assholes ',
' asswhole ',
' a_s_s ',
' b!tch ',
' b00bs ',
' b17ch ',
' b1tch ',
' ballbag ',
' balls ',
' ballsack ',
' bastard ',
' beastial ',
' beastiality ',
' bellend ',
' bestial ',
' bestiality ',
' biatch ',
' bitch ',
' bitcher ',
' bitchers ',
' bitches ',
' bitchin ',
' bitching ',
' bloody ',
' blow job ',
' blowjob ',
' blowjobs ',
' boiolas ',
' bollock ',
' bollok ',
' boner ',
' boob ',
' boobs ',
' booobs ',
' boooobs ',
' booooobs ',
' booooooobs ',
' breasts ',
' buceta ',
' bugger ',
' bum ',
' bunny fucker ',
' butt ',
' butthole ',
' buttmuch ',
' buttplug ',
' c0ck ',
' c0cksucker ',
' carpet muncher ',
' cawk ',
' chink ',
' cipa ',
' cl1t ',
' clit ',
' clitoris ',
' clits ',
' cnut ',
' cock ',
' cock-sucker ',
' cockface ',
' cockhead ',
' cockmunch ',
' cockmuncher ',
' cocks ',
' cocksuck ',
' cocksucked ',
' cocksucker ',
' cocksucking ',
' cocksucks ',
' cocksuka ',
' cocksukka ',
' cok ',
' cokmuncher ',
' coksucka ',
' coon ',
' cox ',
' crap ',
' cum ',
' cummer ',
' cumming ',
' cums ',
' cumshot ',
' cunilingus ',
' cunillingus ',
' cunnilingus ',
' cunt ',
' cuntlick ',
' cuntlicker ',
' cuntlicking ',
' cunts ',
' cyalis ',
' cyberfuc ',
' cyberfuck ',
' cyberfucked ',
' cyberfucker ',
' cyberfuckers ',
' cyberfucking ',
' d1ck ',
' damn ',
' dick ',
' dickhead ',
' dildo ',
' dildos ',
' dink ',
' dinks ',
' dirsa ',
' dlck ',
' dog-fucker ',
' doggin ',
' dogging ',
' donkeyribber ',
' doosh ',
' duche ',
' dyke ',
' ejaculate ',
' ejaculated ',
' ejaculates ',
' ejaculating ',
' ejaculatings ',
' ejaculation ',
' ejakulate ',
' f u c k ',
' f u c k e r ',
' f4nny ',
' fag ',
' fagging ',
' faggitt ',
' faggot ',
' faggs ',
' fagot ',
' fagots ',
' fags ',
' fanny ',
' fannyflaps ',
' fannyfucker ',
' fanyy ',
' fatass ',
' fcuk ',
' fcuker ',
' fcuking ',
' feck ',
' fecker ',
' felching ',
' fellate ',
' fellatio ',
' fingerfuck ',
' fingerfucked ',
' fingerfucker ',
' fingerfuckers ',
' fingerfucking ',
' fingerfucks ',
' fistfuck ',
' fistfucked ',
' fistfucker ',
' fistfuckers ',
' fistfucking ',
' fistfuckings ',
' fistfucks ',
' flange ',
' fook ',
' fooker ',
' fuck ',
' fucka ',
' fucked ',
' fucker ',
' fuckers ',
' fuckhead ',
' fuckheads ',
' fuckin ',
' fucking ',
' fuckings ',
' fuckingshitmotherfucker ',
' fuckme ',
' fucks ',
' fuckwhit ',
' fuckwit ',
' fudge packer ',
' fudgepacker ',
' fuk ',
' fuker ',
' fukker ',
' fukkin ',
' fuks ',
' fukwhit ',
' fukwit ',
' fux ',
' fux0r ',
' f_u_c_k ',
' gangbang ',
' gangbanged ',
' gangbangs ',
' gaylord ',
' gaysex ',
' goatse ',
' God ',
' god-dam ',
' god-damned ',
' goddamn ',
' goddamned ',
' hardcoresex ',
' hell ',
' heshe ',
' hoar ',
' hoare ',
' hoer ',
' homo ',
' hore ',
' horniest ',
' horny ',
' hotsex ',
' jack-off ',
' jackoff ',
' jap ',
' jerk-off ',
' jism ',
' jiz ',
' jizm ',
' jizz ',
' kawk ',
' knob ',
' knobead ',
' knobed ',
' knobend ',
' knobhead ',
' knobjocky ',
' knobjokey ',
' kock ',
' kondum ',
' kondums ',
' kum ',
' kummer ',
' kumming ',
' kums ',
' kunilingus ',
' l3itch ',
' labia ',
' lmfao ',
' lust ',
' lusting ',
' m0f0 ',
' m0fo ',
' m45terbate ',
' ma5terb8 ',
' ma5terbate ',
' masochist ',
' master-bate ',
' masterb8 ',
' masterbat3 ',
' masterbate ',
' masterbation ',
' masterbations ',
' masturbate ',
' mo-fo ',
' mof0 ',
' mofo ',
' mothafuck ',
' mothafucka ',
' mothafuckas ',
' mothafuckaz ',
' mothafucked ',
' mothafucker ',
' mothafuckers ',
' mothafuckin ',
' mothafucking ',
' mothafuckings ',
' mothafucks ',
' mother fucker ',
' motherfuck ',
' motherfucked ',
' motherfucker ',
' motherfuckers ',
' motherfuckin ',
' motherfucking ',
' motherfuckings ',
' motherfuckka ',
' motherfucks ',
' muff ',
' mutha ',
' muthafecker ',
' muthafuckker ',
' muther ',
' mutherfucker ',
' n1gga ',
' n1gger ',
' nazi ',
' nigg3r ',
' nigg4h ',
' nigga ',
' niggah ',
' niggas ',
' niggaz ',
' nigger ',
' niggers ',
' nob ',
' nob jokey ',
' nobhead ',
' nobjocky ',
' nobjokey ',
' numbnuts ',
' nutsack ',
' orgasim ',
' orgasims ',
' orgasm ',
' orgasms ',
' p0rn ',
' pawn ',
' pecker ',
' penis ',
' penisfucker ',
' phonesex ',
' phuck ',
' phuk ',
' phuked ',
' phuking ',
' phukked ',
' phukking ',
' phuks ',
' phuq ',
' pigfucker ',
' pimpis ',
' piss ',
' pissed ',
' pisser ',
' pissers ',
' pisses ',
' pissflaps ',
' pissin ',
' pissing ',
' pissoff ',
' poop ',
' porn ',
' porno ',
' pornography ',
' pornos ',
' prick ',
' pricks ',
' pron ',
' pube ',
' pusse ',
' pussi ',
' pussies ',
' pussy ',
' pussys ',
' rectum ',
' retard ',
' rimjaw ',
' rimming ',
' s hit ',
' s.o.b.',
' sadist ',
' schlong ',
' screwing ',
' scroat ',
' scrote ',
' scrotum ',
' semen ',
' sex ',
' sh!t ',
' sh1t ',
' shag ',
' shagger ',
' shaggin ',
' shagging ',
' shemale ',
' shit ',
' shitdick ',
' shite ',
' shited ',
' shitey ',
' shitfuck ',
' shitfull ',
' shithead ',
' shiting ',
' shitings ',
' shits ',
' shitted ',
' shitter ',
' shitters ',
' shitting ',
' shittings ',
' shitty ',
' skank ',
' slut ',
' sluts ',
' smegma ',
' smut ',
' snatch ',
' son-of-a-bitch ',
' spac ',
' spunk ',
' s_h_i_t ',
' t1tt1e5 ',
' t1tties ',
' teets ',
' teez ',
' testical ',
' testicle ',
' tit ',
' titfuck ',
' tits ',
' titt ',
' tittie5 ',
' tittiefucker ',
' titties ',
' tittyfuck ',
' tittywank ',
' titwank ',
' tosser ',
' turd ',
' tw4t ',
' twat ',
' twathead ',
' twatty ',
' twunt ',
' twunter ',
' v14gra ',
' v1gra ',
' vagina ',
' viagra ',
' vulva ',
' w00se ',
' wang ',
' wank ',
' wanker ',
' wanky ',
' whoar ',
' whore ',
' willies ',
' willy ',
' xrated ',
' xxx '
]<string_transform>
data['headshotrate'] = data['headshotKills']/data['kills']
data['healthitems'] = data['heals'] + data['boosts']
data['totalDistance'] = data['rideDistance'] + data["walkDistance"] + data["swimDistance"]
data['killPlace_over_maxPlace'] = data['killPlace'] / data['maxPlace']
data['killsPerWalkDistance'] = data['kills'] /(data['walkDistance'] + 1)
 data['allKills'] = data['headshotKills']+data['kills']+data['roadKills']+data['teamKills']+data['assists']
replace_with_fuck = []
for swear in swear_words:
if swear[1:(len(swear)-1)] not in embeddings_index:
replace_with_fuck.append(swear)
 replace_with_fuck = '|'.join(replace_with_fuck)
 replace_with_fuck 
<categorify>
test_data['headshotrate'] =test_data['headshotKills']/ test_data['kills']
test_data['healthitems'] = test_data['heals'] + test_data['boosts']
test_data['totalDistance'] = test_data['rideDistance'] + test_data["walkDistance"] + test_data["swimDistance"]
test_data['killPlace_over_maxPlace'] = test_data['killPlace'] / test_data['maxPlace']
test_data['killsPerWalkDistance'] = test_data['kills'] /(test_data['walkDistance'] +1)
 test_data['allKills'] = test_data['headshotKills']+test_data['kills']+test_data['roadKills']+test_data['teamKills']+test_data['assists']
def handle_swears(text):
text = re.sub(replace_with_fuck, ' fuck ', text)
 return text<feature_engineering>
data['killsWithoutMoving'] =(( data['kills'] > 0)&(data['totalDistance'] == 0))
test_data['killsWithoutMoving'] =(( test_data['kills'] > 0)&(test_data['totalDistance'] == 0))
df['comment_text'] = df['comment_text'].apply(lambda x: handle_swears(x))
gc.collect()<split>
data.drop(data[data['killsWithoutMoving'] == True].index, inplace=True)
 data.drop(data[data['roadKills'] > 10].index, inplace=True)
 data.drop(data[data['kills'] >= 40].index, inplace=True)
 data.drop(data[data['longestKill'] >= 1000].index, inplace=True)
 data.drop(data[data['heals'] >= 40].index, inplace=True )
train = df.iloc[:1804874,:]
test = df.iloc[1804874:,:]
train.head()<set_options>
lbl = LabelEncoder()
lbl.fit(list(data['matchType'].values))
data['matchType'] = lbl.transform(list(data['matchType'].values))
lbl.fit(list(test_data['matchType'].values))
test_data['matchType'] = lbl.transform(list(test_data['matchType'].values))
del(df)
 gc.collect()<load_from_csv>
cols = [col for col in data.columns if col not in ['Id','matchId','groupId']]
for i, t in data.loc[:, cols].dtypes.iteritems() :
if t == object:
data[i] = pd.factorize(data[i])[0]
cols = [col for col in test_data.columns if col not in ['Id','matchId','groupId']]
for i, t in test_data.loc[:, cols].dtypes.iteritems() :
if t == object:
test_data[i] = pd.factorize(test_data[i])[0]
train_orig = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv")
 train_orig.head()<concatenate>
total = data.isnull().sum().sort_values(ascending = False)
 percent =(data.isnull().sum() /data.isnull().count() ).sort_values(ascending = False)
 missing_data = pd.concat([total,percent], axis = 1, keys = ['Total', 'Percent'])
 missing_data.head(20 )
train = pd.concat([train,train_orig[['target']]],axis=1)
 train.head()<set_options>
data.drop(2744604, inplace =True)
del(train_orig)
 gc.collect()<feature_engineering>
data= data.dropna()
train['target'] = np.where(train['target'] >= 0.5, True, False )<split>
data = data.drop(columns=['groupId','matchId'], axis = 1)
 test_data = test_data.drop(columns=['groupId','matchId'], axis = 1 )
train_df, validate_df = model_selection.train_test_split(train, test_size=0.1)
 print('%d train comments, %d validate comments' %(len(train_df), len(validate_df)) )<string_transform>
y = data.winPlacePerc
X = data.drop(['winPlacePerc', 'Id'], axis=1 )
MAX_NUM_WORDS = 100000
TOXICITY_COLUMN = 'target'
TEXT_COLUMN = 'comment_text'
tokenizer = Tokenizer(num_words=MAX_NUM_WORDS)
 tokenizer.fit_on_texts(train_df[TEXT_COLUMN])
 MAX_SEQUENCE_LENGTH = 256
def pad_text(texts, tokenizer):
return pad_sequences(tokenizer.texts_to_sequences(texts), maxlen=MAX_SEQUENCE_LENGTH )<set_options>
def identify_zero_importance_features(X, y, iterations = 2):

 feature_importances = np.zeros(X.shape[1])
 model = lgb.LGBMRegressor(objective='regression', boosting_type = 'goss',
n_estimators =10000, class_weight = 'balanced')
 for i in range(iterations):
train_features, valid_features, train_y, valid_y = train_test_split(X, y,
test_size = 0.25,
random_state = i)
 model.fit(train_features, train_y, early_stopping_rounds=200,
eval_set = [(valid_features, valid_y)])
 feature_importances += model.feature_importances_ / iterations
feature_importances = pd.DataFrame({'feature': list(X.columns),
'importance': feature_importances} ).sort_values('importance',
ascending = False)
 zero_features = list(feature_importances[feature_importances['importance'] == 0.0]['feature'])
 print('
There are %d features with 0.0 importance' % len(zero_features))
return zero_features, feature_importances
zero_features, feature_importances = identify_zero_importance_features(X, y, iterations = 1)
 print('zero_features:',zero_features)
 print('feature_importances : ', feature_importances )
gc.collect()<define_variables>
to_drop = feature_importances[feature_importances['importance'] <= pp]['feature']
X = X.drop(columns = to_drop )
EMBEDDINGS_DIMENSION = 300
embedding_matrix = np.zeros(( len(tokenizer.word_index)+ 1,EMBEDDINGS_DIMENSION))<feature_engineering>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1 )
num_words_in_embedding = 0
for word, i in tokenizer.word_index.items() :
if word in embeddings_index.vocab:
embedding_vector = embeddings_index[word]
embedding_matrix[i] = embedding_vector
num_words_in_embedding += 1<categorify>
gbm = LGBMRegressor(objective='regression',
num_leaves=40,
learning_rate=0.005,
n_estimators=20000,
max_bin=55,
bagging_fraction=0.7,
bagging_freq=9,
feature_fraction=0.7,
feature_fraction_seed=9,
bagging_seed=10,
min_data_in_leaf=7,
min_sum_hessian_in_leaf=5)
 gbm.fit(X_train, y_train,
eval_set=[(X_test, y_test)],
eval_metric='rmsle',
early_stopping_rounds=200)
 y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration_)
 print('The accuracy of the lgbm Regressor is',r2_score(y_test,y_pred))
print('RMSE is:
', mean_squared_error(y_test,y_pred))
train_text = pad_text(train_df[TEXT_COLUMN], tokenizer)
 train_labels = train_df[TOXICITY_COLUMN]
validate_text = pad_text(validate_df[TEXT_COLUMN], tokenizer)
 validate_labels = validate_df[TOXICITY_COLUMN]<set_options>
feats = test_data.drop(['Id'], axis=1)
 feats = feats[X_train.columns]
final_preds = gbm.predict(feats,num_iteration=gbm.best_iteration_ )
gc.collect()<choose_model_class>
submission = pd.DataFrame()
submission['Id'] = test_data.Id
submission['winPlacePerc'] = final_preds
submission.to_csv('submission1.csv', index=False )
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
 embedding_layer = Embedding(len(tokenizer.word_index)+ 1,
EMBEDDINGS_DIMENSION,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
 x = embedding_layer(sequence_input)
 x = SpatialDropout1D(0.2 )(x)
 x1 = Bidirectional(CuDNNLSTM(256, return_sequences=True))(x)
 x2 = Bidirectional(CuDNNGRU(128, return_sequences=True))(x1)
 max_pool1 = GlobalMaxPooling1D()(x1)
 max_pool2 = GlobalMaxPooling1D()(x2)
 x = Concatenate()([max_pool1, max_pool2])
 preds = Dense(1, activation='sigmoid' )(x)
 model = Model(sequence_input, preds)
 model.summary() 
<choose_model_class>
submission[submission['winPlacePerc'] < 0] = 0
submission[submission['winPlacePerc'] >1] = 1