Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
%matplotlib inline
%%time
reg_score_list=[]
period=[]
reg=[]
for n in range(3,10):
for region in region_list:
df_temp=final_train[final_train.UniqueRegion==region]
df_temp=df_temp.tail(n ).reset_index()
date=np.arange(1,n+1)
 model=LinearRegression()
X=date.reshape(-1,1)
 Y=df_temp.Delta
model.fit(X,Y)
 reg.append(region)
 reg_score_list.append(model.score(X,Y))
period.append(n)
 score_df=pd.DataFrame({"Region":reg,"Score":reg_score_list, "Period":period})
 for n in range(3,10):
print(f"Average R2 score for {n} days period :{score_df[score_df.Period==n].Score.mean() }")
 plt.figure(figsize=(10,6))
plt.title(f"Distribution of R2 score for {n} days")
 sns.distplot(score_df[score_df.Period==n].Score)
 plt.show() 
<count_unique_values>
gender_submission = pd.read_csv(".. /input/titanic/gender_submission.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 train = pd.read_csv(".. /input/titanic/train.csv" )
print(f"Unique Countries: {len(df_test.Country_Region.unique())}")
 test_dates=list(df_test.Date.unique())
 size_test=len(df_test.Date.unique())
 print(f"Period : {len(df_test.Date.unique())} days")
 print(f"From : {df_test.Date.min() } To : {df_test.Date.max() }")
 print(f"Unique Regions: {df_test.shape[0]/len(df_test.Date.unique())}" )<feature_engineering>
train['FamilySize'] = train['SibSp'] + train['Parch']
test['FamilySize'] = test['SibSp'] + train['Parch']
df_test["UniqueRegion"]=df_test.Country_Region
df_test.UniqueRegion[df_test.Province_State.isna() ==False]=df_test.Province_State+" , "+df_test.Country_Region<drop_column>
f_file=test['PassengerId']
train=train.drop(['Cabin','PassengerId','Name', 'Ticket', 'SibSp', 'Parch'],axis=1)
 test=test.drop(['Cabin','PassengerId','Name', 'Ticket', 'SibSp', 'Parch'],axis=1)
df_test.drop(labels=["ForecastId","Province_State","Country_Region"], axis=1, inplace=True)
 df_test["ConfirmedCases"]=0
df_test["Fatalities"]=0
df_test["NewCases"]=0
df_test["Delta"]=0<prepare_x_and_y>
train['Age'].fillna(train['Age'].mean() , inplace=True)
 train.dropna(inplace=True)
 test['Age'].fillna(test['Age'].mean() , inplace=True)
df_pred=pd.DataFrame(columns=["ConfirmedCases","Fatalities"])
 df_traintest=pd.DataFrame(columns=["Date","ConfirmedCases","Fatalities","UniqueRegion","NewCases","Delta"])
 for region in region_list:
df_temp=final_train[final_train.UniqueRegion==region].reset_index()
n=int(best_n_df[best_n_df.Region==region].N.sum())
 delta_list=np.array(df_temp.tail(n ).Delta ).reshape(-1,1)
 death_rate=df_temp.tail(1 ).Fatalities.sum() /df_temp.tail(1 ).ConfirmedCases.sum()
scaler=MinMaxScaler()
X=np.arange(1,n+1 ).reshape(-1,1)
 Y=scaler.fit_transform(delta_list)
 model=LinearRegression()
model.fit(X,Y)
 df_test_app=app_test[app_test.UniqueRegion==region]
df_temp=pd.concat([df_temp,df_test_app])
 df_temp=df_temp.reset_index()
for i in range(size_train, df_temp.shape[0]):
n=n+1
df_temp.Delta[i]=max(1,scaler.inverse_transform(model.predict(np.array([n] ).reshape(-1,1))))
df_temp.ConfirmedCases[i]=round(df_temp.ConfirmedCases[i-1]*df_temp.Delta[i],0)
 df_temp.Fatalities[i]=round(death_rate*df_temp.ConfirmedCases[i],0)
 df_temp.NewCases[i]=df_temp.ConfirmedCases[i]-df_temp.ConfirmedCases[i-1]
df_traintest=pd.concat([df_traintest,df_temp],ignore_index=True)
 df_temp=df_temp.iloc[-size_test:,:]
df_temp=df_temp[["ConfirmedCases","Fatalities"]]
df_pred=pd.concat([df_pred,df_temp], ignore_index=True)
<prepare_x_and_y>
my_label = LabelEncoder()
train['Sex'] = my_label.fit_transform(train['Sex'])
 train['Embarked'] = my_label.fit_transform(train['Embarked'])
 test['Sex'] = my_label.fit_transform(test['Sex'])
 test['Embarked'] = my_label.fit_transform(test['Embarked'])
 train.head()
df_pred=pd.DataFrame(columns=["ConfirmedCases","Fatalities"])
 df_traintest=pd.DataFrame(columns=["Date","ConfirmedCases","Fatalities","UniqueRegion","NewCases","Delta"])
 for region in region_list:
df_temp=final_train[final_train.UniqueRegion==region].reset_index()
n=10
NewCasesList=df_temp.tail(n ).NewCases
death_rate=df_temp.tail(1 ).Fatalities.sum() /df_temp.tail(1 ).ConfirmedCases.sum()
X=np.arange(1,n+1 ).reshape(-1,1)
 Y=NewCasesList
model=LinearRegression()
model.fit(X,Y)
 df_test_app=app_test[app_test.UniqueRegion==region]
df_temp=pd.concat([df_temp,df_test_app])
 df_temp=df_temp.reset_index()
for i in range(size_train, df_temp.shape[0]):
n=n+1
df_temp.NewCases[i]=round(max(0,model.predict(np.array([n] ).reshape(-1,1)) [0]),0)
 df_temp.ConfirmedCases[i]=df_temp.ConfirmedCases[i-1]+df_temp.NewCases[i]
df_temp.Fatalities[i]=round(death_rate*df_temp.ConfirmedCases[i],0)
 df_temp.Delta[i]=df_temp.ConfirmedCases[i]/df_temp.ConfirmedCases[i-1]
df_traintest=pd.concat([df_traintest,df_temp],ignore_index=True)
 df_temp=df_temp.iloc[-size_test:,:]
df_temp=df_temp[["ConfirmedCases","Fatalities"]]
df_pred=pd.concat([df_pred,df_temp], ignore_index=True)
 df_pred.shape 
<create_dataframe>
train_X, val_X, train_y, val_y = train_test_split(train.drop(['Survived'], axis=1), train['Survived'] )
df_pred=pd.DataFrame(columns=["ConfirmedCases","Fatalities"])
 df_traintest=pd.DataFrame(columns=["Date","ConfirmedCases","Fatalities","UniqueRegion","NewCases","Delta"])
 for region in region_list:
df_temp=final_train[final_train.UniqueRegion==region].reset_index()
n=7
ConfirmedCasesList=df_temp.tail(n ).ConfirmedCases
death_rate=df_temp.tail(1 ).Fatalities.sum() /df_temp.tail(1 ).ConfirmedCases.sum()
polynom=PolynomialFeatures(degree=2)
 X=polynom.fit_transform(np.arange(1,n+1 ).reshape(-1,1))
Y=ConfirmedCasesList
model=LinearRegression()
model.fit(X,Y)
 df_test_app=app_test[app_test.UniqueRegion==region]
df_temp=pd.concat([df_temp,df_test_app])
 df_temp=df_temp.reset_index()
for i in range(size_train, df_temp.shape[0]):
n=n+1
pred=round(model.predict(polynom.fit_transform(np.array(n ).reshape(-1,1)))[0],0)
 df_temp.ConfirmedCases[i]=max(df_temp.ConfirmedCases[i-1],pred)
 df_temp.NewCases[i]=df_temp.ConfirmedCases[i]+df_temp.ConfirmedCases[i-1]
df_temp.Fatalities[i]=round(death_rate*df_temp.ConfirmedCases[i],0)
 df_temp.Delta[i]=df_temp.ConfirmedCases[i]/df_temp.ConfirmedCases[i-1]
df_traintest=pd.concat([df_traintest,df_temp],ignore_index=True)
 df_temp=df_temp.iloc[-size_test:,:]
df_temp=df_temp[["ConfirmedCases","Fatalities"]]
df_pred=pd.concat([df_pred,df_temp], ignore_index=True)
 df_pred.shape 
<save_to_csv>
rf = RandomForestClassifier(random_state=1)
 rf.fit(train_X, train_y)
 prediction = rf.predict(val_X)
 print(mean_absolute_error(val_y, prediction), accuracy_score(val_y, prediction))
df_sub.ConfirmedCases=df_pred.ConfirmedCases
df_sub.Fatalities=df_pred.Fatalities 
<load_from_csv>
XGB = XGBClassifier()
XGB.fit(train_X, train_y)
 prediction = XGB.predict(val_X)
 print(mean_absolute_error(val_y, prediction), accuracy_score(val_y, prediction))
train_df = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/test.csv" )<count_missing_values>
test['Fare'].fillna(test['Fare'].mean() , inplace=True )
train_df.apply(lambda col: col.isnull().value_counts() , axis=0 )<count_missing_values>
vals = rf.predict(test)
 file = pd.DataFrame({'PassengerId':f_file, 'Survived':vals})
 file.to_csv('submission_rf.csv', index = False)
 file.head()
<data_type_conversions><EOS> 
vals = XGB.predict(test)
 file = pd.DataFrame({'PassengerId':f_file, 'Survived':vals})
 file.to_csv('submission_xgb.csv', index = False)
 file.head()