Kaggle competition: COVID19 Global Forecasting (Week 1)
Fare_0 = []
Fare_1 = []
for i in range(0,len(train_data["Fare"])) :
if train_data["Survived"][i] == 0:
Fare_0.append(train_data["Fare"][i])
 else:
Fare_1.append(train_data["Fare"][i] )<filter>
x=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/train.csv')
 x_test=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/test.csv' )
test_data.loc[test_data['Fare'].isnull() ] 
<filter>
x.isnull().sum()
test_data.loc[test_data['Name']=='Storey, Mr.Thomas']<categorify>
x1=x.iloc[:,3:5]
x_test2=x_test.iloc[:,3:5]
embark1 = pd.get_dummies(train_data['Embarked'])
 embark2 = pd.get_dummies(test_data['Embarked'])
 train_data.drop(['Embarked'],axis=1,inplace=True)
 test_data.drop(['Embarked'],axis=1,inplace=True)
 train_data = pd.concat([train_data,embark1],axis=1)
 test_data = pd.concat([test_data,embark2],axis=1 )<rename_columns>
x_test.isnull().sum()
train_data.rename(columns = {'C':'Cherbourg', 'Q':'Queenstown', 'S':'Southampton'}, inplace = True)
 test_data.rename(columns = {'C':'Cherbourg', 'Q':'Queenstown', 'S':'Southampton'}, inplace = True )<feature_engineering>
from fancyimpute import IterativeImputer as MICE
def fam(x):
if(x['SibSp'] + x['Parch'])> 0:
return 1
else:
return 0
train_data['Family'] = train_data.apply(fam, axis = 1)
 test_data['Family'] = test_data.apply(fam, axis = 1 )<drop_column>
x1_c = list(x1 )
train_data.drop(['SibSp','Parch'],axis=1, inplace = True)
 test_data.drop(['SibSp','Parch'],axis=1, inplace = True )<feature_engineering>
y=MICE()
train_data["Cabin"] = pd.Series([i[0]
if not pd.isnull(i)
 else 'X'
for i in train_data['Cabin'] ])
 test_data["Cabin"] = pd.Series([i[0]
if not pd.isnull(i)
 else 'X' for i in test_data['Cabin'] ] )<categorify>
x2 = pd.DataFrame(y.fit_transform(x1))
Train_cabin = pd.get_dummies(train_data['Cabin'])
 Test_cabin = pd.get_dummies(test_data['Cabin'])
 train_data = pd.concat([train_data,Train_cabin],axis=1)
 test_data = pd.concat([test_data,Test_cabin],axis=1)
<feature_engineering>
x_test3=pd.DataFrame(y.transform(x_test2))
train_title = [i.split(",")[1].split(".")[0].strip()
for i in train_data["Name"]]
train_data["Title"] = pd.Series(train_title)
 test_title = [i.split(",")[1].split(".")[0].strip()
for i in test_data["Name"]]
test_data["Title"] = pd.Series(test_title )<count_values>
x2.columns=x1_c
x_test3.columns=x1_c
print('The list of titles for the passengers names are as follow
',train_data["Title"].value_counts() )<count_values>
x2.isnull().sum()
print('The list of titles for the passengers names are as follow
',test_data["Title"].value_counts() )<categorify>
x_test3.isnull().sum()
Train_title = pd.get_dummies(train_data['Title'])
 Test_title = pd.get_dummies(test_data['Title'])
 train_data = pd.concat([train_data,Train_title],axis=1)
 test_data = pd.concat([test_data,Test_title],axis=1)
<feature_engineering>
x['Lat']=x2['Lat']
x_test['Lat']=x_test3['Lat']
Ticket1 = []
for i in list(train_data.Ticket):
if not i.isdigit() :
Ticket1.append(i.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket1.append("X")
 train_data["Ticket"] = Ticket1
Ticket2 = []
for j in list(test_data.Ticket):
if not j.isdigit() :
Ticket2.append(j.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket2.append("X")
 test_data["Ticket"] = Ticket2<categorify>
x['Long']=x2['Long']
x_test['Long']=x_test3['Long']
Train_ticket = pd.get_dummies(train_data['Ticket'], prefix="T")
 Test_ticket = pd.get_dummies(test_data['Ticket'], prefix="T")
 train_data = pd.concat([train_data,Train_ticket],axis=1)
 test_data = pd.concat([test_data,Test_ticket],axis=1)
<drop_column>
x.isnull().sum()
train_data.drop(['Age','Cabin', 'Name', 'Title', 'Ticket'],axis=1,inplace=True)
 test_data.drop(['Age','Cabin', 'Name', 'Title', 'Ticket'],axis=1,inplace=True )<concatenate>
x['Date']=pd.to_datetime(x['Date'])
 x_test['Date']=pd.to_datetime(x_test['Date'] )
intersect_set = np.intersect1d(train_data.columns, test_data.columns)
 print("Selected Features = ", intersect_set )<count_missing_values>
from datetime import date
train_data_select = train_data[intersect_set]
print("Sum of NaN train dataset = ", train_data_select.isnull().sum().sum())
 test_data_select = test_data[intersect_set]
print("Sum of NaN test dataset = ",test_data_select.isnull().sum().sum())
<normalization>
z=x['Date']-mindate
X= preprocessing.StandardScaler().fit_transform(train_data_select )<prepare_x_and_y>
for i in range(len(z)) :
z[i]=int(str(z[i] ).split() [0] )
y = train_data['Survived']<import_modules>
x['Date']=z
from sklearn.pipeline import Pipeline
from sklearn import svm
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier, plot_tree
import xgboost as xgb
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import metrics
from sklearn.model_selection import KFold, cross_val_score<create_dataframe>
c=x_test['Date']-mindate
KFold_Score = pd.DataFrame()
classifiers = ['KNeighborsClassifier','Radial SVM', 'LogisticRegression',
'DecisionTreeClassifier', 'RandomForestClassifier', 'AdaBoostClassifier',
'XGBoostClassifier']
models = [KNeighborsClassifier() ,
svm.SVC(kernel='rbf'),
LogisticRegression(max_iter = 1000),
DecisionTreeClassifier(random_state=42, max_depth = 5),
RandomForestClassifier(n_estimators=200, random_state=42, max_depth = 5),
AdaBoostClassifier(n_estimators=200, random_state = 42),
xgb.XGBClassifier(n_estimators=200, use_label_encoder=False, eval_metric = 'logloss'),
]
j = 0
for i in models:
model = i
cv = KFold(n_splits=5, random_state=42, shuffle=True)
 KFold_Score[classifiers[j]] =(cross_val_score(model, X, np.ravel(y), scoring = 'accuracy', cv=cv))
j = j+1<create_dataframe>
for i in range(len(c)) :
c[i]=int(str(c[i] ).split() [0] )
mean = pd.DataFrame(KFold_Score.mean() , index= classifiers)
 KFold_Score = pd.concat([KFold_Score,mean.T])
 KFold_Score.index=['Fold 1','Fold 2','Fold 3','Fold 4','Fold 5','Mean']
KFold_Score.T.sort_values(by=['Mean'], ascending = False )<normalization>
x_test['Date']=c
X_test = preprocessing.StandardScaler().fit_transform(test_data_select )<choose_model_class>
x['Date']=x['Date'].astype(int )
rfc = RandomForestClassifier(n_estimators=200, random_state=42, max_depth = 5 )<define_search_space>
x_test['Date']=x_test['Date'].astype(int )
param_grid = {
'n_estimators': [ 100, 200,300],
'max_features': ['auto', 'sqrt'],
'max_depth' : [5, 6,7,8],
'criterion' :['gini', 'entropy']
}<train_on_grid>
y1_val=x.iloc[:,-2]
CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
 CV_rfc.fit(X,y)
 CV_rfc.best_params_<train_model>
y1_val=y1_val.astype(int )
select_rfc = RandomForestClassifier(random_state=42, n_estimators= 100, criterion = 'entropy',max_features = 'auto',max_depth = 8)
 select_rfc.fit(X, y )<predict_on_test>
y2_val=x.iloc[:,-1]
y_test = select_rfc.predict(X_test)
 print(y_test )<save_to_csv>
y2_val=y2_val.astype(int )
test_data2 = pd.read_csv('.. /input/titanic/test.csv')
 my_submission = pd.DataFrame({'PassengerId': test_data2.PassengerId,'Survived': y_test})
 my_submission.to_csv('submission.csv', index=False)
 my_submission.head()<load_from_csv>
x_val=x.drop(['ConfirmedCases','Fatalities'],axis=1 )
df_train = pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test = pd.read_csv("/kaggle/input/titanic/test.csv" )<prepare_x_and_y>
import seaborn as sns;
features = ['Sex','Pclass','SibSp','Parch']
X_train = pd.get_dummies(df_train[features])
 X_train['Age'] = df_train['Age']
X_test = pd.get_dummies(df_test[features])
 X_test['Age'] = df_test['Age'] 
<feature_engineering>
z=x_val['Province/State'].isna()
X_train['Age'] = np.where(X_train['Age'].isnull() ,X_train['Age'].median() ,X_train['Age'] )<feature_engineering>
z1=x_test['Province/State'].isna()
X_test['Age']= np.where(X_test['Age'].isnull() ,X_test['Age'].median() ,X_test['Age'] )<prepare_x_and_y>
for i in range(len(z1)) :
if z1[i]==True:
x_test['Province/State'][i]=x_test['Country/Region'][i]
y_train = df_train['Survived']
X_train
X_test<train_model>
x_test.isna().sum()
clf = RandomForestClassifier(max_depth=5,random_state=1)
 clf.fit(X_train,y_train )<predict_on_test>
for i in range(len(z)) :
if z[i]==True:
x_val['Province/State'][i]=x_val['Country/Region'][i]
y_test = clf.predict(X_test )<create_dataframe>
x_val.isnull().sum()
output = pd.DataFrame({'PassengerId':df_test['PassengerId'],'Survived':y_test} )<save_to_csv>
x.drop(['ConfirmedCases','Fatalities'],axis=1,inplace=True )
output.to_csv("titanic_a2.csv",index=False )<import_modules>
x_val.isnull().sum()
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import math 
<load_from_csv>
xx=pd.get_dummies(x_val )
gender_df= pd.read_csv('.. /input/titanic/gender_submission.csv')
 train_df = pd.read_csv('.. /input/titanic/train.csv')
 print(train_df.isnull().sum(axis = 0)) 
<feature_engineering>
x_predfinal=pd.get_dummies(x_test )
si = SimpleImputer(strategy="most_frequent")
 inputerResult = si.fit_transform(train_df[['Embarked']])
 train_df['Embarked'] = inputerResult
si = SimpleImputer(strategy="mean")
 inputerResult = si.fit_transform(train_df[['Age']])
 train_df['Age'] = inputerResult
train_df["Age"] = train_df['Age'].map(lambda x: '{:.2f}'.format(float(x)))
<count_missing_values>
from sklearn.ensemble import BaggingRegressor
print(train_df.isnull().sum(axis = 0)) 
<feature_engineering>
from sklearn import tree
def binAge(row):
if row['Sex'] == 'male':
if float(row['Age'])< 16:
return 'alive'
else:
return 'dead'
else:
return 'itsFemale'
train_df['bin_male_age'] = train_df.apply(binAge, axis=1 )<feature_engineering>
from sklearn.model_selection import train_test_split
def withNanny(row):
if float(row['Parch'])== 0 and float(row['Age'])< 18:
return 'with'
elif float(row['Parch'])!= 0 and float(row['Age'])< 18:
return 'without'
else:
return 'adult'
train_df['withNanny'] = train_df.apply(withNanny, axis=1 )<count_missing_values>
X_train1, X_test1, y_train1, y_test1 = train_test_split(xx, y1_val, test_size=0.2)
 X_train2, X_test2, y_train2, y_test2 = train_test_split(xx, y2_val, test_size=0.2 )
print("Only {}% of data have Cabin number".format(100 - train_df.Cabin.isnull().sum(axis = 0)*100/len(train_df.Cabin)) )<define_variables>
model = BaggingRegressor(tree.DecisionTreeRegressor(random_state=1))
features = [ 'Age', 'SibSp', 'Parch', 'Fare']
cat_features = ['Embarked', 'Sex','Pclass' , 'withNanny']
todo = ['Cabin', 'Ticket']<define_variables>
model.fit(X_train1, y_train1 )
base_model = train_df[features]
cat_df = train_df[cat_features]<categorify>
model.score(X_test1,y_test1 )
data = pd.get_dummies(cat_df, columns=['Embarked'], prefix="Embarked", drop_first=True)
 data = pd.get_dummies(data, columns=['Sex'], prefix="Sex", drop_first=True)
 data = pd.get_dummies(data, columns=['Pclass'], prefix="Pclass", drop_first=True)
 data = pd.get_dummies(data, columns=['withNanny'], prefix="withNanny", drop_first=True )<split>
anssss=model.predict(x_predfinal )
X_train, X_test, y_train, y_test = train_test_split(data, train_df.Survived, test_size = 0.25, random_state = 0 )<train_model>
a1=np.around(anssss )
classifier = SVC(kernel='rbf', random_state = 123)
 classifier.fit(X_train, y_train )<predict_on_test>
a1=a1.astype(int )
y_pred = classifier.predict(X_test )<compute_test_metric>
model2 = BaggingRegressor(tree.DecisionTreeRegressor(random_state=1))
accuracy_score(y_test, y_pred )<load_from_csv>
model2.fit(X_train2, y_train2 )
test_df = pd.read_csv('.. /input/titanic/test.csv')
 print(test_df.isnull().sum(axis = 0)) 
<feature_engineering>
model2.score(X_test2,y_test2 )
si = SimpleImputer(strategy="mean")
 inputerResult = si.fit_transform(test_df[['Age']])
 test_df['Age'] = inputerResult<feature_engineering>
anzzzz=model2.predict(x_predfinal )
test_df['bin_male_age'] = test_df.apply(binAge, axis=1)
 test_df['withNanny'] = test_df.apply(withNanny, axis=1 )<categorify>
a2=np.around(anzzzz )
base_model = test_df[features]
cat_df = test_df[cat_features]
data = pd.get_dummies(cat_df, columns=['Embarked'], prefix="Embarked", drop_first=True)
 data = pd.get_dummies(data, columns=['Sex'], prefix="Sex", drop_first=True)
 data = pd.get_dummies(data, columns=['Pclass'], prefix="Pclass", drop_first=True)
 data = pd.get_dummies(data, columns=['withNanny'], prefix="withNanny", drop_first=True )<predict_on_test>
a2=a2.astype(int )
y_pred = classifier.predict(data )<create_dataframe>
predictions = pd.DataFrame({'ForecastId':x_test['ForecastId'],'ConfirmedCases':a1,'Fatalities':a2})
 predictions.head()
resultDF = pd.DataFrame(index=test_df.index )<prepare_output>
from sklearn.ensemble import AdaBoostRegressor
resultDF['PassengerId'] = pd.Series(test_df['PassengerId'], index=resultDF.index, dtype=int)
 resultDF['Survived'] = pd.Series(y_pred, index=resultDF.index, dtype=int )<feature_engineering>
model3 = AdaBoostRegressor()
model4 = AdaBoostRegressor()
for index, row in test_df.iterrows() :
resultDF['PassengerId'][index] = row.PassengerId<save_to_csv>
model3.fit(X_train1, y_train1 )
resultDF.to_csv('out.csv', index=False )<import_modules>
model4.fit(X_train2, y_train2 )
np.random.seed(0)
 rn.seed(0)
 colors = ["red", "blue", "orange", "pink"]
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
model3.score(X_test1,y_test1 )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<count_missing_values>
model4.score(X_test2,y_test2 )
print("Train data missing value count for each feature")
 print(train_data.isnull().sum())
 print("Test data missing value count for each feature")
 print(test_data.isnull().sum() )<define_variables>
import xgboost as xgb
missing_value_fill_method = "2.3"<count_missing_values>
model5=xgb.XGBRegressor()
if missing_value_fill_method == "2.2":
train_data.fillna(train_data.mean() , inplace=True)
 print(train_data.isnull().sum() )<data_type_conversions>
model6=xgb.XGBRegressor()
if missing_value_fill_method == "2.3":
train_data["Age"] = train_data.groupby(["Pclass", "Sex"])["Age"].apply(lambda x: x.fillna(x.mean()))
test_data["Age"] = test_data.groupby(["Pclass", "Sex"])["Age"].apply(lambda x: x.fillna(x.mean())) 
<feature_engineering>
X_train1.rename(columns = {'Id':'ForecastId'}, inplace = True)
 X_test1.rename(columns = {'Id':'ForecastId'}, inplace = True )
if missing_value_fill_method == "2.3":
train_data["Cabin"] = train_data["Cabin"].apply(lambda s: s[0] if pd.notnull(s)and s[0]!= "T" else "N")
 test_data["Cabin"] = test_data["Cabin"].apply(lambda s: s[0] if pd.notnull(s)and s[0]!= "T" else "N" )<count_values>
X_train2.rename(columns = {'Id':'ForecastId'}, inplace = True)
 X_test2.rename(columns = {'Id':'ForecastId'}, inplace = True )
if missing_value_fill_method == "2.3":
print(train_data.groupby(["Pclass"])["Cabin"].value_counts() )<feature_engineering>
model5.fit(X_train1, y_train1 )
if missing_value_fill_method == "2.3":
train_data.loc[(train_data["Pclass"] == 1)&(train_data["Cabin"] == "N"), "Cabin"]= "C"
train_data.loc[(( train_data["Pclass"] == 2)|(train_data["Pclass"] == 3)) &(train_data["Cabin"] == "N"), "Cabin"]= "F"
test_data.loc[(test_data["Pclass"] == 1)&(test_data["Cabin"] == "N"), "Cabin"]= "C"
test_data.loc[(( test_data["Pclass"] == 2)|(test_data["Pclass"] == 3)) &(test_data["Cabin"] == "N"), "Cabin"]= "F"<filter>
model6.fit(X_train2, y_train2 )
if missing_value_fill_method == "2.3":
print(test_data.loc[test_data["Fare"].isnull() ] )<feature_engineering>
model5.score(X_test1,y_test1 )
if missing_value_fill_method == "2.3":
sample_fare = test_data.loc[(test_data["Pclass"] == 3)&(test_data["SibSp"] == 0)&(test_data["Embarked"] == "S")][
"Fare"].mean()
print(sample_fare)
 test_data.loc[test_data["Fare"].isnull() , "Fare"] = sample_fare<split>
model6.score(X_test2,y_test2 )
if missing_value_fill_method == "2.3":
print(train_data.loc[train_data["Embarked"].isnull() ])
 print(test_data.loc[train_data["Embarked"].isnull() ] )<feature_engineering>
ank=model5.predict(x_predfinal)
 annk=model6.predict(x_predfinal)
 a4=np.around(ank)
 a4=a4.astype(int)
 a5=np.around(annk)
 a5=a5.astype(int)
 predictions2 = pd.DataFrame({'ForecastId':x_test['ForecastId'],'ConfirmedCases':a4,'Fatalities':a5})
 predictions2.to_csv('submission.csv', header=True, index=False)
 predictions2.head()
if missing_value_fill_method == "2.3":
print(train_data.loc[train_data["Embarked"].isnull() ].head())
 print(train_data.loc[(train_data["Pclass"] == 1)&(train_data["Fare"] <= 80.0)&(train_data['Cabin'] == "B")][
"Embarked"].value_counts())
 train_data.loc[train_data["Embarked"].isnull() , "Embarked"] = "S"<count_missing_values>
from sklearn.metrics import mean_squared_error
print(train_data.isnull().sum())
 print(test_data.isnull().sum() )<data_type_conversions>
from sklearn.metrics import mean_squared_error
ser_age, bin_age = pd.cut(train_data["Age"].astype(int), 5, retbins=True, labels=False)
 test_data["Age"] = pd.cut(test_data["Age"], bins=bin_age, labels=False, include_lowest=True)
 train_data["Age"] = ser_age
train_data["Age"] = train_data["Age"].astype("category")
 test_data["Age"] = test_data["Age"].astype("category")
 ser, bins = pd.qcut(train_data["Fare"], 5, retbins=True, labels=False)
 train_data["Fare"] = ser
ser_tst = pd.cut(test_data["Fare"], bins=bins, labels=False, include_lowest=True)
 test_data["Fare"] = ser_tst
train_data["Fare"] = train_data["Fare"].astype("category")
 test_data["Fare"] = test_data["Fare"].astype("category" )<feature_engineering>
y_predmodel5=model5.predict(X_test1 )
<categorify><EOS> 
mean_squared_error(y_test1, y_predmodel5 )