Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.3f}'.format
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1 )<data_type_conversions>
dataframe = pd.read_csv(".. /input/train.csv", sep=",")
 dataframe.head(10)
category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes<categorify>
print('null value summary:')
 dataframe.isnull().sum().sort_values(ascending=False)
encoder = OneHotEncoder()<categorify>
dataframe = dataframe.drop(columns=['PassengerId', 'Name', 'Ticket', 'Cabin', 'Fare'])
X_data = encoder.fit_transform(category_df )<concatenate>
def transform_sex(dataframe):
column_name = 'Sex'
genders = {"female": 0, "male": 1}
return dataframe[column_name].map(genders)
X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()<prepare_x_and_y>
column_name = 'Sex'
dataframe[column_name] = transform_sex(dataframe)
 dataframe.head(10)
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Beauty'].price )<define_variables>
def transform_embarked(dataframe):
column_name = 'Embarked'
dataframe[column_name].fillna('S')
 dataframe = pd.concat(
 [
dataframe,
pd.get_dummies(dataframe[column_name], prefix=column_name)
 ],
axis=1)
 dataframe = dataframe.drop(columns=[column_name, 'Embarked_Q'])
 return dataframe
category_index = category_df.index<count_values>
column_name = 'Embarked'
dataframe = transform_embarked(dataframe)
 dataframe.head(10)
train_category = train_df.split_category_0.value_counts() ['Beauty']<filter>
def transform_age(dataframe):
column_name = 'Age'
column_mean = dataframe[column_name].median()
dataframe[column_name] = dataframe[column_name].fillna(column_mean)
 dataframe.loc[dataframe[column_name] < 11, column_name] = 0
dataframe.loc[(dataframe[column_name] >= 11)&(dataframe[column_name] < 21), column_name] = 1
dataframe.loc[(dataframe[column_name] >= 21)&(dataframe[column_name] < 31), column_name] = 2
dataframe.loc[(dataframe[column_name] >= 31)&(dataframe[column_name] < 41), column_name] = 3
dataframe.loc[(dataframe[column_name] >= 41)&(dataframe[column_name] < 51), column_name] = 4
dataframe.loc[(dataframe[column_name] >= 51)&(dataframe[column_name] < 61), column_name] = 5
dataframe.loc[dataframe[column_name] >= 61, column_name] = 6
dataframe[column_name] = dataframe[column_name].astype(int)
 return dataframe
submit_index = category_index[train_category:]<split>
column_name = 'Age'
dataframe = transform_age(dataframe)
 dataframe.head(10)
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
def transform_pclass(dataframe):
column_name = 'Pclass'
dataframe = pd.concat(
 [
dataframe,
pd.get_dummies(dataframe[column_name], prefix=column_name)
 ],
axis=1)
 dataframe = dataframe.drop(columns=[column_name, 'Pclass_2'])
 return dataframe
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
column_name = 'Pclass'
dataframe = transform_pclass(dataframe)
 dataframe.head(10)
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
def transform_relatives(dataframe):
column_name = 'Relatives'
dataframe[column_name] = dataframe['SibSp'] + dataframe['Parch']
dataframe = dataframe.drop(columns=['SibSp', 'Parch'])
 dataframe[column_name] = dataframe[column_name].astype(int)
 dataframe.loc[dataframe[column_name] >= 6, column_name] = 6
return dataframe
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df<filter>
column_name = 'Relatives'
dataframe = transform_relatives(dataframe)
 dataframe.head(10)
category_df = train_test_df[train_test_df.split_category_0 == 'Kids']
category_df.head()<feature_engineering>
corr.style.background_gradient()
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
minimal_features = [
"Sex", "Pclass_3"
]
X = dataframe[minimal_features]
y = dataframe['Survived']
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes<categorify>
def preprocessing_data(source):
columns = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'Fare']
target = source.drop(columns=columns)
 column_name = 'Sex'
target[column_name] = transform_sex(target)
 column_name = 'Embarked'
target = transform_embarked(target)
 column_name = 'Age'
target = transform_age(target)
 column_name = 'Pclass'
target = transform_pclass(target)
 column_name = 'Relatives'
target = transform_relatives(target)
 return target
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Kids'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Kids']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
print('LinearRegression: ')
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
 linreg = LinearRegression()
linreg.fit(X_train, y_train)
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
y_pred = linreg.predict(X_test)
 y_pred
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
def transform_predicted(dataframe, threshold = 0.5):
column_name = 'Predicted'
dataframe.loc[dataframe[column_name] >= threshold, column_name] = 1
dataframe.loc[dataframe[column_name] < threshold, column_name] = 0
dataframe[column_name] = dataframe[column_name].astype(int)
 return dataframe
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df<string_transform>
threshold = 0.5
y_pred2 = pd.DataFrame({'Predicted': y_pred})
 y_pred2 = transform_predicted(y_pred2, threshold)
 y_pred2.head(10)
category_df = train_test_df[train_test_df.split_category_0 == 'Electronics']
category_df.head()<feature_engineering>
y_test2 = pd.DataFrame({'Survived': y_test})
 y_test2.head(10)
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
cm = confusion_matrix(y_test2['Survived'], y_pred2['Predicted'])
 print(cm)
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes<categorify>
def display_cm_report(cm):
TN = cm[0][0]
FP = cm[0][1]
FN = cm[1][0]
TP = cm[1][1]
print()
print("TP: {}".format(TP))
print("FP: {}".format(FP))
print("FN: {}".format(FN))
print("TN: {}
".format(TN))
accuracy =(TP + TN)/(TP + TN + FP + FN)
 precision = TP /(TP + FP)
 recall = TP /(TP + FN)
 f1_score = 2 *(precision * recall)/(precision + recall)
 print("accuracy: {}".format(accuracy))
print("precision: {}".format(precision))
print("recall: {}".format(recall))
print("f1_score: {}
".format(f1_score))
plt.clf()
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Wistia)
 plt.ylabel('True label')
 plt.xlabel('Predicted label')
 classNames = ['Negative', 'Positive']
tick_marks = np.arange(len(classNames))
plt.xticks(tick_marks, classNames)
 plt.yticks(tick_marks, classNames)
 s = [['TN', 'FP'], ['FN', 'TP']]
for i in range(2):
for j in range(2):
plt.text(j, i, str(s[i][j])+ " = " + str(cm[i][j]))
plt.show()
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Electronics'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Electronics']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
test_dataframe = pd.read_csv(".. /input/test.csv", sep=",")
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
test_csv = pd.DataFrame()
test_csv['PassengerId'] = test_dataframe['PassengerId']
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
test_dataframe = preprocessing_data(test_dataframe)
 test_dataframe = test_dataframe[minimal_features]
test_dataframe.head(10)
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df<filter>
y_pred = linreg.predict(test_dataframe)
 threshold = 0.5
y_pred2 = pd.DataFrame({'Predicted': y_pred})
 y_pred2 = transform_predicted(y_pred2, threshold)
 y_pred2.head(10)
category_df = train_test_df[train_test_df.split_category_0 == 'Men']
category_df.head()<feature_engineering>
y_test = pd.read_csv(".. /input/gender_submission.csv", sep=",")
 cm = confusion_matrix(y_test['Survived'], y_pred2['Predicted'])
 print(cm)
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
test_csv['Survived'] = y_pred2['Predicted']
test_csv.to_csv('submission_all_lin_0.5.csv', index=False)
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes<categorify>
print('RandomForestClassifier: ')
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
 forest_model = RandomForestClassifier(n_estimators=100, criterion='entropy', random_state=1)
 forest_model.fit(X_train, y_train)
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Men'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Men']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
y_pred = forest_model.predict(X_test)
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
threshold = 0.5
y_pred2 = pd.DataFrame({'Predicted': y_pred})
 y_pred2 = transform_predicted(y_pred2)
 y_pred2.head(10)
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
y_test2 = pd.DataFrame({'Survived': y_test})
 y_test2.head(10)
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df<filter>
cm = confusion_matrix(y_test2['Survived'], y_pred2['Predicted'])
 print(cm)
category_df = train_test_df[train_test_df.split_category_0 == 'Home']
category_df.head()<feature_engineering>
y_pred = forest_model.predict(test_dataframe)
 threshold = 0.5
y_pred2 = pd.DataFrame({'Predicted': y_pred})
 y_pred2 = transform_predicted(y_pred2)
 y_pred2.head(10)
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
y_test = pd.read_csv(".. /input/gender_submission.csv", sep=",")
 cm = confusion_matrix(y_test['Survived'], y_pred2['Predicted'])
 print(cm)
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes
category_df.head()<categorify>
test_csv['Survived'] = y_pred2['Predicted']
test_csv.to_csv('submission_all_tree_0.5.csv', index=False)
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Home'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Home']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
print('MLPRegressor: ')
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
 mlpr_model = MLPRegressor()
mlpr_model.fit(X_train, y_train)
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
y_pred = mlpr_model.predict(X_test)
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
threshold = 0.5
y_pred2 = pd.DataFrame({'Predicted': y_pred})
 y_pred2 = transform_predicted(y_pred2)
 y_pred2.head(10)
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df<filter>
y_test2 = pd.DataFrame({'Survived': y_test})
 y_test2.head(10)
category_df = train_test_df[train_test_df.split_category_0 == 'Vintage & Collectibles']
category_df.head()<feature_engineering>
cm = confusion_matrix(y_test2['Survived'], y_pred2['Predicted'])
 print(cm)
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
y_pred = mlpr_model.predict(test_dataframe)
 threshold = 0.5
tansform_survived = np.vectorize(lambda x: 1 if x >= threshold else 0)
 y_pred2 = tansform_survived(y_pred)
 y_pred2 = pd.DataFrame({'Predicted': y_pred2})
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes
category_df.head()<categorify>
y_test = pd.read_csv(".. /input/gender_submission.csv", sep=",")
 cm = confusion_matrix(y_test['Survived'], y_pred2['Predicted'])
 print(cm)
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Vintage & Collectibles'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Vintage & Collectibles']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
test_csv['Survived'] = y_pred2['Predicted']
test_csv.to_csv('submission_all_nn_0.5.csv', index=False)
ridge = Ridge(alpha=3.1)
 ridge.fit(X_train, y_train )<predict_on_test>
print('Keras NN: ')
 nn = Sequential()
nn.add(
 Dense(
 units=3, kernel_initializer='uniform', activation='relu', input_dim=2))
nn.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))
nn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
 nn.fit(X_train, y_train, batch_size=10, epochs=100)
price = np.expm1(ridge.predict(X_submit))<create_dataframe>
y_pred = nn.predict(X_test)
tmp_df = pd.DataFrame(submit_index)
 tmp_df = pd.concat([tmp_df, pd.DataFrame(price)], axis=1)
 tmp_df.columns = ['test_id', 'price']
submit_df = pd.concat([submit_df, tmp_df], axis=0)
 submit_df<filter>
threshold = 0.5
tansform_survived = np.vectorize(lambda x: 1 if x >= threshold else 0)
 y_pred2 = tansform_survived(y_pred)
 y_pred2 = pd.DataFrame({'Predicted': y_pred2[:,0]})
 y_pred2.head(10)
category_df = train_test_df[train_test_df.split_category_0 == 'Other']
category_df.head()<feature_engineering>
y_test2 = pd.DataFrame({'Survived': y_test})
 y_test2.head(10)
name = category_df.name.values
item_desc = category_df.item_description.values
vectorizer = TfidfVectorizer(max_df=0.85, min_df=2, ngram_range=(1,2), max_features=100000)
 name_vecs = vectorizer.fit_transform(name)
 item_desc_vecs = vectorizer.fit_transform(item_desc )<data_type_conversions>
cm = confusion_matrix(y_test2['Survived'], y_pred2['Predicted'])
 print(cm)
category_df = category_df.drop(['name', 'item_description', 'split_category_0'], axis=1)
 category_df.split_category_1 = category_df.split_category_1.astype('category')
 category_df.split_category_2 = category_df.split_category_2.astype('category')
 category_df.brand_name = category_df.brand_name.astype('category')
 category_df.brand_category_2 = category_df.brand_category_2.astype('category')
 category_df.split_category_1 = category_df.split_category_1.cat.codes
category_df.split_category_2 = category_df.split_category_2.cat.codes
category_df.brand_name = category_df.brand_name.cat.codes
category_df.brand_category_2 = category_df.brand_category_2.cat.codes
category_df.head()<categorify>
y_pred = nn.predict(test_dataframe)
 threshold = 0.5
tansform_survived = np.vectorize(lambda x: 1 if x >= threshold else 0)
 y_pred2 = tansform_survived(y_pred)
 y_pred2 = pd.DataFrame({'Predicted': y_pred2[:,0]})
encoder = OneHotEncoder()
X_data = encoder.fit_transform(category_df)
 X_data = hstack([X_data, name_vecs, item_desc_vecs] ).tocsr()
X_data = np.log1p(X_data)
 y_train = np.log1p(train_df[train_df.split_category_0 == 'Other'].price)
 category_index = category_df.index
train_category = train_df.split_category_0.value_counts() ['Other']
submit_index = category_index[train_category:]
X_train = X_data[:train_category]
X_submit = X_data[train_category:]<train_model>
y_test = pd.read_csv(".. /input/gender_submission.csv", sep=",")
 cm = confusion_matrix(y_test['Survived'], y_pred2['Predicted'])
 print(cm)
<predict_on_test><EOS> 
test_csv['Survived'] = y_pred2['Predicted']
test_csv.to_csv('submission_all_keras_nn_0.5.csv', index=False)