Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
sns.set()
fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred<prepare_output>
test = pd.read_csv(".. /input/titanic/test.csv")
 train = pd.read_csv(".. /input/titanic/train.csv")
 data_cleaner = [train, test]
submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred<save_to_csv>
for data in data_cleaner:
print(data.isnull().sum())
 print('
' )
submission.to_csv("submission.csv" , index = False )<load_from_csv>
age_ref = pd.DataFrame(data=[train.groupby('Pclass')['Age'].mean() ],columns=train['Pclass'].unique())
 age_ref
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )<drop_column>
def fill_age(pclass,age):
if pd.isnull(age):
return float(age_ref[pclass])
 else:
return age
for data in data_cleaner:
data['Age'] = train.apply(lambda x: fill_age(x['Pclass'],x['Age']), axis=1 )
train_df['Province_State'].fillna("",inplace = True)
 test_df['Province_State'].fillna("",inplace = True)
 train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']<feature_engineering>
def fill_fare(fare):
if pd.isnull(fare):
return train['Fare'].mean()
else:
return fare
def fill_embark(embark):
if pd.isnull(embark):
return train['Embarked'].mode().iloc[0]
else:
return embark
for data in data_cleaner:
data['Fare'] = train.apply(lambda x: fill_fare(x['Fare']), axis=1)
 data['Embarked'] = train.apply(lambda x: fill_embark(x['Embarked']), axis=1 )
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )<feature_engineering>
for data in data_cleaner:
data.drop(['Cabin'],axis=1,inplace=True )
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )<feature_engineering>
for data in data_cleaner:
print(data.isnull().sum())
 print('
' )
train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']<feature_engineering>
title_list = list()
for data in data_cleaner:
for title in data['Name']:
title = title.split('.')[0].split(',')[1]
title_list.append(title)
 data['Title'] = title_list
title_list = list()
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )<feature_engineering>
for data in data_cleaner:
print(data['Title'].value_counts())
 print('
' )
test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']<train_model>
train['Title'] = train['Title'].replace([ ' Don', ' Rev', ' Dr', ' Mme',' Ms', ' Major', ' Lady', ' Sir', ' Mlle', ' Col', ' Capt',
' the Countess', ' Jonkheer'], 'Others')
 train['Title'].value_counts()
rf = XGBRegressor(n_estimators = 1600 , random_state = 0 , max_depth = 15)
 rf.fit(x_train,cases)
 cases_pred = rf.predict(x_test)
 cases_pred = np.around(cases_pred,decimals = 0)
 x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]
x_train_cas = np.array(x_train_cas)
 rf = XGBRegressor(n_estimators = 1600 , random_state = 0 , max_depth = 15)
 rf.fit(x_train_cas,fatalities)
 x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]
x_test_cas = np.array(x_test_cas)
 fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred = np.around(fatalities_pred,decimals = 0)
 submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred
submission.to_csv("submission.csv" , index = False )<set_options>
test['Title'] = test['Title'].replace([ ' Don', ' Rev', ' Dr', ' Mme',' Ms', ' Major', ' Lady', ' Sir', ' Mlle', ' Col', ' Capt',
' the Countess', ' Jonkheer',' Dona'], 'Others')
 test['Title'].value_counts()
plt.style.use('fivethirtyeight')
 le = preprocessing.LabelEncoder()
warnings.filterwarnings('ignore' )<import_modules>
def get_size(df):
if df['SibSp'] + df['Parch'] + 1 == 1:
return 'Single'
if df['SibSp'] + df['Parch'] + 1 > 1:
return 'Small'
if df['SibSp'] + df['Parch'] + 1 > 4:
return 'Big'
for data in data_cleaner:
data['FamilySize'] = data.apply(get_size,axis=1)
 for data in data_cleaner:
data['IsAlone'] = 1
data['IsAlone'].loc[data['FamilySize'] != 'Single'] = 0
import plotly.io as pio
import plotly.express as px
import plotly.graph_objects as go 
<load_from_csv>
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True)
 title = pd.get_dummies(train['Title'],drop_first=True)
 Pclass = pd.get_dummies(train['Pclass'],drop_first=True)
 FamilySize = pd.get_dummies(train['FamilySize'],drop_first=True)
 sex2 = pd.get_dummies(test['Sex'],drop_first=True)
 embark2 = pd.get_dummies(test['Embarked'],drop_first=True)
 title2 = pd.get_dummies(test['Title'],drop_first=True)
 Pclass2 = pd.get_dummies(test['Pclass'],drop_first=True)
 FamilySize2 = pd.get_dummies(test['FamilySize'],drop_first=True)
 for data in data_cleaner:
data.drop(['Sex','Embarked','Name','Ticket','PassengerId','Title','FamilySize'],axis=1,inplace=True)
 train = pd.concat([sex,embark,train,title,FamilySize],axis=1)
 test = pd.concat([sex2,embark2,test,title2,FamilySize2],axis=1 )
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )<count_missing_values>
X = train.drop('Survived',axis=1)
 y = train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=101 )
display(train_df.isnull().sum() /len(train_df)*100)
 display(test_df.isnull().sum() /len(test_df)*100 )<rename_columns>
scaler = MinMaxScaler()
scaler.fit(X_train)
 scaler.transform(X_train)
 scaler.transform(X_test)
 scaler.transform(test )
train_df.rename(columns={'Province_State':'State','Country_Region':'Country'}, inplace=True)
 test_df.rename(columns={'Province_State':'State','Country_Region':'Country'}, inplace=True )<compute_test_metric>
logistic_model = LogisticRegression()
logistic_model.fit(X_train, y_train)
 y_pred = logistic_model.predict(X_test )
def missings(state, country):
return country if pd.isna(state)== True else state<feature_engineering>
print(classification_report(y_test,y_pred))
print('
')
 print(confusion_matrix(y_test,y_pred))
<count_unique_values><EOS> 
predictions = logistic_model.predict(test)
 pred_list = [int(x)for x in predictions]
test2 = pd.read_csv(".. /input/titanic/test.csv")
 output = pd.DataFrame({'PassengerId': test2['PassengerId'], 'Survived': pred_list})
 output.to_csv('Titanic_with_logistic.csv', index=False )