Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<import_modules>
warnings.filterwarnings('ignore')
 %matplotlib inline
py.init_notebook_mode(connected=True)
from sklearn.svm import SVC<find_best_model_class>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
svc = SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
 svc.fit(X_train, Y_train)
 y_pred_svc = svc.predict(X_test )<compute_test_metric>
df_train[df_train['groupId']==24]
acc_SVC = metrics.accuracy_score(y_pred_svc, Y_test)
 print(y_pred_svc)
 print("We see that SVC classifier gives an Accuracy of ",acc_SVC*100,"% on the traing set." )<import_modules>
len(df_train[df_train['matchId']==0] )
from xgboost import XGBClassifier<predict_on_test>
df_train['roadKills'].value_counts()
xgb=XGBClassifier(learning_rate=0.05, n_estimators=500)
 xgb.fit(X_train,Y_train)
 y_pred_xgb=xgb.predict(X_test )<compute_test_metric>
df_train['teamKills'].value_counts()
acc_XGB= metrics.accuracy_score(y_pred_xgb,Y_test)
 print(y_pred_xgb)
 print("We see that XGB classifier gives an Accuracy of ",acc_XGB*100,"% on the traing set." )<import_modules>
df_train['headshotKills'].value_counts()
import keras
from keras.layers import Dense
from keras.models import Sequential
from sklearn.metrics import classification_report<choose_model_class>
df_train['vehicleDestroys'].value_counts()
nn = Sequential()
nn.add(Dense(units= 14, activation = 'relu', input_dim=7, kernel_initializer="uniform"))
nn.add(Dense(units= 14, activation = 'relu',kernel_initializer="uniform"))
nn.add(Dense(units= 1, activation = 'sigmoid',kernel_initializer="uniform"))
nn.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy'])
<train_model>
headshot = df_train[['kills','winPlacePerc','headshotKills']]
headshot['headshotrate'] = headshot['headshotKills']/headshot['kills']
nn.fit(X_train,Y_train, batch_size=32,epochs=50,verbose= 0)
 nn_pred = nn.predict(X_test)
 nn_pred = [ 1 if y>=0.5 else 0 for y in nn_pred]
print(nn_pred)
 acc_NN = metrics.accuracy_score(Y_test, nn_pred )<train_model>
df_train['headshotrate'] = df_train['headshotKills']/df_train['kills']
df_test['headshotrate'] = df_test['headshotKills']/df_test['kills']
print("We can see that the neural network gives an Accuracy of ",acc_NN*100 , "% on the training set." )<find_best_params>
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()
best=best_model['Model'].iloc[0]
print(str(best))<feature_engineering>
del killStreak
df_train['killStreakrate'] = -(df_train['killStreaks']/df_train['kills'])
 df_test['killStreakrate'] = -(df_test['killStreaks']/df_test['kills'])
 del df_train['killStreaks']; del df_test['killStreaks']
test.head()
for c in test.columns:
print(c, str(100*test[c].isnull().sum() /len(test)))
 print(".............. Before")
 test['Age'] = test['Age'].fillna(test['Age'].mean())
 test['Fare'] = test['Fare'].fillna(test['Fare'].mean())
 print("
")
 for c in test.columns:
print(c, str(100*test[c].isnull().sum() /len(test)))
 print(".............. After" )<predict_on_test>
df_train['hacker_pt'] = 0
df_test['hacker_pt'] = 0
if best == 'Logistic Regression':
modelLR.fit(train.drop(['Survived'],axis=1),train['Survived'])
 test_pred=modelLR.predict(test)
 print(test_pred)
 print('Logistic Regression')
 if best == 'Decision Tree':
dtree.fit(train.drop(['Survived'],axis=1),train['Survived'])
 test_pred=modelLR.predict(test)
 print(test_pred)
 print('Decision Tree')
 if best =='Random Forest':
rforest.fit(train.drop(['Survived'],axis=1),train['Survived'])
 test_pred=rforest.predict(test)
 print(test_pred)
 print('Random Forest')
 if best == 'Support Vector Machine':
svc.fit(train.drop(['Survived'],axis=1),train['Survived'])
 test_pred=svc.predict(test)
 print(test_pred)
 print('Support Vector Machines')
 if best == 'Gradient Boosting Classifier':
xgb.fit(train.drop(['Survived'],axis=1),train['Survived'])
 test_pred=xgb.predict(test)
 print(test_pred)
 print('Gradient Boosting Classifier')
 if best == 'Artificial Neural Network':
nn.fit(train.drop(['Survived'],axis=1),train['Survived'], epochs=500 , verbose=0)
 test_pred=nn.predict(test)
 test_pred = [ 1 if y>=0.5 else 0 for y in test_pred]
print(test_pred)
 print('Artificial Neural Network')
<load_from_csv>
df_train['total_Distance'] = df_train['rideDistance'] + df_train["walkDistance"] + df_train["swimDistance"]
df_test['total_Distance'] = df_test['rideDistance'] + df_test["walkDistance"] + df_test["swimDistance"]
test_data=pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 test_data = test_data.drop(['PassengerId'], axis=1)
 test_data.head()<compute_test_metric>
df_train['hacker_pt'][(df_train['heals'] + df_train['boosts'] < 1)&(df_train['total_Distance'] < 100)&(df_train['kills'] > 20)] = 1
df_test['hacker_pt'][(df_test['heals'] + df_test['boosts'] < 1)&(df_test['total_Distance'] < 100)&(df_test['kills'] > 20)] = 1
test_data.values.tolist()
test_acc = metrics.accuracy_score(test_data, test_pred)
 print("Here we see that the test data has an Accuracy of ",test_acc*100,"% " )<load_from_csv>
df_train['hacker_pt'][(df_train['kills'] > 10)&(df_train['weaponsAcquired'] >= 10)&(df_train['total_Distance'] == 0)] += 1
df_test['hacker_pt'][(df_test['kills'] > 10)&(df_test['weaponsAcquired'] >= 10)&(df_test['total_Distance'] == 0)] += 1
test_predict = pd.DataFrame(test_pred, columns= ['Survived'])
 test_new= pd.read_csv('/kaggle/input/titanic/test.csv')
 new_test = pd.concat([test_new, test_predict], axis=1, join='inner' )<save_to_csv>
del healthitems
submit=new_test[['PassengerId','Survived']]
submit.to_csv('predictions.csv',index=False )<install_modules>
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()
!pip install emnist<import_modules>
del kills
df_train['kills_assists'] = df_train['kills'] + df_train['assists']
df_test['kills_assists'] = df_test['kills'] + df_test['assists']
del df_train['kills']; del df_test['kills']
import matplotlib.pyplot as plt,seaborn as sns,pandas as pd,numpy as np
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Conv2D, MaxPooling2D,MaxPool2D,Flatten,BatchNormalization
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from emnist import extract_training_samples
from emnist import extract_test_samples
from keras.optimizers import Adam<prepare_x_and_y>
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()
x_train, y_train = extract_training_samples('digits')
 x_test, y_test = extract_test_samples('digits' )<load_from_csv>
df_train_size = df_train.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_test_size = df_test.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_train_mean = df_train.groupby(['matchId','groupId'] ).mean().reset_index()
df_test_mean = df_test.groupby(['matchId','groupId'] ).mean().reset_index()
df_train_max = df_train.groupby(['matchId','groupId'] ).max().reset_index()
df_test_max = df_test.groupby(['matchId','groupId'] ).max().reset_index()
df_train_min = df_train.groupby(['matchId','groupId'] ).min().reset_index()
df_test_min = df_test.groupby(['matchId','groupId'] ).min().reset_index()

 df_train_match_mean = df_train.groupby(['matchId'] ).mean().reset_index()
df_test_match_mean = df_test.groupby(['matchId'] ).mean().reset_index()

 df_train = pd.merge(df_train, df_train_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_mean, suffixes=["", "_mean"], how='left', on=['matchId', 'groupId'])
 del df_train_mean
del df_test_mean
df_train = pd.merge(df_train, df_train_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_max, suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 del df_train_max
del df_test_max
df_train = pd.merge(df_train, df_train_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_min, suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 del df_train_min
del df_test_min
df_train = pd.merge(df_train, df_train_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 df_test = pd.merge(df_test, df_test_match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 del df_train_match_mean
del df_test_match_mean
df_train = pd.merge(df_train, df_train_size, how='left', on=['matchId', 'groupId'])
 df_test = pd.merge(df_test, df_test_size, how='left', on=['matchId', 'groupId'])
 del df_train_size
del df_test_size
gc.collect()
df_train = reduce_mem_usage(df_train)
 df_test = reduce_mem_usage(df_test)
 gc.collect()
in_train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 ex_y_train = in_train_data["label"]
ex_x_train = in_train_data.drop(labels = ["label"],axis = 1 )<prepare_x_and_y>
warnings.filterwarnings("ignore")
 color = sns.color_palette()
X_train = x_train.reshape(240000, 28, 28,1)
 X_test = x_test.reshape(40000, 28, 28,1)
 ex_x_train = ex_x_train.values.reshape(42000,28,28,1)
 X_train = np.vstack(( X_train, ex_x_train))
print(X_train.shape )<data_type_conversions>
train_columns = list(df_test.columns)
 train_idx = df_train.Id
test_idx = df_test.Id
train_columns.remove("Id")
 train_columns.remove("matchId")
 train_columns.remove("groupId" )
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32' )<categorify>
x_train = df_train[train_columns]
x_test = df_test[train_columns]
y_train = df_train["winPlacePerc"].astype('float' )
X_train /= 255
X_test /= 255<concatenate>
del df_train; del df_test
gc.collect()
y_train = np.concatenate([y_train,ex_y_train.values])
 print(y_train.shape )<categorify>
folds = KFold(n_splits=5,random_state=6)
 oof_preds = np.zeros(x_train.shape[0])
 sub_preds = np.zeros(x_test.shape[0])
 start = time.time()
valid_score = 0
importances = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(x_train, y_train)) :
trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]
val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 params = {"objective" : "regression", "metric" : "mae", 'n_estimators':10000, 'early_stopping_rounds':100,
"num_leaves" : 30, "learning_rate" : 0.3, "bagging_fraction" : 0.9,
"bagging_seed" : 0}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)
 oof_preds[oof_preds>1] = 1
oof_preds[oof_preds<0] = 0
sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration)/ folds.n_splits
sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred
print('Fold %2d RMSE : %.6f' %(n_fold + 1, mean_absolute_error(val_y, oof_preds[val_idx])))
 valid_score += mean_absolute_error(val_y, oof_preds[val_idx] )
n_classes = 10
print("Shape before one-hot encoding: ", y_train.shape)
 Y_train = np_utils.to_categorical(y_train, n_classes)
 Y_test = np_utils.to_categorical(y_test, n_classes)
 print("Shape after one-hot encoding: ", Y_train.shape )<choose_model_class>
test_pred = pd.DataFrame({"Id":test_idx})
 test_pred["winPlacePerc"] = sub_preds
test_pred.columns = ["Id", "winPlacePerc"]
test_pred.to_csv("lgb_base_model.csv", index=False )