Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
%matplotlib inline
%pylab inline
print(os.listdir(".. /input"))
warnings.filterwarnings(action='ignore' )
def _get_masks(tokens, max_seq_length):

 if len(tokens)> max_seq_length:
raise IndexError("Token length more than max seq length!")
 return [1] * len(tokens)+ [0] *(max_seq_length - len(tokens))
def _get_ids(tokens, tokenizer, max_seq_length):

 token_ids = tokenizer.convert_tokens_to_ids(tokens)
 input_ids = token_ids + [1] *(max_seq_length - len(token_ids))
return input_ids
def _trim_input(title, question, max_sequence_length,
t_max_len=100, head=128, tail=281, Q=True, q_max_len=239, a_max_len=239):
t = tokenizer.tokenize(title)
 q = tokenizer.tokenize(question)
 t_len = len(t)
 q_len = len(q)
 if(t_len + q_len + 4)> max_sequence_length:
if t_max_len > t_len:
t_new_len = t_len
q_head = head
q_tail = 508 - q_head - t_new_len
else:
t_new_len = t_max_len
if(t_new_len + q_len + 4)> max_sequence_length:
q_head = head
q_tail = 508 - q_head - t_new_len
else:
t = t[:t_new_len]
return t, q
t = t[:t_new_len]
q = q[:q_head] + q[-q_tail:]
return t, q
else:
return t, q
def _convert_to_bert_inputs(title, question, tokenizer, max_sequence_length):

 stoken = ["
"] + title + ["
"] + ["
"] + question + ["
"]
input_ids = _get_ids(stoken, tokenizer, max_sequence_length)
 input_masks = _get_masks(stoken, max_sequence_length)
 return [input_ids, input_masks]
def compute_input_arays(df, columns, tokenizer, max_sequence_length, Q=True):
input_ids, input_masks, input_segments = [], [], []
for _, instance in tqdm(df[columns].iterrows()):
if Q:
t, q = instance.question_title, instance.question_body
t, q = _trim_input(t, q, max_sequence_length)
 ids, masks= _convert_to_bert_inputs(t, q, tokenizer, max_sequence_length)
 input_ids.append(ids)
 input_masks.append(masks)
 else:
t, a = instance.question_title, instance.answer
t, a = _trim_input(t, a, max_sequence_length)
 ids, masks = _convert_to_bert_inputs(t, a, tokenizer, max_sequence_length)
 input_ids.append(ids)
 input_masks.append(masks)
 return [np.asarray(input_ids, dtype=np.int32),
np.asarray(input_masks, dtype=np.int32)]
def compute_output_arrays(df, columns):
return np.asarray(df[columns])
 def compute_spearmanr(trues, preds):
rhos = []
for col_trues, col_pred in zip(trues.T, preds.T):
rhos.append(
 spearmanr(col_trues, col_pred + np.random.normal(0, 1e-7, col_pred.shape[0])).correlation)
 return np.mean(rhos)
<categorify>
df_train = pd.read_csv('.. /input/train_V2.csv')
 df = reduce_mem_usage(df_train)
 df.info()
input_columns_answer = ['question_title', 'answer']
input_columns_question = ['question_title', 'question_body']
find = re.compile(r"^[^.]*")
 df_train['netloc'] = df_train['url'].apply(lambda x: re.findall(find, urlparse(x ).netloc)[0])
 df_test['netloc'] = df_test['url'].apply(lambda x: re.findall(find, urlparse(x ).netloc)[0])
 features = ['netloc', 'category']
merged_answer = pd.concat([df_train[features], df_train[features]])
 merged_question = pd.concat([df_train[features], df_train[features]])
 ohe_answer = OneHotEncoder(handle_unknown='ignore')
 ohe_question = OneHotEncoder(handle_unknown='ignore')
 ohe_answer.fit(merged_answer)
 ohe_question.fit(merged_question)
 for i in range(len(df_test)) :
if df_test.loc[i,'netloc'] not in ohe_answer.categories_[0]:
df_test.loc[i,'netloc'] = 'stackoverflow'
if df_test.loc[i,'category'] not in ohe_answer.categories_[1]:
df_test.loc[i,'category'] = 'TECHNOLOGY'
features_test_answer = ohe_answer.transform(df_test[features] ).toarray()
features_test_question = ohe_question.transform(df_test[features] ).toarray()
module_url = "/kaggle/input/universalsentenceencoderlarge4/"
embed = hub.load(module_url)
 embeddings_test = {}
for text in input_columns_answer:
print(text)
 test_text = df_test[text].str.replace('?', '.' ).str.replace('!', '.' ).tolist()
curr_test_emb = []
batch_size = 16
ind = 0
while ind * batch_size < len(test_text):
curr_test_emb.append(embed(test_text[ind * batch_size:(ind + 1)* batch_size])["outputs"].numpy())
 ind += 1
embeddings_test[text + '_embedding'] = np.vstack(curr_test_emb)
 del embed
K.clear_session()
gc.collect()
l2_dist = lambda x, y: np.power(x - y, 2 ).sum(axis=1)
 cos_dist = lambda x, y:(x*y ).sum(axis=1)
 dist_features_test_answer = np.array([
l2_dist(embeddings_test['question_title_embedding'], embeddings_test['answer_embedding']),
cos_dist(embeddings_test['question_title_embedding'], embeddings_test['answer_embedding'])
 ] ).T
feature_embeddings_answer = np.hstack([item for k, item in embeddings_test.items() ] + [features_test_answer, dist_features_test_answer])
 module_url = "/kaggle/input/universalsentenceencoderlarge4/"
embed = hub.load(module_url)
 embeddings_test = {}
for text in input_columns_question:
print(text)
 test_text = df_test[text].str.replace('?', '.' ).str.replace('!', '.' ).tolist()
curr_test_emb = []
batch_size = 16
ind = 0
while ind * batch_size < len(test_text):
curr_test_emb.append(embed(test_text[ind * batch_size:(ind + 1)* batch_size])["outputs"].numpy())
 ind += 1
embeddings_test[text + '_embedding'] = np.vstack(curr_test_emb)
 del embed
K.clear_session()
gc.collect()
l2_dist = lambda x, y: np.power(x - y, 2 ).sum(axis=1)
 cos_dist = lambda x, y:(x*y ).sum(axis=1)
 dist_features_test_question = np.array([
l2_dist(embeddings_test['question_body_embedding'], embeddings_test['question_title_embedding']),
cos_dist(embeddings_test['question_body_embedding'], embeddings_test['question_title_embedding'])
 ] ).T
feature_embeddings_question = np.hstack([item for k, item in embeddings_test.items() ] + [features_test_question, dist_features_test_question] )<init_hyperparams>
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor
from IPython.display import display
from sklearn import metrics
from scipy.cluster import hierarchy as hc
from pdpbox import pdp
from plotnine import *
from fastai.imports import *
from fastai.structured import *
class CustomCallback(tf.keras.callbacks.Callback):
def __init__(self, valid_data, test_data, batch_size=16, fold=None, Q=True):
self.valid_inputs = valid_data[0]
self.valid_outputs = valid_data[1]
self.test_inputs = test_data
self.batch_size = batch_size
self.fold = fold
self.Q = Q
def on_train_begin(self, logs={}):
self.valid_predictions = []
self.test_predictions = []
def on_epoch_end(self, epoch, logs={}):
self.valid_predictions.append(
 self.model.predict(self.valid_inputs, batch_size=self.batch_size))
rho_val = compute_spearmanr(
 self.valid_outputs, np.average(self.valid_predictions, axis=0))
print("
validation rho: %.4f" % rho_val)
 if self.fold is not None:
if epoch >= 5:
if self.Q:
self.model.save_weights(f'checkpoints_question/roberta-base-question-{fold}-{epoch}.h5py')
 else:
self.model.save_weights(f'checkpoints_answer/roberta-base-answer-{fold}-{epoch}.h5py')
 self.test_predictions.append(
 self.model.predict(self.test_inputs, batch_size=self.batch_size)
)
 def bert_model_question() :
input_word_ids = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_word_ids')
 input_masks = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_masks')
 feature_embeddings = tf.keras.layers.Input(
(feature_embeddings_question.shape[1],), dtype=tf.float32, name='feature_embeddings')
 bert_model = TFRobertaModel.from_pretrained(BERT_PATH, output_hidden_states=True)
 hidden_output = bert_model([input_word_ids, input_masks])[2]
last_cat_sequence = tf.concat(
(hidden_output[-1], hidden_output[-2], hidden_output[-3]),
2, 
)
 x = tf.keras.layers.GlobalAveragePooling1D()(last_cat_sequence)
 x = tf.concat(( x, feature_embeddings), 1)
 out = tf.keras.layers.Dense(21, activation="sigmoid", name="dense_output" )(x)
 model = tf.keras.models.Model(
 inputs=[input_word_ids, input_masks, feature_embeddings], outputs=out)
 return model
def bert_model_answer() :
input_word_ids = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_word_ids')
 input_masks = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_masks')
 feature_embeddings = tf.keras.layers.Input(
(feature_embeddings_answer.shape[1],), dtype=tf.float32, name='feature_embeddings')
 bert_model = TFRobertaModel.from_pretrained(BERT_PATH, output_hidden_states=True)
 hidden_output = bert_model([input_word_ids, input_masks])[2]
last_cat_sequence = tf.concat(
(hidden_output[-1], hidden_output[-2], hidden_output[-3]),
2, 
)
 x = tf.keras.layers.GlobalAveragePooling1D()(last_cat_sequence)
 x = tf.concat(( x, feature_embeddings), 1)
 out = tf.keras.layers.Dense(9, activation="sigmoid", name="dense_output" )(x)
 model = tf.keras.models.Model(
 inputs=[input_word_ids, input_masks, feature_embeddings], outputs=out)
 return model
def train_and_predict(model, train_data, valid_data, test_data,
learning_rate, epochs, batch_size, loss_function, fold,Q=True):
if Q:
custom_callback = CustomCallback(
 valid_data=(valid_data[0], valid_data[1]),
test_data=test_data,
batch_size=batch_size,
fold=5,
Q=True)
 else:
custom_callback = CustomCallback(
 valid_data=(valid_data[0], valid_data[1]),
test_data=test_data,
batch_size=batch_size,
fold=5,
Q=False)
 optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
 model.compile(loss=loss_function, optimizer=optimizer)
 model.fit(train_data[0], train_data[1], epochs=epochs,
batch_size=batch_size, callbacks=[custom_callback])
 return custom_callback<split>
df[df['winPlacePerc'].isnull() ]
test_inputs_question = compute_input_arays(df_test, input_categories_question, tokenizer, MAX_SEQUENCE_LENGTH)
 test_inputs_question.append(feature_embeddings_question )<split>
df.drop(2744604, inplace=True)
 df[df['winPlacePerc'].isnull() ]
test_inputs_answer = compute_input_arays(df_test, input_categories_answer, tokenizer, MAX_SEQUENCE_LENGTH,Q=False)
 test_inputs_answer.append(feature_embeddings_answer )<set_options>
train = df
gc.collect()<define_variables>
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 to_show = ['Id', 'kills','killsNorm','damageDealt', 'damageDealtNorm', 'maxPlace', 'maxPlaceNorm', 'matchDuration', 'matchDurationNorm']
train[to_show][0:11]
test_predictions_question = []
test_predictions_answer = []
for i in range(16):
if i == 0:
model_path = '.. /input/largequestion1/q1/roberta-large-features-question-0-3.h5py'
print(model_path)
 if i == 1:
model_path = '.. /input/largequestion1/q1/roberta-large-features-question-1-4.h5py'
print(model_path)
 if i == 2:
model_path = '.. /input/largequestion1/q1/roberta-large-features-question-2-4.h5py'
print(model_path)
 if i == 3:
model_path = '.. /input/largequestion2/q2/q2/roberta-large-features-question-3-3.h5py'
print(model_path)
 if i == 4:
model_path = '.. /input/largequestion2/q2/q2/roberta-large-features-question-4-4.h5py'
print(model_path)
 if i == 5:
model_path = '.. /input/largequestion2/q2/q2/roberta-large-features-question-5-4.h5py'
print(model_path)
 if i == 6:
model_path = '.. /input/largequestion3/q3/roberta-large-features-question-6-5.h5py'
print(model_path)
 if i == 7:
model_path = '.. /input/largequestion3/q3/roberta-large-features-question-7-3.h5py'
print(model_path)
 if i == 8:
model_path = '.. /input/largeanswer2/11/roberta-large-features-answer-2-3.h5py'
print(model_path)
 if i == 9:
model_path = '.. /input/largeanswer1/22/22/roberta-large-features-answer-0-4.h5py'
print(model_path)
 if i == 10:
model_path = '.. /input/largeanswer1/22/22/roberta-large-features-answer-1-3.h5py'
print(model_path)
 if i == 11:
model_path = '.. /input/largeanswer1/22/22/roberta-large-features-answer-6-3.h5py'
print(model_path)
 if i == 12:
model_path = '.. /input/largeanswer1/33/33/roberta-large-features-answer-7-2.h5py'
print(model_path)
 if i == 13:
model_path = '.. /input/largeanswer1/33/33/roberta-large-features-answer-5-2.h5py'
print(model_path)
 if i == 14:
model_path = '.. /input/largeanswer2/11/roberta-large-features-answer-4-5.h5py'
print(model_path)
 if i == 15:
model_path = '.. /input/largeanswer2/11/roberta-large-features-answer-3-3.h5py'
print(model_path)
 if i<8:
K.clear_session()
model = bert_model_question()
print('载入')
 model.load_weights(model_path)
 print("加载")
 test_predictions_question.append(model.predict(test_inputs_question, batch_size=30))
print(i)
 else:
K.clear_session()
model = bert_model_answer()
print('载入')
 model.load_weights(model_path)
 print('载入')
 test_predictions_answer.append(model.predict(test_inputs_answer, batch_size=30))
print(i)
<prepare_output>
train['healsandboosts'] = train['heals'] + train['boosts']
train[['heals', 'boosts', 'healsandboosts']].tail()
final_predictions_question = np.mean(test_predictions_question, axis=0)
 final_predictions_answer = np.mean(test_predictions_answer, axis=0 )<set_options>
train['totalDistance'] = train['rideDistance'] + train['walkDistance'] + train['swimDistance']
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
gc.collect()<define_variables>
train['headshot_rate'] = train['headshotKills'] / train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0 )
BERT_PATH = '.. /input/robertabase/'
tokenizer = RobertaTokenizer(BERT_PATH + "roberta-base-vocab.json", BERT_PATH + "roberta-base-merges.txt" )<define_variables>
train.drop(train[train['killsWithoutMoving'] == True].index, inplace=True )
test_predictions_question = []
test_predictions_answer = []
for i in range(16):
if i == 0:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-0-4.h5py'
print(model_path)
 if i == 1:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-1-4.h5py'
print(model_path)
 if i == 2:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-2-6.h5py'
print(model_path)
 if i == 3:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-3-3.h5py'
print(model_path)
 if i == 4:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-4-4.h5py'
print(model_path)
 if i == 5:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-5-4.h5py'
print(model_path)
 if i ==6:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-6-4.h5py'
print(model_path)
 if i ==7:
model_path = '/kaggle/input/basequestion/rfbq11/roberta-base-features-question-7-3.h5py'
print(model_path)
 if i == 8:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-0-4.h5py'
print(model_path)
 if i == 9:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-1-2.h5py'
print(model_path)
 if i == 10:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-2-4.h5py'
print(model_path)
 if i == 11:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-3-4.h5py'
print(model_path)
 if i == 12:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-4-3.h5py'
print(model_path)
 if i == 13:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-5-3.h5py'
print(model_path)
 if i == 14:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-6-4.h5py'
print(model_path)
 if i == 15:
model_path = '/kaggle/input/baseanswer/best11/roberta-base-features-answer-7-3.h5py'
print(model_path)
 if i<8:
K.clear_session()
model = bert_model_question()
print('载入')
 model.load_weights(model_path)
 print("加载")
 test_predictions_question.append(model.predict(test_inputs_question, batch_size=60))
print(i)
 else:
K.clear_session()
model = bert_model_answer()
print('载入')
 model.load_weights(model_path)
 print('载入')
 test_predictions_answer.append(model.predict(test_inputs_answer, batch_size=60))
print(i )<prepare_output>
train[train['roadKills'] > 10]
final_predictions_question_bert = np.mean(test_predictions_question, axis=0)
 final_predictions_answer_bert = np.mean(test_predictions_answer, axis=0 )<prepare_output>
train.drop(train[train['roadKills'] > 10].index, inplace=True )
final_predictions_q = 0.5*final_predictions_question + 0.5*final_predictions_question_bert
final_predictions_a = 0.5*final_predictions_answer + 0.5*final_predictions_answer_bert<feature_engineering>
display(train[train['kills'] > 30].shape)
 train[train['kills'] > 30]
df_sub.iloc[:, 1:20] = np.around(final_predictions_q[:,:19] *99)/ 99
df_sub.iloc[:, 21] = np.around(final_predictions_q[:,20] *99)/ 99
df_sub.iloc[:, 20] = np.around(final_predictions_q[:,19] *9000)/ 9000
df_sub.iloc[:, 22:] = np.around(final_predictions_a * 99)/ 99
pd.set_option('display.max_columns', None)
 df_sub.head(40 )<define_variables>
train.drop(train[train['kills'] > 30].index, inplace=True )
submission = df_sub<define_variables>
train.drop(train[train['longestKill'] >= 1000].index, inplace=True )
n = df_test['url'].apply(lambda x:(( 'ell.stackexchange.com' in x)or('english.stackexchange.com' in x)) ).tolist()
spelling=[]
for x in n:
if x:
spelling.append(0.5)
 else:
spelling.append(0.) 
<feature_engineering>
train.drop(train[train['walkDistance'] >= 10000].index, inplace=True )
submission.loc[submission['question_asker_intent_understanding'] >= 0.91, ['question_asker_intent_understanding']] = 1.0
submission.loc[(submission['question_asker_intent_understanding'] >=0.88)&(submission['question_asker_intent_understanding'] <0.91),
['question_asker_intent_understanding']] = 8/9
submission.loc[submission['question_body_critical'] <0.43, ['question_body_critical']] = 1/3
submission.loc[submission['question_body_critical'] >0.84, ['question_body_critical']] = 1.0
submission.loc[submission['question_conversational']<= 0.06 ,['question_conversational']] = 0.0
submission.loc[submission['question_expect_short_answer'] >0.72 ,['question_expect_short_answer']] = 1.0
submission.loc[submission['question_expect_short_answer'] <0.4, ['question_expect_short_answer']] = 0.0
submission.loc[(submission['question_expect_short_answer'] >=0.63)&(submission['question_expect_short_answer'] <0.69),
['question_expect_short_answer']] = 2/3
submission.loc[submission['question_fact_seeking']>0.81 ,['question_fact_seeking']] = 1.0
submission.loc[submission['question_has_commonly_accepted_answer']>0.83 ,['question_has_commonly_accepted_answer']] = 1.0
submission.loc[submission['question_has_commonly_accepted_answer'] <0.3, ['question_has_commonly_accepted_answer']] = 0.0
submission.loc[submission['question_interestingness_others']>0.76, ['question_interestingness_others']] = 1.0
submission.loc[submission['question_multi_intent'] >0.8, ['question_multi_intent']] = 1.0
submission.loc[submission['question_multi_intent'] <0.2, ['question_multi_intent']] = 0.0
submission.loc[submission['question_not_really_a_question'] <0.040, ['question_not_really_a_question']] =0.0
submission.loc[submission['question_not_really_a_question'] >=0.040, ['question_not_really_a_question']] =1/3
submission.loc[submission['question_opinion_seeking']<0.25, ['question_opinion_seeking']] = 0.0
submission.loc[(submission['question_opinion_seeking'] >=0.3)&(submission['question_opinion_seeking'] <0.36),
['question_opinion_seeking']] = 1/3
submission.loc[submission['question_opinion_seeking']>0.7, ['question_opinion_seeking']] = 1.0
submission.loc[submission['question_type_choice'] <0.25, ['question_type_choice']] = 0.0
submission.loc[submission['question_type_choice'] >0.86, ['question_type_choice']] = 1.0
submission.loc[submission['question_type_compare'] <0.2, ['question_type_compare']] =0.0
submission.loc[submission['question_type_consequence'] <0.3, ['question_type_consequence']] = 0.0
submission.loc[submission['question_type_definition']<0.2, ['question_type_definition']] = 0.0
submission.loc[submission['question_type_entity'] <0.188, ['question_type_entity']] =0.0
submission.loc[submission['question_type_instructions'] <0.33, ['question_type_instructions']] =0.0
submission.loc[submission['question_type_instructions'] >0.88, ['question_type_instructions']] = 1.0
submission.loc[submission['question_type_procedure'] <0.21, ['question_type_procedure']] =0.0
submission['question_type_spelling'] = spelling
submission.loc[submission['question_type_reason_explanation'] <0.16, ['question_type_reason_explanation']] =0.0
submission.loc[submission['question_type_reason_explanation'] >0.82, ['question_type_reason_explanation']] = 1.0
submission.loc[submission['question_well_written']>0.9, ['question_well_written']] = 1.0
submission.loc[submission['answer_helpful']>0.95, ['answer_helpful']] = 1.0
submission.loc[(submission['answer_level_of_information'] >=0.63)&(submission['answer_level_of_information'] <0.80),
['answer_level_of_information']] = 2/3
submission.loc[submission['answer_level_of_information']>0.80, ['answer_level_of_information']] = 1
submission.loc[submission['answer_plausible'] >0.96, ['answer_plausible']] = 1.0
submission.loc[submission['answer_relevance'] >0.97, ['answer_relevance']] = 1.0
submission.loc[submission['answer_satisfaction'] >0.92, ['answer_satisfaction']] = 1.0
submission.loc[submission['answer_type_instructions'] < 0.3, ['answer_type_instructions']] = 0
submission.loc[submission['answer_type_instructions'] > 0.85, ['answer_type_instructions']] = 1
submission.loc[submission['answer_type_procedure'] < 0.18, ['answer_type_procedure']] = 0.0
submission.loc[submission['answer_type_reason_explanation'] < 0.18, ['answer_type_reason_explanation']] = 0.0
submission.loc[submission['answer_type_reason_explanation'] >0.88, ['answer_type_reason_explanation']] = 1.0
submission.loc[submission['answer_well_written'] >0.93, ['answer_well_written']] = 1.0
submission.loc[(submission['answer_well_written'] >=0.87)&(submission['answer_well_written']<0.91),
['answer_well_written']]=8/9<save_to_csv>
train[train['rideDistance'] >= 20000]
submission.to_csv('submission.csv', index=False )<save_to_csv>
train.drop(train[train['rideDistance'] >= 20000].index, inplace=True )
submission.to_csv('submission.csv', index=False )<load_from_csv>
train[train['swimDistance'] >= 2000]
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 sample_submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv' )<set_options>
train.drop(train[train['swimDistance'] >= 2000].index, inplace=True )
pd.set_option('display.max_columns', 100)
 train.head()<load_from_csv>
train[train['weaponsAcquired'] >= 80]
f = open('.. /input/house-prices-advanced-regression-techniques/data_description.txt','r')
 message = f.read()
print(message )<create_dataframe>
train.drop(train[train['weaponsAcquired'] >= 80].index, inplace=True )
NA_col = pd.DataFrame(train.isna().sum() , columns = ['NA_Count'])
 NA_col['% of NA'] =(NA_col.NA_Count/len(train)) *100
NA_col.sort_values(by = ['% of NA'], ascending = False, na_position = 'first')
<prepare_output>
train.drop(train[train['heals'] >= 40].index, inplace=True )
pd.set_option('display.max_rows', 500)
 NA_row = pd.DataFrame(train.isnull().sum(axis = 1), columns = ['NA_Row_Count'])
 NA_row['% of NA'] =(NA_row.NA_Row_Count/len(train.columns)) *100
NA_row.sort_values(by = ['% of NA'], ascending = False, na_position = 'first')
<create_dataframe>
train = train.drop(columns = ['matchType', 'Id', 'groupId', 'matchId'] )
NA_col = pd.DataFrame(test.isna().sum() , columns = ['NA_Count'])
 NA_col['% of NA'] =(NA_col.NA_Count/len(test)) *100
NA_col.sort_values(by = ['% of NA'], ascending = False, na_position = 'first')
<prepare_output>
df = df_sample.drop(columns = ['winPlacePerc'])
 y = df_sample['winPlacePerc']
pd.set_option('display.max_rows', 500)
 NA_row = pd.DataFrame(test.isnull().sum(axis = 1), columns = ['NA_Row_Count'])
 NA_row['% of NA'] =(NA_row.NA_Row_Count/len(test.columns)) *100
NA_row.sort_values(by = ['% of NA'], ascending = False, na_position = 'first')
<drop_column>
def split_vals(a, n : int):
return a[:n].copy() , a[n:].copy()
val_perc = 0.12
n_valid = int(val_perc * sample)
 n_trn = len(df)-n_valid
raw_train, raw_valid = split_vals(df_sample, n_trn)
 X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )
train_data = train.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence'], axis=1)
 test_data = test.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence'], axis=1 )<drop_column>
def print_score(m : RandomForestRegressor):
res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train),
'mae val: ', mean_absolute_error(m.predict(X_valid), y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
train_wo_target = train_data.drop(['Id', 'SalePrice'], axis=1 )<define_variables>
m1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m1.fit(X_train, y_train)
 print_score(m1 )
cols = train_wo_target.columns
num_cols = train_wo_target._get_numeric_data().columns
cat_cols = list(set(cols)- set(num_cols))<prepare_x_and_y>
fi = rf_feat_importance(m1, df); fi[:10]
X_train = train_data.copy().drop('SalePrice', axis = 1)
 y_train = train_data[['Id','SalePrice']]<import_modules>
to_keep = fi[fi.imp>0.005].cols
print('Significant features: ', len(to_keep))
to_keep
from sklearn.preprocessing import Imputer
from sklearn.impute import SimpleImputer<prepare_x_and_y>
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn )
mean_imputer = Imputer(missing_values="NaN", strategy="mean", axis=0)
 mean_imputer.fit(X_train[num_cols])
 X_train[num_cols] = pd.DataFrame(mean_imputer.fit_transform(X_train[num_cols]))
test_data[num_cols] = pd.DataFrame(mean_imputer.fit_transform(test_data[num_cols]))<categorify>
m2 = RandomForestRegressor(n_estimators=80, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m2.fit(X_train, y_train)
 print_score(m2 )
train_objs_num = len(X_train)
 dataset = pd.concat(objs=[X_train, test_data], axis=0)
 dataset_preprocessed = pd.get_dummies(dataset)
 X_train = dataset_preprocessed[:train_objs_num]
test_data = dataset_preprocessed[train_objs_num:]<count_missing_values>
val_perc_full = 0.12
n_valid_full = int(val_perc_full * len(train))
n_trn_full = len(train)-n_valid_full
df_full = train.drop(columns = ['winPlacePerc'])
 y = train['winPlacePerc']
df_full = df_full[to_keep]
X_train, X_valid = split_vals(df_full, n_trn_full)
 y_train, y_valid = split_vals(y, n_trn_full)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )
X_train.isna().sum()<count_missing_values>
m3 = RandomForestRegressor(n_estimators=60, min_samples_leaf=3, max_features=0.5, n_jobs=-1)
 m3.fit(X_train, y_train)
 print_score(m3 )
test_data.isna().sum()<prepare_x_and_y>
test = pd.read_csv('.. /input/test_V2.csv' )
X_train.set_index('Id', inplace = True)
 y_train.set_index('Id', inplace = True)
 test_data.set_index('Id', inplace = True )<drop_column>
test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 test['totalDistance'] = test['rideDistance'] + test['walkDistance'] + test['swimDistance']
test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['maxPlaceNorm'] = test['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 test['matchDurationNorm'] = test['matchDuration']*(( 100-test['playersJoined'])/100 + 1)
 test['healsandboosts'] = test['heals'] + test['boosts']
test['killsWithoutMoving'] =(( test['kills'] > 0)&(test['totalDistance'] == 0))
test_pred = test[to_keep].copy()
test_pred.fillna(0, inplace=True)
 test_pred.head()
%%time
X_train = reduce_mem_usage(X_train)
 y_train = reduce_mem_usage(y_train)
 test_data = reduce_mem_usage(test_data )<import_modules>
predictions = np.clip(a = m3.predict(test_pred), a_min = 0.0, a_max = 1.0)
 pred_df = pd.DataFrame({'Id' : test['Id'], 'winPlacePerc' : predictions})
 pred_df.to_csv("submission.csv", index=False )