Kaggle competition: Titanic - Machine Learning from Disaster
convert_dict = {'Province_State': str,'Country_Region':str,'ConfirmedCases':int,'Fatalities':int}
convert_dict_test = {'Province_State': str,'Country_Region':str}
train_data = train_data.astype(convert_dict)
 test_data = test_data.astype(convert_dict_test )<data_type_conversions>
train_data_raw = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data_raw.head()
train_data['Date'] = pd.to_datetime(train_data['Date'], infer_datetime_format=True)
 test_data['Date'] = pd.to_datetime(test_data['Date'], infer_datetime_format=True )<data_type_conversions>
train_data=train_data_raw.drop(columns=['PassengerId','Name','Cabin','Ticket'])
 train_data.head()
train_data.loc[:, 'Date'] = train_data.Date.dt.strftime('%m%d')
 train_data.loc[:, 'Date'] = train_data['Date'].astype(int)
 test_data.loc[:, 'Date'] = test_data.Date.dt.strftime('%m%d')
 test_data.loc[:, 'Date'] = test_data['Date'].astype(int )<feature_engineering>
train_data=train_data.loc[pd.notna(train_data.Embarked)]
train_data
train_data['Country_Region'] = np.where(train_data['Province_State'] == 'nan',train_data['Country_Region'],train_data['Province_State']+' '+train_data['Country_Region'])
 test_data['Country_Region'] = np.where(test_data['Province_State'] == 'nan',test_data['Country_Region'],test_data['Province_State']+' '+test_data['Country_Region'])
<drop_column>
age_arr=train_data.Age.values
bool_arr=pd.isna(train_data.Age.values)
 total_age=0
num_age=0
for i in range(len(age_arr)) :
if bool_arr[i]==False:
total_age+=age_arr[i]
num_age+=1
avg_age=(total_age/num_age)
 for i in range(len(age_arr)) :
if bool_arr[i]==True:
age_arr[i]=avg_age
train_data.replace(to_replace=train_data.Age.values,value=age_arr)
 print(train_data.Age.values )
train_data = train_data.drop(columns=['Province_State'])
 test_data = test_data.drop(columns=['Province_State'] )<define_variables>
scaled_age_arr=[round(age/avg_age,2)for age in age_arr]
train_data.Age=train_data.Age.replace(to_replace=train_data.Age.values,value=scaled_age_arr)
 fare_arr=train_data.Fare.values
total_fare=0
num_fare=len(fare_arr)
 for fare in fare_arr:
total_fare+=fare
avg_fare=total_fare/num_fare
scaled_fare_arr=[round(fare/avg_fare,2)for fare in fare_arr]
train_data.Fare=train_data.Fare.replace(to_replace=train_data.Fare.values,value=scaled_fare_arr)
 train_data
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index )<import_modules>
d_Sex={'male':0,'female':1}
d_Embarked={'S':0,'C':1,'Q':2}
train_data.Sex = train_data.Sex.replace(d_Sex)
 train_data.Embarked = train_data.Embarked.replace(d_Embarked)
 train_data.Embarked=train_data.Embarked.astype(int)
 train_data
from sklearn.preprocessing import LabelEncoder<categorify>
input_data=train_data[['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']][:]
target=train_data['Survived'][:]
print(input_data.values)
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train_data['Country_Region'] = label_encoder2.fit_transform(train_data['Country_Region'])
 test_data['Country_Region'] = label_encoder2.transform(test_data['Country_Region'] )<define_variables>
shuffled_indices = np.arange(len(input_data.values)-1)
 np.random.shuffle(shuffled_indices)
 shuffled_inputs = input_data.values[shuffled_indices]
shuffled_targets = target.values[shuffled_indices]
shuffled_inputs
Test_id = test_data.ForecastId<drop_column>
num_train_samples=int(0.8*len(shuffled_inputs))
num_validation_samples=int(len(shuffled_inputs)) -num_train_samples
train_input=shuffled_inputs[:num_train_samples]
train_target=shuffled_targets[:num_train_samples]
validation_input=shuffled_inputs[num_train_samples:]
validation_target=shuffled_targets[num_train_samples:]
train_data.drop(['Id'], axis=1, inplace=True)
 test_data.drop('ForecastId', axis=1, inplace=True )<count_missing_values>
input_size=7
output_size=2
hidden_layer_size=2700
num_hidden_layers=10
model=tf.keras.Sequential()
for i in range(num_hidden_layers):
model.add(tf.keras.layers.Dense(units=hidden_layer_size,activation='relu'))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(units=output_size,activation='softmax'))
learning_rate=0.005
optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.9)
 model.compile(optimizer=optimizer,loss=tf.keras.losses.SparseCategoricalCrossentropy() ,metrics=['accuracy'])
 batch_size=90
epochs=30
early_stopping=tf.keras.callbacks.EarlyStopping(monitor='val_loss',patience=10)
 def scheduler1(epoch, lr):
if epoch < 10:
return lr
elif epoch < 20:
return 0.003
elif epoch < 30:
return 0.002
elif epoch < 50:
return 0.001
else:
return 0.0009
def scheduler2(epoch, lr):
if epoch < 10:
return lr
else:
return lr * tf.math.exp(-0.1)
 def scheduler3(epoch, lr):
if epoch < 10:
return lr
elif epoch < 20:
return lr * tf.math.exp(-0.1)
 else:
return lr * tf.math.exp(-1.0)
 scheduler=scheduler3
lrs=tf.keras.callbacks.LearningRateScheduler(scheduler)
 model.fit(
 x=train_input,
y=train_target,
batch_size=batch_size,
epochs=epochs,
verbose=1,
callbacks=[
lrs
],
validation_data=(validation_input,validation_target)
)
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column>0] )<import_modules>
test_data_raw = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data=test_data_raw.drop(['Name','Ticket','Cabin'],axis='columns')
 test_data.head()
from xgboost.sklearn import XGBRegressor<prepare_x_and_y>
age_arr=test_data.Age.values
bool_arr=pd.isna(test_data.Age.values)
 total_age=0
num_age=0
for i in range(len(age_arr)) :
if bool_arr[i]==False:
total_age+=age_arr[i]
num_age+=1
avg_age=(total_age/num_age)
 for i in range(len(age_arr)) :
if bool_arr[i]==True:
age_arr[i]=avg_age
scaled_age_arr=[round(age/avg_age,2)for age in age_arr]
test_data.Age=test_data.Age.replace(to_replace=test_data.Age.values,value=scaled_age_arr)
 fare_arr=test_data.Fare.values
bool_arr=pd.isna(test_data.Fare.values)
 total_fare=0
num_fare=0
for i in range(len(fare_arr)) :
if bool_arr[i]==False:
total_fare+=fare_arr[i]
num_fare+=1
avg_fare=(total_fare/num_fare)
 for i in range(len(fare_arr)) :
if bool_arr[i]==True:
fare_arr[i]=avg_fare
scaled_fare_arr=[round(fare/avg_fare,2)for fare in fare_arr]
test_data.Fare=test_data.Fare.replace(to_replace=test_data.Fare.values,value=scaled_fare_arr)
 test_data
X_train = train_data[['Country_Region','Date']]
y_train = train_data[['ConfirmedCases', 'Fatalities']]<import_modules>
d_Sex={'male':0,'female':1}
d_Embarked={'S':0,'C':1,'Q':2}
test_data.Sex = test_data.Sex.replace(d_Sex)
 test_data.Embarked = test_data.Embarked.replace(d_Embarked)
 test_data.Embarked=test_data.Embarked.astype(int)
from sklearn.tree import DecisionTreeRegressor<choose_model_class>
test_data.drop(['PassengerId'],axis=1 ).values
tree_regressor1 = DecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=None,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=6967, splitter='best' )<choose_model_class>
probabilities = model.predict(test_data.drop(['PassengerId'],axis=1 ).values)
 predictions=list()
for p in probabilities:
if p[0]>p[1]:
predictions.append(0)
 else:
predictions.append(1)
 print(predictions )
tree_regressor2 = DecisionTreeRegressor(ccp_alpha=0.0, criterion='mse', max_depth=None,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=6967, splitter='best' )<train_model>
output = pd.DataFrame({'PassengerId': test_data.PassengerId.values, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
tree_regressor1.fit(X_train, y_train.ConfirmedCases )<train_model>
with open('my_submission.csv','r')as f:
reader=csv.reader(f)
 for row in reader:
print(row )