Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
%matplotlib inline
def transform_dataset(ds):
transformed_dataset = ds.copy()
transformed_dataset['Age'].fillna(transformed_dataset['Age'].median() , inplace=True)
 transformed_dataset['Fare'].fillna(transformed_dataset['Fare'].median() , inplace=True)
 transformed_dataset['Sex'] = pd.factorize(transformed_dataset['Sex'])[0]
transformed_dataset=pd.concat([transformed_dataset, pd.get_dummies(transformed_dataset['Embarked'], prefix="Embarked")], axis=1)
 transformed_dataset['Childs'] = transformed_dataset.apply(lambda row: get_childs(row), axis=1)
 transformed_dataset['Parents'] = transformed_dataset.apply(lambda row: get_parents(row), axis=1)
 transformed_dataset = transformed_dataset.drop(['PassengerId', 'Name', 'Ticket', 'Cabin', 'Embarked', 'Parch'], axis=1)
 return transformed_dataset
def get_childs(row):
if(row.Parch>2 or row.Age>=18):
return row.Parch
return 0
def get_parents(row):
if(row.Parch <= 2 and row.Age<18):
return row.Parch
return 0
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train = pd.read_csv('.. /input/covid19-global-forecasting-week-4/train.csv')
 test = pd.read_csv('.. /input/covid19-global-forecasting-week-4/test.csv')
 Y1=train['ConfirmedCases']
Y2=train['Fatalities']<data_type_conversions>
dataset = pd.read_csv('.. /input/titanic/train.csv')
 dataset.info()
train['Complete_Date'] = train['Date'].astype('datetime64[ns]')
 test['Complete_Date'] = test['Date'].astype('datetime64[ns]')
 month = [int(el[5:7])for el in list(train['Date'].values)]
day = [int(el[8:10])for el in list(train['Date'].values)]
month_test = [int(el[5:7])for el in list(test['Date'].values)]
day_test = [int(el[8:10])for el in list(test['Date'].values)]
df_month= pd.DataFrame(month, columns= ['Month'])
 df_day= pd.DataFrame(day, columns= ['Day'])
 df_month_test= pd.DataFrame(month_test, columns= ['Month'])
 df_day_test= pd.DataFrame(day_test, columns= ['Day'])
 train=pd.concat([train, df_month], axis=1)
 test=pd.concat([test, df_month_test], axis=1)
 train=pd.concat([train, df_day], axis=1)
 test=pd.concat([test, df_day_test], axis=1)
 train['Date']=train['Month']*100+train['Day']
test['Date']=test['Month']*100+test['Day']<prepare_x_and_y>
transformed_dataset = transform_dataset(dataset)
 transformed_dataset.info()
train['Province_State'].fillna('',inplace=True)
 test['Province_State'].fillna('',inplace=True)
 train['Province_State']=train['Province_State'].astype(str)
 test['Province_State']=test['Province_State'].astype(str)
 y= train['Country_Region']+train['Province_State']
y= pd.DataFrame(y, columns= ['Place'])
 y_test= test['Country_Region']+test['Province_State']
y_test= pd.DataFrame(y_test, columns= ['Place'])
 train=pd.concat([train, y], axis=1)
 test=pd.concat([test, y_test], axis=1 )<data_type_conversions>
X = transformed_dataset.drop(['Survived'], axis=1)
 Y = dataset['Survived']
Country_df=train["Place"]
ConfirmedCases_df=train["ConfirmedCases"]
Country_df.to_numpy()
ConfirmedCases_df.to_numpy()
Country=Country_df[0]
NbDay = pd.DataFrame(columns=['NbDay'])
 day=0
count=0
for x in train["Month"]:
if(ConfirmedCases_df[count]==0):
NbDay = NbDay.append({'NbDay': int(0)}, ignore_index=True)
 count=count+1
else:
if(Country_df[count]==Country):
day=day+1
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
else:
Country=Country_df[count]
day=1
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
train=pd.concat([train, NbDay], axis=1)
 NbDay_test_array=np.zeros(test.shape[0])
 i=0
df=test["Place"]
Place_array=df.to_numpy()
for t in test.Date:
place=Place_array[i]
if t==402:
row=train.loc[(train['Place'] == place)&(train['Date'] ==t)]
row=row.to_numpy()
NbDay_test_array[i]= row[0][10]
else:
NbDay_test_array[i]=0
i=i+1
NbDay=pd.DataFrame(NbDay_test_array, columns=['NbDay1'])
 test=pd.concat([test,NbDay], axis=1)
 Country_df=test["Place"]
NbDay_df=test['NbDay1']
Country_df.to_numpy()
day_array=NbDay_df.to_numpy()
Country=Country_df[0]
NbDay = pd.DataFrame(columns=['NbDay'])
 day=0
count=0
for t in test["Date"]:
if(t==402):
day=day_array[count]
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
else:
day=day+1
NbDay = NbDay.append({'NbDay': int(day)}, ignore_index=True)
 count=count+1
test=pd.concat([test,NbDay], axis=1 )<drop_column>
X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size = 0.2, random_state = 25 )
train=train[['Place','Country_Region','NbDay','ConfirmedCases','Fatalities']]
test=test[['Place','Country_Region','NbDay','ForecastId']]<count_unique_values>
model = tree.DecisionTreeClassifier(max_depth=6,random_state=25 )
country_array=train['Place'].to_numpy()
def distinct_values(country_array):
liste=[]
liste.append(country_array[0])
 for i in range(1,len(country_array)) :
if country_array[i]!=country_array[i-1]:
liste.append(country_array[i])
 return liste
Countries_liste=distinct_values(country_array)
 len(Countries_liste )<categorify>
model.fit(X_train,Y_train)
 print('train score = ', model.score(X_train,Y_train), '
test score = ', model.score(X_test,Y_test))
def exponentiate_alpha(column,v):
array=column.to_numpy()
string='NbDay'+str(v)
 array=np.power(v,array)
 frame=pd.DataFrame(array, columns=[string])
 return frame
def product(column1,column2,number):
array=column1.to_numpy()
array2=column2.to_numpy()
string='Product'+str(number)
 array=np.multiply(array,array2)
 frame=pd.DataFrame(array, columns=[string])
 return frame<normalization>
test = pd.read_csv('.. /input/titanic/test.csv')
 test.info()
df1=exponentiate_alpha(train['NbDay'],1.0001 )<concatenate>
passenger_id = test['PassengerId']
df2=product(df1,train['NbDay'],1 )<feature_engineering>
transformed_test = transform_dataset(test)
 transformed_test.info()
train['NbDay_exp']=df1
train['Product']=df12<choose_model_class>
Y_predict = model.predict(transformed_test)
 Y_p = pd.DataFrame(Y_predict, columns=['Survived'])
 Y_p
model = xgboost.XGBRegressor(colsample_bytree=0.4,
gamma=0,
learning_rate=0.07,
max_depth=5,
min_child_weight=1.5,
n_estimators=10000,
reg_alpha=0.75,
reg_lambda=0.45,
subsample=0.6,
seed=42,
objective='reg:squarederror',
eval_metric='rmse' )<predict_on_test>
res = pd.concat([passenger_id, Y_p], axis=1)
 res
ConfirmedCasesPredictions=[]
i=1
for country in Countries_liste:
train_=train[train['Place']==country][['NbDay']]
y_=train[train['Place']==country]['ConfirmedCases']
train_=train_.astype(float)
 test_=test[test['Place']==country][['NbDay']]
test_=test_.astype(float)
 model.fit(train_, y_)
 y_pred = model.predict(test_)
 y_pred = list(y_pred)
 ConfirmedCasesPredictions+=y_pred
i=i+1 
<create_dataframe>
res.to_csv('res.csv', index=None )
ConfirmedCases=np.array(ConfirmedCasesPredictions)
 ConfirmedCases=pd.DataFrame(ConfirmedCases, columns=['ConfirmedCases'])
 test['ConfirmedCases']=ConfirmedCases<predict_on_test>
eli5.explain_weights_sklearn(model, feature_names=X_train.columns.values )