Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<filter>
%matplotlib inline
data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 data.describe()
female = df_train[df_train['sex'] == 'Female']
male = df_train[df_train['sex'] == 'Male']<count_values>
columnFilter = ["Survived", "Pclass", "Sex", "Age", "SibSp", "Parch"]
filteredData = data[columnFilter]
filteredTestData = test_data[columnFilter[1:]]
filteredData.head()
white = df_train[df_train['race'] == 'White'].count() [0]
black = df_train[df_train['race'] == 'Black'].count() [0]
amer = df_train[df_train['race'] == 'Amer-Indian-Eskimo'].count() [0]
other = df_train[df_train['race'] == 'Other'].count() [0]
asian = df_train[df_train['race'] == 'Asian-Pac-Islander'].count() [0]
white_income = []
temp = df_train[df_train['race'] == 'White']
white_income.append(temp[temp['income'] == '>50K'].count() [0])
 white_income.append(white-white_income[0])
 black_income = []
temp = df_train[df_train['race'] == 'Black']
black_income.append(temp[temp['income'] == '>50K'].count() [0])
 black_income.append(black-black_income[0])
 amer_income = []
temp = df_train[df_train['race'] == 'Amer-Indian-Eskimo']
amer_income.append(temp[temp['income'] == '>50K'].count() [0])
 amer_income.append(amer-amer_income[0])
 asian_income = []
temp = df_train[df_train['race'] == 'Asian-Pac-Islander']
asian_income.append(temp[temp['income'] == '>50K'].count() [0])
 asian_income.append(asian-asian_income[0])
 other_income = []
temp = df_train[df_train['race'] == 'Other']
other_income.append(temp[temp['income'] == '>50K'].count() [0])
 other_income.append(other-other_income[0] )<filter>
def oneHotEncode(dataToEncode, column):
onehot = pd.get_dummies(dataToEncode[column])
 dataToEncode = dataToEncode.join(onehot)
 dataToEncode = dataToEncode.drop(columns=column)
 return dataToEncode
filteredData = oneHotEncode(filteredData, "Pclass")
 filteredData.head()
white = df_train[df_train['race'] == 'White']
black = df_train[df_train['race'] == 'Black']<filter>
def sex_to_numerical(d):
sex = d["Sex"]
if sex == "male":
return 1
return 0
filteredData['Sex'] = filteredData.apply(sex_to_numerical, axis=1)
 filteredData.head()
over_50k = df_train[df_train['income'] == '>50K']
under_50k = df_train[df_train['income'] == '<=50K']<count_values>
def normalize_age(dataToNormalize):
scaler = MinMaxScaler()
dataToNormalize["Age"] = scaler.fit_transform(dataToNormalize["Age"].values.reshape(-1,1))
return dataToNormalize
filteredData.Age.fillna(( filteredData['Age'].mean()), inplace=True)
 filteredData = normalize_age(filteredData)
 filteredData.head()
df_train['income'].value_counts()<define_variables>
X = filteredData[filteredData.columns[1:]]
y = filteredData[filteredData.columns[0]]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
 X_train = X_train.to_numpy()
X_test = X_test.to_numpy()
y_train = y_train.to_numpy()
y_test = y_test.to_numpy()
base = df_train<define_variables>
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])
 history = model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test))
quantitative_columns = ['age', 'fnlwgt', 'education.num', 'capital.gain', 'capital.loss', 'hours.per.week']
qualitative_columns = ['education', 'marital.status', 'occupation', 'relationship', 'race',
'sex', 'native.country', 'income']<compute_test_metric>
filteredTestData = oneHotEncode(filteredTestData, "Pclass")
 filteredTestData['Sex'] = filteredTestData.apply(sex_to_numerical, axis=1)
 filteredTestData.Age.fillna(( filteredTestData['Age'].mean()), inplace=True)
 filteredTestData = normalize_age(filteredTestData )
def isprivate(value):
if value == 'Private':
return 1
return 0
def catg(value, categories, ordenation = None):
if ordenation is not None:
ordenation = np.arange(0, len(categories))
for pos in ordenation:
if value == categories[pos]:
return pos
return -1
def equals(value, x):
for v in x:
if v == value:
return 1
return 0<create_dataframe>
model.fit(X, y, epochs=100 )
private = pd.DataFrame({'private': base['workclass'].apply(isprivate)} )<count_values>
predictions = model.predict(filteredTestData)
 predictions = np.where(predictions > 0.5, 1, 0)
 predictions
<create_dataframe><EOS> 
predictions = pd.DataFrame(predictions ).rename(columns={0: "Survived"})
 predictions.index.name ="PassengerId"
predictions.index += 892
predictions.to_csv("predictions.csv")
 predictions