Kaggle competition: COVID19 Global Forecasting (Week 4)
Y_Column = 'winPlacePerc'
ColumnList = ['assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill', 'maxPlace', 'numGroups', 'revives',
'rideDistance', 'roadKills', 'swimDistance', 'teamKills',
'vehicleDestroys', 'walkDistance', 'weaponsAcquired', 'winPoints']
for colname in ColumnList:
df[colname+ '_1']= df[colname]/(max(df[colname])- min(df[colname]))
print(colname,'_1',' created')
 df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['kills_assists'] =(df['kills'] + df['assists'])
 df['healthitems'] = df['heals'] + df['boosts']
df['kills_']= df['kills'] + df['longestKill']+ df['killStreaks']
df['killtypes']= df['headshotKills'] + df['roadKills']+ df['teamKills']+ df['vehicleDestroys']
df['Others']= df['damageDealt'] + df['DBNOs']+ df['revives'] + df['weaponsAcquired']
df['Points_'] = df['killPoints'] + df['winPoints']
df['Rank_Points']= df.groupby(by = ['matchId'])['Points_'].rank(ascending= False)
 df['Rank_kills']= df.groupby(by = ['matchId'])['kills_'].rank(ascending= False)
 df['Rank_killassist']= df.groupby(by = ['matchId'])['kills_assists'].rank(ascending= False)
 df['Rank_GroupPoints']= df.groupby(by = ['groupId'])['Points_'].rank(ascending= False)
 df['Rank_Groupkills']= df.groupby(by = ['groupId'])['kills_'].rank(ascending= False)
 df['Rank_killassist']= df.groupby(by = ['groupId'])['kills_assists'].rank(ascending= False)
 ColumnList = df.columns.tolist()
print(ColumnList)
 ColumnList.remove('Id')
 ColumnList.remove('groupId')
 ColumnList.remove('matchId')
 ColumnList.remove('Type')
 ColumnList.remove('winPlacePerc')
 ColumnList.remove('matchType')
 df_Train = df[df['Type']== 'Train']
df_Test = df[df['Type']== 'Test']
df_Train= df_Train.set_index(['Id', 'groupId', 'matchId','Type'])
 df_Test= df_Test.set_index(['Id', 'groupId', 'matchId','Type'])
 X_Train = df_Train[ColumnList]
Y_Train = df_Train[Y_Column]
X_test = df_Test[ColumnList]
print(X_Train.shape, Y_Train.shape, X_test.shape )<import_modules>
%matplotlib inline
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error<train_model>
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv' )
%%time
d_train1 = lgb.Dataset(X_Train, label=Y_Train.values)
 params = {}
params['learning_rate'] = 0.09
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'mae'
params['sub_feature'] = 0.8
params['num_leaves'] = 1000
params['min_data'] = 1
params['max_depth'] = 400
params['min_gain_to_split']= 0.0000001
clf1 = lgb.train(params, d_train1, 1000)
 y_pred=clf1.predict(X_test)
 print(y_pred)
<merge>
date = pd.to_datetime(train["Date"])
 datet = pd.to_datetime(test["Date"])
 print(date)
 train['Date'] = pd.to_datetime(train.Date)
 test['Date'] = pd.to_datetime(test.Date )
X_test = X_test.reset_index()
X_test["winPlacePerc"] = y_pred
X_test = X_test[['Id',"winPlacePerc"]]
df_test = df_test[['Id','groupId']]
df_test= df_test.merge(X_test, on = ['Id'] )<save_to_csv>
ldate = int(len(date))
ldatet = int(len(datet))
print("Length of training- date is", ldate)
 print("Length of test- date is", ldatet )
df_test[['Id',"winPlacePerc"]].to_csv("submission.csv", index=False )<import_modules>
train['month'] = train['Date'].dt.month
train['day'] = train['Date'].dt.day
test['month'] = test['Date'].dt.month
test['day'] = test['Date'].dt.day
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn.preprocessing import MinMaxScaler,StandardScaler
from sklearn.model_selection import train_test_split
import seaborn as sns
from catboost import Pool, CatBoostRegressor<load_from_csv>
print("Datatrain")
 traindays = train['Date'].nunique()
print("Number of Country_Region: ", train['Country_Region'].nunique())
 print("Number of Province_State: ", train['Province_State'].nunique())
 print("Number of Days: ", traindays)
 notrain = train['Id'].nunique()
print("Number of datapoints in train:", notrain)
 lotrain = int(notrain/traindays)
 print("L Trains:", lotrain)
data=pd.read_csv('.. /input/train_V2.csv' )<groupby>
print("Datatest")
 testdays = test['Date'].nunique()
print("Number of Days: ", testdays)
 notest = test['ForecastId'].nunique()
print("Number of datapoints in test:", notest)
 lotest = int(notest/testdays)
 print("L Test:", lotest )
gamer_counts=data.groupby(['matchId'] ).size().reset_index(name='counts' )<merge>
zt = datet[0]
daycount = []
for i in range(0,lotrain):
for j in range(1,traindays+1):
daycount.append(j)
 print(daycount)
 print(zt )
train_data=pd.merge(data,gamer_counts, on='matchId' )<categorify>
for i in range(traindays):
if(zt == date[i]):
zx = i
print(zx)
 daytest = []
for i in range(0,lotest):
for j in range(1,testdays+1):
jr = zx + j
daytest.append(jr)
 print(daytest )
mtype_val=['squad-fpp','duo-fpp','squad','solo-fpp','duo','solo','normal-squad-fpp','crashfpp','normal-duo-fpp','flaretpp','normal-solo-fpp','flarefpp','normal-squad','crashtpp','normal-solo','normal-duo']
mtype_s=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
mtype_typ=dict(zip(mtype_val,mtype_s))
train_data=train_data.replace({'matchType':mtype_typ} )<feature_engineering>
train.insert(8,"DayCount",daycount,False)
 test.insert(6,"DayCount",daytest,False )
train_data['winPlacePerc']=train_data['winPlacePerc'].fillna(train_data['winPlacePerc'].mean() )<drop_column>
traincount = int(len(train["Date"]))
testcount = int(len(test["Date"]))
print(traincount,testcount )
train_sub=train_data['winPlacePerc']
train_data=train_data.drop(['winPlacePerc','Id','groupId','matchId'],axis=1 )<normalization>
train.Province_State = train.Province_State.fillna(0)
 empty = 0
for i in range(0,traincount):
if(train.Province_State[i] == empty):
train.Province_State[i] = train.Country_Region[i]
scaler = MinMaxScaler()
scaler.fit(train_data)
 train=scaler.transform(train_data )<split>
test.Province_State = test.Province_State.fillna(0)
 empty = 0
for i in range(0,testcount):
if(test.Province_State[i] == empty):
test.Province_State[i] = test.Country_Region[i]
train_X, test_X, train_y, test_y = train_test_split(train, train_sub, test_size=0.5, random_state=42 )<define_variables>
label = preprocessing.LabelEncoder()
train.Country_Region = label.fit_transform(train.Country_Region)
 train.Province_State = label.fit_transform(train.Province_State )
train_pool = Pool(train_X, train_y)
 test_pool = Pool(test_X, test_y.values )<choose_model_class>
test.Country_Region = label.fit_transform(test.Country_Region)
 test.Province_State = label.fit_transform(test.Province_State)
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_model>
train.rename({'Id': 'ForecastId'}, axis=1, inplace=True )
model.fit(train_pool, eval_set=test_pool, plot=True )<load_from_csv>
forecastid = test['ForecastId']
cases = train.ConfirmedCases
fatalities = train.Fatalities
train = train.drop(['ForecastId', 'Date','ConfirmedCases','Fatalities'], axis = 1)
 test = test.drop(['ForecastId', 'Date'], axis = 1 )
test_data=pd.read_csv('.. /input/test_V2.csv' )<groupby>
lr_model = LinearRegression()
lr_modelfat = LinearRegression()
lr_model.fit(train, cases)
 lr_modelfat.fit(train,fatalities )
gamer_counts=test_data.groupby(['matchId'] ).size().reset_index(name='counts' )<merge>
ypredtr = lr_model.predict(train)
 ypredtrft = lr_modelfat.predict(train)
 err = mean_squared_error(ypredtr,cases)
 err1 = mean_squared_error(ypredtrft,fatalities)
 print(err,err1 )
test=pd.merge(test_data,gamer_counts, on='matchId' )<drop_column>
regr_cs = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.04, random_state = 42 , max_depth = 23)
 regr_ft = XGBRegressor(n_estimators = 2500 , gamma = 0, learning_rate = 0.04, random_state = 42 , max_depth = 23 )
test=test.drop(['Id','groupId','matchId'],axis=1 )<categorify>
regr_cs.fit(train,cases)
 ypred = regr_cs.predict(train)
 err = mean_squared_error(ypred,cases)
 print("Training - Mean Squared Error is: ",err )
test=test.replace({'matchType':mtype_typ} )<normalization>
ypred1 = regr_cs.predict(test )
test=scaler.transform(test )<predict_on_test>
submission = pd.DataFrame({"ForecastId": forecastid, "ConfirmedCases": ypred1},
columns=["ForecastId", "ConfirmedCases"] )
pred_test=model.predict(test )<groupby>
regr_ft.fit(train,fatalities)
 ypred2= regr_ft.predict(train)
 yptest = regr_ft.predict(test)
 error = mean_squared_error(ypred2,fatalities)
 print("Training -(Fatalities)Mean Squared Error is", error )
res=test_data.filter(['Id'],axis=1)
 res['winPlacePerc']=pred_test<save_to_csv>
submission['Fatalities'] = yptest
submission = round(submission)
 submission.head(10 )
res.to_csv('submission.csv', index=False )<set_options>
submission.to_csv('submission.csv', index=False )
gc.enable()<load_from_csv>
submission.to_csv('submission.csv', index=False )