Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<save_to_csv>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso
from catboost import CatBoostRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import StandardScaler,MinMaxScaler
sub.to_csv('submission.csv', index=False )<load_from_csv>
train=pd.read_csv('.. /input/train_V2.csv')
 test=pd.read_csv('.. /input/test_V2.csv')
 ID=test['Id']
df=pd.read_csv("/kaggle/input/covid19-global-forecasting-week-3/train.csv")
 test=pd.read_csv("/kaggle/input/covid19-global-forecasting-week-3/test.csv" )<feature_engineering>
train.isna().sum()
df["Province_State"].fillna("state", inplace = True)
 df["Country_Region"] = [country_name.replace("'","")for country_name in df["Country_Region"]]
print(df.shape,"
",df.head() )<split>
train=train.dropna(axis=0 )
X_y=shuffle(new_data)
 y_cases=X_y['target_infection']
y_fatal=X_y['target_fatal']
X=X_y.drop(['target_infection','target_fatal'],axis=1)
 X_train_cases, X_test_cases, y_train_cases, y_test_cases = train_test_split(X, y_cases, test_size=0.33)
 X_train_fatal, X_test_fatal, y_train_fatal, y_test_fatal = train_test_split(X, y_fatal, test_size=0.33)
 print("Shape of infection train dataset:",(X_train_cases.shape,y_train_cases.shape))
print("Shape of infection test dataset:",(X_test_cases.shape,y_test_cases.shape))
print("Shape of fatal train dataset:",(X_train_fatal.shape,y_train_fatal.shape))
print("Shape of fatal test dataset:",(X_test_fatal.shape,y_test_fatal.shape))<train_on_grid>
y_train=train['winPlacePerc']
train=train.drop(['winPlacePerc'],axis=1 )
reg_case=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=2200)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf = RandomizedSearchCV(reg_case, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search=clf.fit(X_train_cases, y_train_cases)
 results = pd.DataFrame.from_dict(clf.cv_results_)
 print("The best parameter is:",search.best_params_ )<train_model>
train["playersInMatch"] = train.groupby("matchId")["Id"].transform("count")
 train["playersInGroup"] = train.groupby("groupId")["Id"].transform("count")
 test["playersInMatch"] = test.groupby("matchId")["Id"].transform("count")
 test["playersInGroup"] = test.groupby("groupId")["Id"].transform("count" )
best_alpha=10000
best_itr=2400
final_reg_case=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_itr)
 final_reg_case.fit(X_train_cases,y_train_cases )<predict_on_test>
train['TotalKills'] = train.groupby('groupId')['kills'].transform('sum')
 test['TotalKills'] = test.groupby('groupId')['kills'].transform('sum' )
pred=final_reg_case.predict(X_test_cases)
 print("The RMSE value",(mean_squared_error(y_test_cases,pred)) **0.5 )<train_on_grid>
train['FirstMan'] = train.groupby('groupId')['matchDuration'].transform('min')
 test['FirstMan'] = test.groupby('groupId')['matchDuration'].transform('min' )
reg_fatal=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=3500)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf = RandomizedSearchCV(reg_fatal, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search=clf.fit(X_train_fatal, y_train_fatal)
 results = pd.DataFrame.from_dict(clf.cv_results_)
 print("The best parameter is:",search.best_params_ )<train_model>
train['LastMan'] = train.groupby('groupId')['matchDuration'].transform('max')
 test['LastMan'] = test.groupby('groupId')['matchDuration'].transform('max' )
best_alpha=100
best_iter=3500
final_reg_fatal = ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_iter)
 final_reg_fatal.fit(X_train_fatal, y_train_fatal )<predict_on_test>
train['Survival'] = train['LastMan'] - train['FirstMan']
test['Survival'] = test['LastMan'] - test['FirstMan']
pred=final_reg_fatal.predict(X_test_fatal)
 print("The RMSE value",(mean_squared_error(y_test_fatal,pred)) **0.5 )<split>
train['Position'] = train['killPlace'] /(train['maxPlace'] + 1e-9)
 test['Position'] = test['killPlace'] /(test['maxPlace'] + 1e-9 )
featured=pd.DataFrame(data)
 X_y_f=shuffle(featured)
 y_cases_f=X_y_f['target_infection']
y_fatal_f=X_y_f['target_fatal']
X_f=X_y_f.drop(['target_infection','target_fatal'],axis=1)
 X_train_cases_f, X_test_cases_f, y_train_cases_f, y_test_cases_f = train_test_split(X_f, y_cases_f, test_size=0.33)
 X_train_fatal_f, X_test_fatal_f, y_train_fatal_f, y_test_fatal_f = train_test_split(X_f, y_fatal_f, test_size=0.33)
 print("Shape of featurized infection train dataset:",(X_train_cases_f.shape,y_train_cases_f.shape))
print("Shape of featurized infection test dataset:",(X_test_cases_f.shape,y_test_cases_f.shape))
print("Shape of featurized fatal train dataset:",(X_train_fatal_f.shape,y_train_fatal_f.shape))
print("Shape of featurized fatal test dataset:",(X_test_fatal_f.shape,y_test_fatal_f.shape))<train_on_grid>
train.drop(["matchId","groupId",'Id','killPoints', 'maxPlace', 'winPoints','vehicleDestroys'],axis=1,inplace=True)
 test.drop(["matchId","groupId",'Id','killPoints', 'maxPlace', 'winPoints','vehicleDestroys'],axis=1,inplace=True )
reg_case_f=ElasticNet(random_state=42,l1_ratio=0.1,max_iter=2200)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf_f= RandomizedSearchCV(reg_case_f, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search_f=clf_f.fit(X_train_cases_f, y_train_cases_f)
 results_f = pd.DataFrame.from_dict(clf_f.cv_results_)
 print("The best parameter is:",search_f.best_params_ )<train_model>
train['headshotrate'] = train['kills'] /(train['headshotKills'] + 1e-9)
 test['headshotrate'] = test['kills'] /(test['headshotKills'] + 1e-9)
 train['killStreakrate'] = train['killStreaks'] /(train['kills'] + 1e-9)
 test['killStreakrate'] = test['killStreaks'] /(test['kills'] + 1e-9 )
best_alpha=100
best_itr=4200
final_reg_case_f=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_itr)
 final_reg_case_f.fit(X_train_cases_f,y_train_cases_f )<predict_on_test>
train['TotalDamage'] = train['damageDealt'] + train['teamKills']*100
test['TotalDamage'] = test['damageDealt'] + test['teamKills']*100
pred_f=final_reg_case_f.predict(X_test_cases_f)
 print("RMSE is:",(mean_squared_error(y_test_cases_f,pred_f)) **0.5 )<train_on_grid>
train['Noob']=(train['matchDuration'] < train['matchDuration'].mean())
 test['Noob']=(test['matchDuration'] < train['matchDuration'].mean() )
reg_fatal_f=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=2200)
 params = [{'alpha': [10**-4,10**-3, 10**-2,10**-1, 10**0,10**1, 10**2,10**3,10**4]}]
clf_f= RandomizedSearchCV(reg_fatal_f, params, cv=4, scoring='neg_root_mean_squared_error',return_train_score=True)
 search_f=clf_f.fit(X_train_fatal_f, y_train_fatal_f)
 results_f = pd.DataFrame.from_dict(clf_f.cv_results_)
 print("The best parameter is:",search_f.best_params_)
<train_model>
train['Sniper']=(train['longestKill']>=250)
 test['Sniper']=(test['longestKill']>=250 )
best_alpha=100
best_itr=2400
final_reg_fatal_f=ElasticNet(random_state=42,alpha=best_alpha,l1_ratio=0.1,max_iter=best_itr)
 final_reg_fatal_f.fit(X_train_fatal_f,y_train_fatal_f )<predict_on_test>
train['ProAim']=(train['headshotKills']/(train['kills']+1e-9))
test['ProAim']=(test['headshotKills']/(test['kills']+1e-9))
pred_f=final_reg_fatal_f.predict(X_test_fatal_f)
 print("RMSE is:",(mean_squared_error(y_test_fatal_f,pred_f)) **0.5 )<feature_engineering>
train['distance'] =(train['rideDistance']+train['swimDistance']+train['walkDistance'])
 test['distance'] =(test['rideDistance']+test['swimDistance']+test['walkDistance'])
 train['distance'] = np.log1p(train['distance'])
 test['distance'] = np.log1p(test['distance'] )
test["Province_State"].fillna("state", inplace = True)
 test["Country_Region"] = [country_name.replace("'","")for country_name in test["Country_Region"]]<categorify>
set1=set(i for i in train[(train['kills']>40)&(train['heals']==0)].index.tolist())
 set2=set(i for i in train[(train['distance']==0)&(train['kills']>20)].index.tolist())
 set3=set(i for i in train[(train['damageDealt']>4000)&(train['heals']<2)].index.tolist())
 set4=set(i for i in train[(train['rideDistance']>25000)].index.tolist())
 set5=set(i for i in train[(train['killStreaks']>3)&(train['weaponsAcquired']> 30)].index.tolist())
 sets=set1 | set2 | set3 | set4 | set5
test['ConfirmedCases']=list(map(int,predicted_case))
test['Fatalities']=list(map(int,predicted_fatal))<save_to_csv>
train=train.drop(list(sets))
y_train=y_train.drop(list(sets))
submission=test[['ForecastId','ConfirmedCases','Fatalities']]
submission=shuffle(submission)
 submission.to_csv("submission.csv",index=False )<import_modules>
fpp=['crashfpp','duo-fpp','flare-fpp','normal-duo-fpp','normal-solo-fpp','normal-squad-fpp','solo-fpp','squad-fpp']
train["fpp"] = np.where(train["matchType"].isin(fpp),1,0)
 test["fpp"] = np.where(test["matchType"].isin(fpp),1,0 )
import pandas as pd
import numpy as np
from sklearn.model_selection import cross_validate
from sklearn.gaussian_process.kernels import RBF, WhiteKernel, ConstantKernel, RationalQuadratic, DotProduct,Matern
from sklearn.gaussian_process import GaussianProcessClassifier 
<load_from_csv>
change={'crashfpp':'crash',
'crashtpp':'crash',
'duo':'duo',
'duo-fpp':'duo',
'flarefpp':'flare',
'flaretpp':'flare',
'normal-duo':'duo',
'normal-duo-fpp':'duo',
'normal-solo':'solo',
'normal-solo-fpp':'solo',
'normal-squad':'squad',
'normal-squad-fpp':'squad',
'solo-fpp':'solo',
'squad-fpp':'squad',
'solo':'solo',
'squad':'squad'
}
train['matchType']=train['matchType'].map(change)
 test['matchType']=test['matchType'].map(change )
train_df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv')
 for test_set,df in enumerate([train_df, test_df]):
df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df["Title"] = df["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1,
"Mlle":1, "Mrs":1, "Mr":2, "Rare":3} ).fillna(3 ).astype(int)
 df['Sex'] = df['Sex'].map({'female': 1, 'male': 0} ).fillna(2 ).astype(int)
 df['Embarked'] = df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).fillna(3 ).astype(int)
 df["Cabin"] = pd.Series([i[0] if not pd.isnull(i)else 'X' for i in df['Cabin'] ])
 df['Cabin'] = df['Cabin'].map({'A': 0, 'B': 1, 'C': 2,'D': 3, 'E': 4,
'F': 5,'G': 6, 'T': 7, 'X': 8} ).fillna(9 ).astype(int)
 df = df.drop(['Name', 'PassengerId', 'Ticket'], axis=1)
 index_NaN = list(df["Fare"][df["Fare"].isnull() ].index)
 for i in index_NaN:
df.loc[i,'Fare'] = df.Fare[df['Pclass'] == df.loc[i,"Pclass"]].median()
index_NaN = list(df["Age"][df["Age"].isnull() ].index)
 for i in index_NaN:
idx =(( df['SibSp'] == df.iloc[i]["SibSp"])& 
(df['Parch'] == df.iloc[i]["Parch"])& 
(df['Pclass'] == df.iloc[i]["Pclass"]))
age_pred = df.Age[idx].median()
if np.isnan(age_pred):
df.loc[i,'Age'] = df["Age"].median()
else:
df.loc[i,'Age'] = age_pred
if test_set == True:
X_test = df
else:
X_train = df.drop(['Survived'], axis=1)
 y_train = df["Survived"]
print(X_train.head(5))
print(X_test.head(5))
print(y_train.head(5))
dx = X_train.shape[1] 
<train_on_grid>
modes={'crash':1,
'duo':2,
'flare':3,
'solo':4,
'squad':5
}
train['matchType']=train['matchType'].map(modes)
 test['matchType']=test['matchType'].map(modes )
ls = np.ones(( dx,))
kernels = [ConstantKernel() * RBF(ls)+ WhiteKernel() ,
ConstantKernel() * Matern(ls)+ WhiteKernel() ,
]
best_score = 0
for kernel in kernels:
classifier = GaussianProcessClassifier(kernel=kernel,n_restarts_optimizer=5)
 classifier.fit(X_train, y_train)
 result = cross_validate(classifier,X_train, y_train,cv=3)
 score = result['test_score'].mean()
print('Average result: ' + str(score)+ ', Training time: ' + str(result['fit_time'].mean()))
if score > best_score:
best_score = score
best_classifier = classifier 
<save_to_csv>
d1=pd.get_dummies(train['matchType'])
 train=train.drop(['matchType'],axis=1)
 train=train.join(d1)
 d2=pd.get_dummies(test['matchType'])
 test=test.drop(['matchType'],axis=1)
 test=test.join(d2)
predictions = best_classifier.predict(X_test ).astype(int)
 output = pd.DataFrame({'PassengerId': test_df.PassengerId,
'Survived': predictions})
 print(best_classifier.kernel)
 print(output)
 output.to_csv('submission.csv', index=False )<load_from_csv>
scaler = MinMaxScaler()
scaler.fit(train)
 train=scaler.transform(train)
 test=scaler.transform(test )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head(5 )<load_from_csv>
df = pd.DataFrame(train)
 df.isnull().sum()
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head(5 )<define_variables>
X_train,X_test,y_train,y_test= train_test_split(train,y_train,test_size=0.3 )
data_dict = {}
data_dict['train data'] = train_data
data_dict['test data'] = test_data; 
<feature_engineering>
lm = Lasso(alpha=1e-5)
 lm.fit(X_train,y_train )
compare_features(train_data, 'Pclass','Age' )<feature_engineering>
train_mse =(mean_absolute_error(y_train,lm.predict(X_train)))
 test_mse =(mean_absolute_error(y_test, lm.predict(X_test)))
 train_mse,test_mse
compare_features(train_data, 'SibSp', 'Survived' )<feature_engineering>
y_train = y_train - lm.predict(X_train)
 y_test = y_test - lm.predict(X_test )
compare_features(train_data, 'Parch', 'Survived' )<feature_engineering>
train_pool = Pool(X_train, y_train)
 test_pool = Pool(X_test, y_test )
compare_features(train_data, 'Embarked', 'Survived' )<drop_column>
model = CatBoostRegressor(
 iterations=5000,
depth=10,
learning_rate=0.1,
l2_leaf_reg= 2,
loss_function='RMSE',
eval_metric='MAE',
random_strength=0.1,
bootstrap_type='Bernoulli',
leaf_estimation_method='Gradient',
leaf_estimation_iterations=1,
boosting_type='Plain'
,task_type = "GPU"
,feature_border_type='GreedyLogSum'
,random_seed=1234
)
train_data.groupby(['Embarked'] ).mean().drop(['PassengerId', 'SibSp', 'Parch'], axis=1 )<concatenate>
model.fit(train_pool, eval_set=test_pool )
def concat_data(data_1, data_2):
return pd.concat([data_1, data_2], sort=False ).reset_index(drop=True)
 def divide_data(all_data):
return all_data.loc[:890], all_data.loc[891:].drop(['Survived'], axis=1)
 data_all = concat_data(train_data, test_data)
 data_fs = [train_data, test_data]<count_missing_values>
train_mse =(mean_absolute_error(y_train,lm.predict(X_train)+ model.predict(X_train)))
 test_mse =(mean_absolute_error(y_test, lm.predict(X_test)+ model.predict(X_test)))
 print('Train error= ',train_mse)
 print('Test error= ',test_mse)
<compute_test_metric><EOS> 
subm = pd.read_csv('.. /input/sample_submission_V2.csv')
 predictions = model.predict(test)+ lm.predict(test)
 test = pd.read_csv('.. /input/test_V2.csv')
 test['winPlacePerc'] = predictions
test['winPlacePerc'] = test.groupby('groupId')['winPlacePerc'].transform('median')
 subm['winPlacePerc'] = test['winPlacePerc']
subm['Id']=ID
subm.to_csv('submission.csv', index = False)