Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<predict_on_test>
import pandas as pd
from sklearn.preprocessing import OneHotEncoder,LabelEncoder
from sklearn.model_selection import train_test_split
preds = model.predict(test_x_final, batch_size=128 )<prepare_output>
train=pd.read_csv(".. /input/train_V2.csv")
results = np.argmax(preds, axis=-1)
 results.shape<save_to_csv>
test=pd.read_csv(".. /input/test_V2.csv" )
sub = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))
sub.head()
df = pd.DataFrame({'ImageId': sub['ImageId'], 'Label': results})
 df.to_csv('submission.csv', index=False)
 os.listdir('./' )<set_options>
le=LabelEncoder()
enc=OneHotEncoder()
train.loc[(train.matchType!='solo')&(train.matchType!='duo')&(train.matchType!='squad')&(train.matchType!='solo-fpp')&(train.matchType!='duo-fpp')&(train.matchType!='squad-fpp'),'matchType']='other'
train['matchType']=train['matchType'].map({'solo':0 , 'duo':1, 'squad':2, 'solo-fpp':3, 'duo-fpp':4, 'squad-fpp':5,'other':6} )
%matplotlib inline 
<load_from_csv>
train.isnull().sum()
train_data=pd.read_csv(".. /input/train.csv")
 test_data=pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
train.isnull().sum()
y_label=train_data['label']<define_variables>
train.dropna(inplace=True)
 train.isnull().sum()
img_rows, img_cols = 28, 28
num_classes = 10<categorify>
data=enc.fit(train[['matchType']])
 temp=enc.transform(train[['matchType']] )
def data_prep(raw):
out_y = keras.utils.to_categorical(raw.label, num_classes)
 num_images = raw.shape[0]
x_as_array = raw.values[:,1:]
x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)
 out_x = x_shaped_array / 255
return out_x, out_y<define_variables>
temp1=pd.DataFrame(temp.toarray() ,columns=["solo", "duo", "squad", "solo-fpp", "duo-fpp", "squad-fpp","other"])
 temp1=temp1.set_index(train.index.values)
 temp1
train=pd.concat([train,temp1],axis=1)
train_size =len(train_data )<prepare_x_and_y>
train['killsasist']=train['kills']+train['assists']+train['roadKills']
train['total_distance']=train['swimDistance']+train['rideDistance']+train['walkDistance']
train['external_booster']=train['boosts']+train['weaponsAcquired']+train['heals']
train=train.drop(['assists','kills','swimDistance','rideDistance','walkDistance','boosts','weaponsAcquired','heals','roadKills','rankPoints'],axis=1 )
x,y = data_prep(train_data )<init_hyperparams>
train=train.drop(['killPoints','maxPlace','winPoints'],axis=1 )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
zoom_range = 0.18,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x )<split>
train['Players_all']=train.groupby('matchId')['Id'].transform('count')
 train['players_group']=train.groupby('groupId')['Id'].transform('count' )
X_train, X_val, Y_train, Y_val = train_test_split(x, y, test_size = 0.1, random_state=2 )<train_model>
Y=train.winPlacePerc
train = train.drop(["Id", "groupId", "matchId","winPlacePerc"], axis=1)
 del train['matchType']
train.head()
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self,epoch,logs={}):
if(logs.get('acc')>0.997):
print("
Reached 99.7% accuracy so cancelling training")
 self.model.stop_training=True<choose_model_class>
model = Sequential()<choose_model_class>
model.add(Conv2D(filters = 16, kernel_size =(3,3),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Flatten())
 model.add(Dense(512, activation = "relu"))
model.add(Dense(10, activation = "softmax")) 
<choose_model_class>
d_train = lgb.Dataset(train, label=Y)
 params = {}
params['learning_rate'] = 0.05
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'mae'
params['sub_feature'] = 0.9
params['num_leaves'] = 511
params['min_data'] = 1
params['max_depth'] = 30
params['min_gain_to_split']= 0.00001
clf = lgb.train(params, d_train,2000)
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<choose_model_class>
adam = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(
 loss=keras.losses.categorical_crossentropy,
optimizer= adam,
metrics=['accuracy'] )<train_model>
callbacks=myCallback()
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=128),
epochs = 80, validation_data =(X_val,Y_val), callbacks=[callbacks] )<predict_on_test>
test.loc[(test.matchType!='solo')&(test.matchType!='duo')&(test.matchType!='squad')&(test.matchType!='solo-fpp')&(test.matchType!='duo-fpp')&(test.matchType!='squad-fpp'),'matchType']='other'
test['matchType']=test['matchType'].map({'solo':0 , 'duo':1, 'squad':2, 'solo-fpp':3, 'duo-fpp':4, 'squad-fpp':5,'other':6} )
num_images=test_data.shape[0]
test_as_array = test_data.values[:,:]
test_shaped_array = test_as_array.reshape(num_images, img_rows, img_cols, 1)
 out_test= test_shaped_array / 255
y_pred=model.predict_classes(out_test )<save_to_csv>
test.isnull().sum()
submission = pd.DataFrame({"ImageId": np.arange(1, len(test_data)+1), "Label": y_pred})
 submission.to_csv('submission.csv', index=False )<load_from_csv>
test.isnull().sum()
print(os.listdir(".. /input"))
print(os.getcwd())
 X_data = pd.read_csv(".. /input/train.csv")
 T_df = pd.read_csv(".. /input/test.csv")
 Y_df = X_data["label"]
X_df = X_data.drop("label", axis=1)
 X = X_df.values
X = X/255
Y = Y_df.values
np.random.seed(100)
 X_train, X_test, Y_train, Y_test = train_test_split(X, Y)
 Y_train_r = np.ravel(Y_train)
 Y_test_r = np.ravel(Y_test)
 Y_train_c = np_utils.to_categorical(Y_train, 10)
 Y_test_c = np_utils.to_categorical(Y_test, 10 )<train_model>
data_test=enc.fit(test[['matchType']])
 temp_test=enc.transform(test[['matchType']])
 temp2=pd.DataFrame(temp_test.toarray() ,columns=["solo", "duo", "squad", "solo-fpp", "duo-fpp", "squad-fpp","other"])
 temp2=temp2.set_index(test.index.values)
 temp2
test=pd.concat([test,temp2],axis=1)
 del test['matchType']
KNN = KNeighborsClassifier(n_neighbors=1)
 KNN.fit(X_train,Y_train)
 KNN.score(X_test, Y_test)
<train_model>
test['killsasist']=test['kills']+test['assists']+test['roadKills']
test['total_distance']=test['swimDistance']+test['rideDistance']+test['walkDistance']
test['external_booster']=test['boosts']+test['weaponsAcquired']+test['heals']
test=test.drop(['assists','kills','swimDistance','rideDistance','walkDistance','boosts','weaponsAcquired','heals','roadKills','rankPoints'],axis=1 )
KNN_2 = KNeighborsClassifier(n_neighbors=2)
 KNN_2.fit(X_train,Y_train)
 KNN_2.score(X_test, Y_test )<compute_train_metric>
test=test.drop(['killPoints','maxPlace','winPoints'],axis=1 )
svclassifier = SVC(kernel="linear")
 svclassifier.fit(X_train, Y_train)
 Y_pred = svclassifier.predict(X_test)
 accuracy_score(Y_test, Y_pred )<compute_train_metric>
test['Players_all']=test.groupby('matchId')['Id'].transform('count')
 test['players_group']=test.groupby('groupId')['Id'].transform('count' )
svclassifier_rbf = SVC(kernel="rbf", gamma = "auto")
 svclassifier_rbf.fit(X_train, Y_train)
 Y_pred = svclassifier_rbf.predict(X_test)
 accuracy_score(Y_test, Y_pred )<train_model>
test_id=test.Id
test = test.drop(["Id", "groupId", "matchId"], axis=1)
 test.head()
Lreg = list()
for i in range(-3,4):
c = 10**i
dummy = LogisticRegression(multi_class="multinomial",solver="lbfgs", max_iter = 4000, C=c)
 Lreg.append(dummy.fit(X_train,Y_train_r))
Lreg_scores = list()
for i in range(0,6):
Lreg_scores.append(Lreg[i].score(X_test, Y_test))
print(Lreg_scores )<choose_model_class>
out=clf.predict(test)
 submission=pd.DataFrame({'Id':test_id,'winPlacePerc':out})
 submission.to_csv('submission.csv', index=False )