Kaggle competition: Titanic - Machine Learning from Disaster
x_test_txt = open(".. /input/x_test.txt" ).read().splitlines()
x_test_idx = [np.array([char_vocab.token2idx[c] if c in char_vocab.token2idx else unk_index for c in line])for line in x_test_txt]
test_data = [(x, idx)for idx, x in enumerate(x_test_idx)]<train_model>
%matplotlib inline
warnings.filterwarnings('ignore')
 train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 PassengerId=test['PassengerId']
all_data = pd.concat([train, test], ignore_index = True )
index, labels = test(model, test_data, batch_size, token_size)
 order = np.argsort(index)
 labels = labels[order]<save_to_csv>
all_data['Title'] = all_data['Name'].apply(lambda x:x.split(',')[1].split('.')[0].strip())
 Title_Dict = {}
Title_Dict.update(dict.fromkeys(['Capt', 'Col', 'Major', 'Dr', 'Rev'], 'Officer'))
Title_Dict.update(dict.fromkeys(['Don', 'Sir', 'the Countess', 'Dona', 'Lady'], 'Royalty'))
Title_Dict.update(dict.fromkeys(['Mme', 'Ms', 'Mrs'], 'Mrs'))
Title_Dict.update(dict.fromkeys(['Mlle', 'Miss'], 'Miss'))
Title_Dict.update(dict.fromkeys(['Mr'], 'Mr'))
Title_Dict.update(dict.fromkeys(['Master','Jonkheer'], 'Master'))
all_data['Title'] = all_data['Title'].map(Title_Dict)
 sns.barplot(x='Title', y='Survived', data=all_data )
with open('submission.csv', 'w')as f:
print('Id,Language', file=f)
 for sentence_id, lang_id in enumerate(labels):
language = lang_vocab.idx2token[lang_id]
if sentence_id < 10:
print(f'{sentence_id},{language}')
 print(f'{sentence_id},{language}', file=f )<set_options>
all_data[all_data['Embarked'].isnull() ]
%matplotlib inline
plt.style.use('seaborn-whitegrid')
 warnings.filterwarnings('ignore' )<load_from_csv>
all_data[all_data['Fare'].isnull() ]
train_data = pd.read_csv('.. /input/challenge-gh/TrainData.csv', encoding='ISO-8859-1',sep=';')
 test_data = pd.read_csv('.. /input/challenge-gh/TestData.csv', encoding='ISO-8859-1',sep=';')
 submission_data = pd.read_csv('.. /input/challenge-gh/Lsungstemplate.csv', encoding='ISO-8859-1' )<categorify>
def info_fill(all_data):
all_data['Embarked'] = all_data['Embarked'].fillna('C')
 fare=all_data[(all_data['Embarked'] == "S")&(all_data['Pclass'] == 3)].Fare.median()
all_data['Fare']=all_data['Fare'].fillna(fare)
 age_df = all_data[['Age', 'Pclass', 'Sex', 'Title', 'SibSp', 'Parch']]
age_df=pd.get_dummies(age_df)
 known_age = age_df[age_df.Age.notnull() ].values
unknown_age = age_df[age_df.Age.isnull() ].values
y = known_age[:, 0]
X = known_age[:, 1:]
rfr = RandomForestRegressor(random_state=0, n_estimators=100, n_jobs=-1)
 rfr.fit(X, y)
 predictedAges = rfr.predict(unknown_age[:, 1::])
 all_data.loc[(all_data.Age.isnull()), 'Age' ] = predictedAges
return all_data
df_train = train_data.replace('Unbekannt', np.nan, inplace=False )<categorify>
def TrainPreprocess(train):
cols=['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Title', 'Fare', 'Embarked']
train = train[cols]
train_linear = train[['Age', 'Fare']]
train['Sex'] = train['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 train['Embarked'] = train['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train['Title'] = train['Title'].map({'Officer': 0, 'Royalty': 1, 'Mrs': 2, 'Miss': 3,
'Mr': 4, 'Master':5} ).astype(int)
 train_non_linear = train[['Pclass', 'Sex', 'SibSp', 'Parch', 'Title', 'Embarked']]
train_non_linear = pd.get_dummies(train_non_linear)
 features = pd.concat([train_linear, train_non_linear], axis=1)
 features = features.values
mean = features.mean(axis=0)
 features -= mean
std = features.std(axis=0)
 features /= std
label = train['Survived'].values
return features, label
def encode_categorical_features(df):

 lb_make = LabelEncoder()
obj_df = df.select_dtypes(include=['object'] ).copy()
cat_col_names=obj_df.columns.tolist()
for name in cat_col_names:
df[name] = lb_make.fit_transform(df[name])
 return df<train_model>
def TestPreprocess(test):
cols=['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Title', 'Fare', 'Embarked']
test = test[cols]
test_linear = test[['Age', 'Fare']]
test['Sex'] = test['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 test['Embarked'] = test['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test['Title'] = test['Title'].map({'Officer': 0, 'Royalty': 1, 'Mrs': 2, 'Miss': 3,
'Mr': 4, 'Master':5} ).astype(int)
 test_non_linear = test[['Pclass', 'Sex', 'SibSp', 'Parch', 'Title', 'Embarked']]
test_non_linear = pd.get_dummies(test_non_linear)
 test = pd.concat([test_linear, test_non_linear], axis=1)
 features = test.values
mean = features.mean(axis=0)
 features -= mean
std = features.std(axis=0)
 features /= std
return features
<categorify>
batchsz = 20
_lambda = 0.1
columns_to_drop=columns_to_drop = ['Stammnummer', 'Anruf-ID','Tage seit letzter Kampagne','Ergebnis letzte Kampagne','Tag','Alter','Kontostand','Anzahl der Ansprachen','Anzahl Kontakte letzte Kampagne']
df_train=df_train.drop(columns_to_drop,axis=1)
 df_train = df_train.apply(lambda x: x.fillna(x.value_counts().index[0]))
df_train=encode_categorical_features(df_train )<normalization>
def build_network() :
network = Sequential([
layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(_lambda)) ,
layers.Dropout(rate=0.5),
layers.Dense(32, activation='relu', kernel_regularizer=regularizers.l2(_lambda)) ,
layers.Dropout(rate=0.5),
layers.Dense(1, activation='sigmoid')
 ])
 network.build(input_shape=(batchsz, 8))
network.compile(optimizer=optimizers.Adam(lr=0.001),loss=losses.BinaryCrossentropy(from_logits=False), metrics='accuracy')
 network.summary()
return network
df_train[['Dauer']] = df_train[['Dauer']].apply(np.sqrt)
 scaler = MinMaxScaler(feature_range=(0, 1))
df_train[['Dauer']] = scaler.fit_transform(df_train[['Dauer']])
 train_features_all = df_train.drop(['Zielvariable'], axis=1, errors='ignore')
 train_labels_all =df_train.Zielvariable<train_on_grid>
train = all_data[0:891]
test = all_data[891:len(all_data)]
x_train, y_train = TrainPreprocess(train)
 x_test = TestPreprocess(test)
 db_train = tf.data.Dataset.from_tensor_slices(( x_train, y_train))
db_train = db_train.shuffle(1000 ).batch(batchsz)
 network = build_network()
network.fit(db_train, epochs=300)
 y_test = network.predict(x_test)
 print(y_test)
 with open('survival_rate.csv','w+',newline='')as f:
csv_file = csv.writer(f)
 csv_file.writerows(y_test)
 my_prediction = []
for i in range(len(y_test)) :
my_prediction.append(1 if(y_test[i] > 0.5)else 0)
 my_submission = pd.DataFrame({'PassengerId': PassengerId, 'Survived': my_prediction})
 my_submission.to_csv('my_submission.csv', index=False, sep=',' )