Kaggle competition: COVID19 Global Forecasting (Week 1)
PassengerId=df_test['PassengerId']<drop_column>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from math import cos, sin
df_train.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
 df_test.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True )<count_missing_values>
train_df = pd.read_csv('.. /input/covid19-global-forecasting-week-1/train.csv')
 test_df = pd.read_csv('.. /input/covid19-global-forecasting-week-1/test.csv')
 df = [train_df, test_df]
df_train.isnull().sum() /len(df_train)*100<drop_column>
train_df.isnull().sum()
df_train.drop(['Cabin'],axis=1,inplace=True)
 df_test.drop(['Cabin'],axis=1,inplace=True )<count_missing_values>
arr2 = [i for i in range(163)]
df_train.dropna(subset=['Embarked'],inplace=True)
 df_train['Embarked'].isnull().sum()<groupby>
country_dict = dict(zip(arr1, arr2))
age_train_series=df_train.groupby(['Pclass','Sex'])['Age'].transform('median' )<groupby>
for dataset in df:
dataset['Country_int'] = dataset['Country/Region'].map(country_dict ).astype(int )
age_test_series=df_test.groupby(['Pclass','Sex'])['Age'].transform('median' )<feature_engineering>
for dataset in df:
dataset['Date'] = pd.to_datetime(dataset['Date'] )
df_train['Age']=df_train['Age'].fillna(age_train_series )<feature_engineering>
for dataset in df:
dataset['Day'] = dataset.Date.apply(lambda x: x.day)
 dataset['Month'] = dataset.Date.apply(lambda x: x.month )
df_test['Age']=df_test['Age'].fillna(age_test_series )<count_missing_values>
for j in train_df['Country/Region'].unique() :
l = train_df[(train_df['Country/Region'] == j)]
len_state = len(l['Province/State'].unique())
 if len_state != 1:
arr_state1 = l['Province/State'].unique()
arr_state2 = [i for i in range(1,len_state+1)]
state_dict = dict(zip(arr_state1, arr_state2))
for dataset in df:
dataset['Province/State'] = dataset['Province/State'].map(state_dict )
df_test.isnull().sum()<categorify>
for dataset in df:
dataset['X_Cord'] =(dataset['Lat'].apply(cos)) *(dataset['Long'].apply(cos))
dataset['Y_Cord'] =(dataset['Lat'].apply(cos)) *(dataset['Long'].apply(sin))
Fare_test_series=df_test.groupby(['Pclass'])['Fare'].transform('median')
 df_test['Fare']=df_test['Fare'].fillna(Fare_test_series )<categorify>
for dataset in df:
dataset['Province/State'] = dataset['Province/State'].astype(int )
df_train=pd.get_dummies(df_train,drop_first=True)
<categorify>
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].astype(int)
 train_df['Fatalities'] = train_df['Fatalities'].astype(int )
df_test=pd.get_dummies(df_test,drop_first=True )<split>
import seaborn as sns
X = df_train.drop('Survived',axis=1)
 y = df_train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=42)
 X_train.shape, X_test.shape<import_modules>
X = train_df.drop(['Id', 'Country/Region', 'Lat', 'Long', 'Date', 'ConfirmedCases', 'Fatalities', 'Province/State'], axis=1)
 y1 = train_df['ConfirmedCases']
y2 = train_df['Fatalities']
X_test = test_df.drop(['ForecastId', 'Country/Region', 'Lat', 'Long', 'Date', 'Province/State'], axis=1 )
from sklearn.ensemble import RandomForestClassifier<train_model>
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.model_selection import GridSearchCV
rf=RandomForestClassifier(random_state=42,n_estimators=100,max_depth=4,min_samples_leaf=15,max_features=3)
 rf.fit(X_train,y_train )<compute_test_metric>
lin_reg_regressor = LinearRegression()
lin_reg_regressor.fit(X, y1)
 lin_reg_regressor.score(X, y1 )
accuracy_score(y_test,rf.predict(X_test))<save_to_csv>
KNN_regressor = KNeighborsRegressor()
KNN_regressor.fit(X, y1)
 KNN_regressor.score(X, y1 )
predictions=rf.predict(df_test)
 titanic_4=pd.DataFrame({'PassengerId':PassengerId,'Survived':predictions})
 titanic_4.to_csv('My_4th_submission',index=False)
<load_from_csv>
tree_regressor = DecisionTreeRegressor()
tree_regressor.fit(X, y1)
 tree_regressor.score(X, y1 )
pd.read_csv('My_4th_submission' )<load_from_csv>
Random_Forest_regressor = RandomForestRegressor()
Random_Forest_regressor.fit(X, y1)
 Random_Forest_regressor.score(X, y1 )
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv' )<drop_column>
XGB_regressor = XGBRegressor()
XGB_regressor.fit(X, y1)
 XGB_regressor.score(X, y1 )
train_data.drop(['PassengerId'],axis=1,inplace=True)
 test_data.drop(['PassengerId'],axis=1,inplace=True )<count_missing_values>
param_test = {
'max_depth': range(3, 12, 2),
'min_child_weight': range(1, 6, 2),
'gamma': [i/10.0 for i in range(0,5)],
'learning_rate': [0.1, 0.2, 0.3],
'n_estimators': [100, 400, 600, 900, 1100]
}
print("Train dataset
", train_data.isna().sum())
 print("
Test dataset
", test_data.isna().sum() )<categorify>
gsearch = GridSearchCV(estimator = XGBRegressor() ,
param_grid = param_test,
scoring='neg_root_mean_squared_error',
n_jobs=-1,
cv=5 )
train_age = pd.get_dummies(train_data['Age_Cat'])
 test_age = pd.get_dummies(test_data['Age_Cat'])
 train_data.drop(['Age_Cat'],axis=1,inplace=True)
 test_data.drop(['Age_Cat'],axis=1,inplace=True)
 train_data = pd.concat([train_data,train_age],axis=1)
 test_data = pd.concat([test_data,test_age],axis=1 )<categorify>
gsearch.best_estimator_
sex1 = pd.get_dummies(train_data['Sex'])
 sex2 = pd.get_dummies(test_data['Sex'])
 train_data.drop(['Sex'],axis=1,inplace=True)
 test_data.drop(['Sex'],axis=1,inplace=True)
 train_data = pd.concat([train_data,sex1],axis=1)
 test_data = pd.concat([test_data,sex2],axis=1)
<drop_column>
XGB_regressor=XGBRegressor(base_score=0.5, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0.0, gpu_id=-1,
importance_type='gain', learning_rate=0.1, max_delta_step=0, max_depth=3,
min_child_weight=1, n_estimators=100, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1)
 XGB_regressor.fit(X, y1)
 XGB_regressor.score(X, y1 )
train_data.drop(['female'],axis=1,inplace=True)
 test_data.drop(['female'],axis=1,inplace=True )<rename_columns>
ConfirmedCasesPred = XGB_regressor.predict(X_test)
 ConfirmedCasesPred = pd.DataFrame(ConfirmedCasesPred, columns=['ConfirmedCases'] )
train_data.rename(columns={'male':'gender'}, inplace = True)
 test_data.rename(columns={'male':'gender'}, inplace = True )<categorify>
XGB_regressor.fit(X, y2)
 XGB_regressor.score(X, y2 )
pclass_age_map = {
1: 38,
2: 30,
3: 25,
}
def replace_age_na(x_df, fill_map):
cond=x_df['Age'].isna()
res =x_df.loc[cond,'Pclass'].map(fill_map)
 x_df.loc[cond,'Age']=res
return x_df<categorify>
FatalitiesPred = XGB_regressor.predict(X_test)
 FatalitiesPred = pd.DataFrame(FatalitiesPred, columns=['Fatalities'] )
train_data =(train_data.pipe(replace_age_na, pclass_age_map))
test_data =(test_data.pipe(replace_age_na, pclass_age_map))<categorify>
ForecastId = test_df.ForecastId
ForecastId = pd.DataFrame(ForecastId )
Class_train = pd.get_dummies(train_data['Pclass'].astype(str))
Class_test = pd.get_dummies(test_data['Pclass'].astype(str))
train_data = pd.concat([train_data,Class_train],axis=1)
 test_data = pd.concat([test_data,Class_test],axis=1)
<rename_columns>
pred_file = pd.concat([ForecastId, ConfirmedCasesPred, FatalitiesPred], axis=1 )
train_data.rename(columns = {'1':'Class 1', '2':'Class 2', '3':'Class 3'}, inplace = True)
 test_data.rename(columns = {'1':'Class 1', '2':'Class 2', '3':'Class 3'}, inplace = True )<define_variables>
pred_file.to_csv('submission.csv', index=False )