Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<feature_engineering>
def get_null_observations(dataframe, column):
return dataframe[pd.isnull(dataframe[column])]
def delete_null_observations(dataframe, column):
fixed_df = dataframe.drop(get_null_observations(dataframe,column ).index)
 return fixed_df
def get_missing_data_table(dataframe):
total = dataframe.isnull().sum()
percentage = dataframe.isnull().sum() / dataframe.isnull().count()
missing_data = pd.concat([total, percentage], axis='columns', keys=['TOTAL','PERCENTAGE'])
 return missing_data.sort_index(ascending=True)
 df = pd.read_csv('.. /input/train_V2.csv')
 df.head()
df['comment_text'] = df['comment_text'].apply(lambda x: x.lower())
 gc.collect()<compute_train_metric>
get_missing_data_table(df )
vocab = build_vocab(df['comment_text'])
 oov = check_coverage(vocab, embeddings_index)
 oov[:10]<set_options>
df = delete_null_observations(dataframe=df, column='winPlacePerc')
 get_missing_data_table(df )
gc.collect()<define_variables>
df_team_dict =(df.groupby('groupId', as_index = True)
.agg({'Id':'count', 'kills':'sum'})
.rename(columns={'Id':'teamSize', 'kills':'teamKills'})).to_dict()
teamKills = []
teamSize = []
for teamId in df['groupId']:
teamKills.append(df_team_dict['teamKills'][teamId])
 teamSize.append(df_team_dict['teamSize'][teamId])
 df['teamKills'] = teamKills
df['teamSize'] = teamSize
df.head()
contraction_mapping = {"ain't": "is not", "aren't": "are not","can't": "cannot", "'cause": "because", "could've": "could have", "couldn't": "could not", "didn't": "did not", "doesn't": "does not", "don't": "do not", "hadn't": "had not", "hasn't": "has not", "haven't": "have not", "he'd": "he would","he'll": "he will", "he's": "he is", "how'd": "how did", "how'd'y": "how do you", "how'll": "how will", "how's": "how is", "I'd": "I would", "I'd've": "I would have", "I'll": "I will", "I'll've": "I will have","I'm": "I am", "I've": "I have", "i'd": "i would", "i'd've": "i would have", "i'll": "i will", "i'll've": "i will have","i'm": "i am", "i've": "i have", "isn't": "is not", "it'd": "it would", "it'd've": "it would have", "it'll": "it will", "it'll've": "it will have","it's": "it is", "let's": "let us", "ma'am": "madam", "mayn't": "may not", "might've": "might have","mightn't": "might not","mightn't've": "might not have", "must've": "must have", "mustn't": "must not", "mustn't've": "must not have", "needn't": "need not", "needn't've": "need not have","o'clock": "of the clock", "oughtn't": "ought not", "oughtn't've": "ought not have", "shan't": "shall not", "sha'n't": "shall not", "shan't've": "shall not have", "she'd": "she would", "she'd've": "she would have", "she'll": "she will", "she'll've": "she will have", "she's": "she is", "should've": "should have", "shouldn't": "should not", "shouldn't've": "should not have", "so've": "so have","so's": "so as", "this's": "this is","that'd": "that would", "that'd've": "that would have", "that's": "that is", "there'd": "there would", "there'd've": "there would have", "there's": "there is", "here's": "here is","they'd": "they would", "they'd've": "they would have", "they'll": "they will", "they'll've": "they will have", "they're": "they are", "they've": "they have", "to've": "to have", "wasn't": "was not", "we'd": "we would", "we'd've": "we would have", "we'll": "we will", "we'll've": "we will have", "we're": "we are", "we've": "we have", "weren't": "were not", "what'll": "what will", "what'll've": "what will have", "what're": "what are", "what's": "what is", "what've": "what have", "when's": "when is", "when've": "when have", "where'd": "where did", "where's": "where is", "where've": "where have", "who'll": "who will", "who'll've": "who will have", "who's": "who is", "who've": "who have", "why's": "why is", "why've": "why have", "will've": "will have", "won't": "will not", "won't've": "will not have", "would've": "would have", "wouldn't": "would not", "wouldn't've": "would not have", "y'all": "you all", "y'all'd": "you all would","y'all'd've": "you all would have","y'all're": "you all are","y'all've": "you all have","you'd": "you would", "you'd've": "you would have", "you'll": "you will", "you'll've": "you will have", "you're": "you are", "you've": "you have" }<set_options>
df_team =(df.groupby('groupId', as_index = False)
.agg({'Id':'count', 'matchId':lambda x: x.unique() [0], 'kills':'sum'})
.rename(columns={'Id':'teamSize', 'kills':'teamKills'})).reset_index()
df_match =(df_team.groupby('matchId', as_index = True)
.agg({'teamSize':'sum', 'teamKills':'sum'})
.rename(columns={'teamSize':'matchSize', 'teamKills':'matchKills'})).to_dict()
matchSize = []
matchKills = []
for matchId in df['matchId']:
matchSize.append(df_match['matchSize'][matchId])
 matchKills.append(df_match['matchKills'][matchId])
 df['matchSize'] = matchSize
df['matchKills'] = matchKills
df.head()
del(vocab,oov)
 gc.collect()<define_variables>
df.drop(['Id'], axis='columns', inplace=True)
 df.drop(['groupId'], axis='columns', inplace=True)
 df.drop(['matchId'], axis='columns', inplace=True)
 df.head()
def known_contractions(embed):
known = []
for contract in contraction_mapping:
if contract in embed:
known.append(contract)
 return known<string_transform>
previous_record_size = df.shape[0]
h_spread = df['matchDuration'].quantile (.75)- df['matchDuration'].quantile (.25)
 limit = df['matchDuration'].quantile (.25)- 2 * h_spread
df.drop(df[df['matchDuration'] < limit].index, inplace=True)
 new_record_size = df.shape[0]
print('Total records deleted: {}({:.7%} of previous record size)'.format(previous_record_size - new_record_size, 1 - new_record_size / previous_record_size))
def clean_contractions(text, mapping):
specials = ["’", "‘", "´", "`"]
for s in specials:
text = text.replace(s, "'")
 text = ' '.join([mapping[t] if t in mapping else t for t in text.split(" ")])
 return text<drop_column>
previous_record_size = df.shape[0]
df.drop(df.query('rideDistance == 0 and roadKills > 0' ).index, inplace=True)
 new_record_size = df.shape[0]
print('Total records deleted: {}({:.7%} of previous record size)'.format(previous_record_size - new_record_size, 1 - new_record_size / previous_record_size))
df['comment_text'] = df['comment_text'].apply(lambda x: clean_contractions(x, contraction_mapping))<compute_train_metric>
encoder = preprocessing.LabelEncoder()
df['matchType'] = encoder.fit_transform(df['matchType'])
 df.head()
vocab = build_vocab(df['comment_text'])
 oov = check_coverage(vocab, embeddings_index)
 oov[:10]<set_options>
y = df['winPlacePerc'].values
X = df.drop(['winPlacePerc'], axis='columns' ).values
del(vocab,oov)
 gc.collect()<drop_column>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
 lgb_train = lgb.Dataset(X_train, y_train, categorical_feature=[12])
 lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
 params = {
"objective" : "regression",
"metric" : "mae",
"n_estimators":15000,
"early_stopping_rounds":100,
"num_leaves" : 31,
"learning_rate" : 0.05,
"bagging_fraction" : 0.9,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.7
}
model = lgb.train(params,
lgb_train,
num_boost_round=20,
valid_sets=lgb_eval,
early_stopping_rounds=5,
verbose_eval=1000 )
def unknown_punct(embed, punct):
unknown = ''
for p in punct:
if p not in embed:
unknown += p
unknown += ' '
return unknown<define_variables>
df_test = pd.read_csv('.. /input/test_V2.csv')
 df_test['matchType'] = encoder.transform(df_test['matchType'])
 df_test_team_dict =(df_test.groupby('groupId', as_index = True)
.agg({'Id':'count', 'kills':'sum'})
.rename(columns={'Id':'teamSize', 'kills':'teamKills'})).to_dict()
teamKills_test = []
teamSize_test = []
for teamId in df_test['groupId']:
teamKills_test.append(df_test_team_dict['teamKills'][teamId])
 teamSize_test.append(df_test_team_dict['teamSize'][teamId])
 df_test['teamKills'] = teamKills_test
df_test['teamSize'] = teamSize_test
df_team_test =(df_test.groupby('groupId', as_index = False)
.agg({'Id':'count', 'matchId':lambda x: x.unique() [0], 'kills':'sum'})
.rename(columns={'Id':'teamSize', 'kills':'teamKills'})).reset_index()
df_match_test =(df_team_test.groupby('matchId', as_index = True)
.agg({'teamSize':'sum', 'teamKills':'sum'})
.rename(columns={'teamSize':'matchSize', 'teamKills':'matchKills'})).to_dict()
matchSize_test = []
matchKills_test = []
for matchId in df_test['matchId']:
matchSize_test.append(df_match_test['matchSize'][matchId])
 matchKills_test.append(df_match_test['matchKills'][matchId])
 df_test['matchSize'] = matchSize_test
df_test['matchKills'] = matchKills_test
X_testdata = df_test.drop(['Id','groupId','matchId'], axis='columns' ).values
df_test['winPlacePerc'] = model.predict(X_testdata, num_iteration=model.best_iteration)
 submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False)
 print('Done!' )