Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<train_model>
warnings.simplefilter('ignore' )
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=BATCH_SIZE),
epochs=EPOCHS, validation_data=(x_val, y_val),
verbose=2, steps_per_epoch=x_train.shape[0] // BATCH_SIZE )<predict_on_test>
test_set = pd.read_csv(".. /input/test_V2.csv")
 train = pd.read_csv(".. /input/train_V2.csv" )
predictions = model.predict_classes(test )<save_to_csv>
train.dropna(inplace=True )
submission = pd.DataFrame({"ImageId": list(range(1, len(predictions)+ 1)) , "Label": predictions})
 submission.to_csv(SUBMISSION_NAME, index=False)
 submission.head(10 )<set_options>
print(f'Nan values: {train.isnull().values.any() }' )
%matplotlib inline
warnings.filterwarnings("ignore" )<define_variables>
feature_list = ['assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill',
'rankPoints', 'revives', 'rideDistance', 'roadKills',
'swimDistance', 'teamKills', 'vehicleDestroys', 'walkDistance',
'weaponsAcquired', 'winPoints']
path = ".. /input/titanic/"<define_variables>
for x in feature_list:
train[f'match_{x}'] = train.groupby('matchId')[x].transform(sum)
 test_set[f'match_{x}'] = test_set.groupby('matchId')[x].transform(sum )
cv_n_split = 2
random_state = 0
test_train_split_part = 0.15<define_variables>
for x in feature_list:
train[f'{x}_rate'] =(train[x] / train[f'match_{x}'] ).replace([np.inf, -np.inf, np.nan], 0)
 test_set[f'{x}_rate'] =(test_set[x] / test_set[f'match_{x}'] ).replace([np.inf, -np.inf, np.nan], 0 )
metrics_all = {1 : 'r2_score', 2: 'acc', 3 : 'rmse', 4 : 're'}
metrics_now = [1, 2, 3, 4]<load_from_csv>
print(f'Nan values: {train.isnull().values.any() }' )
traindf = pd.read_csv(path + 'train.csv' ).set_index('PassengerId')
 testdf = pd.read_csv(path + 'test.csv' ).set_index('PassengerId')
 submission = pd.read_csv(path + 'gender_submission.csv' )<define_variables>
y = train['winPlacePerc']
X = train.drop(['winPlacePerc','matchType','Id','groupId','matchId'],axis=1)
 test_pred = test_set.drop(['matchType','Id','groupId','matchId'],axis=1 )
target_name = 'Survived'<feature_engineering>
match = pd.get_dummies(train['matchType'],drop_first=False)
 tmatch = pd.get_dummies(test_set['matchType'],drop_first=False )
df = pd.concat([traindf, testdf], axis=0, sort=False)
 df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['IsWomanOrBoy'] =(( df.Title == 'Master')|(df.Sex == 'female'))
df['LastName'] = df.Name.str.split(',' ).str[0]
family = df.groupby(df.LastName ).Survived
df['WomanOrBoyCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).count())
 df['WomanOrBoyCount'] = df.mask(df.IsWomanOrBoy, df.WomanOrBoyCount - 1, axis=0)
 df['FamilySurvivedCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).sum())
 df['FamilySurvivedCount'] = df.mask(df.IsWomanOrBoy, df.FamilySurvivedCount - \
df.Survived.fillna(0), axis=0)
 df['WomanOrBoySurvived'] = df.FamilySurvivedCount / df.WomanOrBoyCount.replace(0, np.nan)
 df.WomanOrBoyCount = df.WomanOrBoyCount.replace(np.nan, 0)
 df['Alone'] =(df.WomanOrBoyCount == 0 )<feature_engineering>
keep_list = ['squad-fpp','squad','solo-fpp','solo']
X = pd.concat([X,match[keep_list]],axis=1)
 test_pred = pd.concat([test_pred,tmatch[keep_list]],axis=1 )
df['Title'] = df['Title'].replace('Ms','Miss')
 df['Title'] = df['Title'].replace('Mlle','Miss')
 df['Title'] = df['Title'].replace('Mme','Mrs')
 df['Embarked'] = df['Embarked'].fillna('S')
 med_fare = df.groupby(['Pclass', 'Parch', 'SibSp'] ).Fare.median() [3][0][0]
df['Fare'] = df['Fare'].fillna(med_fare)
 df['famous_cabin'] = df["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 df['Deck'] = df['Cabin'].apply(lambda s: s[0] if pd.notnull(s)else 'M')
 df.loc[(df['Deck'] == 'T'), 'Deck'] = 'A'
df['Family_Size'] = df['SibSp'] + df['Parch'] + 1
df['Name_length'] = df['Name'].apply(len)
 df.WomanOrBoySurvived = df.WomanOrBoySurvived.fillna(0)
 df.WomanOrBoyCount = df.WomanOrBoyCount.fillna(0)
 df.FamilySurvivedCount = df.FamilySurvivedCount.fillna(0)
 df.Alone = df.Alone.fillna(0 )<drop_column>
test_id = test_set['Id']
cols_to_drop = ['Name','Ticket','Cabin']
df = df.drop(cols_to_drop, axis=1 )<prepare_x_and_y>
del test_set
del train
del match
del tmatch
del feature_list
del keep_list
Y = df.Survived.loc[traindf.index].astype(int)
 X_train, X_test = df.loc[traindf.index], df.loc[testdf.index]
X_test = X_test.drop(['Survived'], axis = 1 )<count_missing_values>
print(X_train.isnull().sum())
<define_variables>
numerics = ['int8', 'int16', 'int32', 'int64', 'float16', 'float32', 'float64']
categorical_columns = []
features = X_train.columns.values.tolist()
for col in features:
if X_train[col].dtype in numerics: continue
categorical_columns.append(col)
 categorical_columns<categorify>
import lightgbm as lgb
for col in categorical_columns:
if col in X_train.columns:
le = LabelEncoder()
le.fit(list(X_train[col].astype(str ).values)+ list(X_test[col].astype(str ).values))
X_train[col] = le.transform(list(X_train[col].astype(str ).values))
X_test[col] = le.transform(list(X_test[col].astype(str ).values))<data_type_conversions>
my_model = lgb.LGBMRegressor(n_estimators = 10000,
objective= 'xentropy',
metric='l1',
learning_rate = 0.05,
bagging_fraction = 0.9,
colsample_bytree = 0.8,
num_leaves=31 )
X_train = X_train.reset_index()
X_test = X_test.reset_index()
X_dropna_categor = X_train.dropna().astype(int)
 Xtest_dropna_categor = X_test.dropna().astype(int )<filter>
my_model.fit(X, y,
eval_set=[(X, y)],
eval_metric= 'l1',
early_stopping_rounds=15,
)
Sex_female_Survived = X_dropna_categor.loc[(X_dropna_categor.Sex == 0)&(X_dropna_categor.Survived == 1)]
Sex_female_NoSurvived = X_dropna_categor.loc[(X_dropna_categor.Sex == 0)&(X_dropna_categor.Survived == 0)]
X_Sex_male_Survived = X_dropna_categor.loc[(X_dropna_categor.Sex == 1)&(X_dropna_categor.Survived == 1)]
X_Sex_male_NoSurvived = X_dropna_categor.loc[(X_dropna_categor.Sex == 1)&(X_dropna_categor.Survived == 0)]
X_test_male = Xtest_dropna_categor.loc[Xtest_dropna_categor.Sex == 1]
X_test_female = Xtest_dropna_categor.loc[Xtest_dropna_categor.Sex == 0]
female_Survived_mean, female_NoSurvived_mean = Sex_female_Survived['Age'].mean() , Sex_female_NoSurvived['Age'].mean()
male_Survived_mean, male_NoSurvived_mean = X_Sex_male_Survived['Age'].mean() , X_Sex_male_NoSurvived['Age'].mean()
female_Survived_std, female_NoSurvived_std = Sex_female_Survived['Age'].std() , Sex_female_NoSurvived['Age'].std()
male_Survived_std, male_NoSurvived_std = X_Sex_male_Survived['Age'].std() , X_Sex_male_NoSurvived['Age'].std()
female_std, female_mean = X_test_female['Age'].std() , X_test_female['Age'].mean()
male_std, male_mean = X_test_male['Age'].std() , X_test_male['Age'].mean()
X_train['Survived'] = X_train['Survived'].astype(int )<compute_test_metric>
def derf(sample, mean, std):
age_shape = sample['Age'].shape[0]
if age_shape > 0:
standard_error_ofthe_mean = std / math.sqrt(age_shape)
 random_mean = round(random.uniform(mean-(1.96*standard_error_ofthe_mean), mean+(1.96*standard_error_ofthe_mean)) , 2)
 else: random_mean = 0
return random_mean<feature_engineering>
test_pred = my_model.predict(test_pred )
for i in X_train.loc[(X_train['Sex']==0)&(X_train['Survived']==1)&(X_train['Age'].isnull())].index:
X_train.at[i, 'Age'] = derf(Sex_female_Survived, female_Survived_mean, female_Survived_std)
 for h in X_train.loc[(X_train['Sex']==0)&(X_train['Survived']==0)&(X_train['Age'].isnull())].index:
X_train.at[h, 'Age'] = derf(Sex_female_NoSurvived, female_NoSurvived_mean, female_NoSurvived_std)
 for l in X_train.loc[(X_train['Sex']==1)&(X_train['Survived']==1)&(X_train['Age'].isnull())].index:
X_train.at[l, 'Age'] = derf(X_Sex_male_Survived, male_Survived_mean, male_Survived_std)
 for b in X_train.loc[(X_train['Sex']==1)&(X_train['Survived']==0)&(X_train['Age'].isnull())].index:
X_train.at[b, 'Age'] = derf(X_Sex_male_NoSurvived, male_NoSurvived_mean, male_NoSurvived_std)
 for p in X_test.loc[(X_test['Sex']==1)&(X_test['Age'].isnull())].index:
X_test.at[p, 'Age'] = derf(X_test_male, male_mean, male_std)
 for y in X_test.loc[(X_test['Sex']==0)&(X_test['Age'].isnull())].index:
X_test.at[y, 'Age'] = derf(X_test_female, female_mean, female_std )<drop_column>
test_pred[test_pred > 1] = 1
test_pred[test_pred < 0] = 0
X_train = X_train.drop(['Survived'], axis = 1 )<count_missing_values>
df = pd.DataFrame(test_pred)
 df.columns = ['winPlacePerc']
print(X_train.isnull().sum())
 print(X_test.isnull().sum() )<data_type_conversions>
df = pd.concat([test_id,df],axis=1 )
<define_variables><EOS> 
df.to_csv('xen_retry_V8.csv', index=False )