<jupyter_start><jupyter_text>The sinking of the RMS Titanic is one of the most infamous tragedy in history. In this hands-on, a machine learning system will be developed to predict which passengers survived the tragedy.![Image of Titanic](https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/RMS_Titanic_3.jpg/450px-RMS_Titanic_3.jpg)<jupyter_code>import pandas as pd
from warnings import filterwarnings
filterwarnings('ignore')

df = pd.read_csv('titanic.csv')
# Preview 10 lines of dataset
df.head(10)
# Statistical summary
df.describe()
# Check for missing data
df.isnull().sum()
# Select the relevant columns only
selected = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked', 'Survived']
df1 = df[selected]<jupyter_output><empty_output><jupyter_text>To do: Replace the missing data (imputation) using the statistics (mean, median etc.)<jupyter_code>df1['Age'] = df1['Age'].fillna(df1['Age'].median())<jupyter_output><empty_output><jupyter_text>To do: Check whether there is any more missing data<jupyter_code>df1.isnull().sum()<jupyter_output><empty_output><jupyter_text>To do: - Remove the samples with missing data.- Use get_dummies() function from pandas to convert categorical features into one hot encoding.<jupyter_code>df1.dropna(inplace=True)
df2 = pd.get_dummies(df1)
df2<jupyter_output><empty_output><jupyter_text>To do: - Store the features as variable X and targets as variable y- Split the data into training set and testing test<jupyter_code>from sklearn.model_selection import train_test_split as split

y = df2['Survived'].values
del df2['Survived']
X = df2.values
print(X.shape, y.shape)

X_train, X_test, y_train, y_test = split(X, y, random_state=42)<jupyter_output>(889, 10) (889,)<jupyter_text>To do: Use grid search to fine tune the hyperparameters of Decition Tree Classifier (do this after Part 3)<jupyter_code>from sklearn.tree import DecisionTreeClassifier

dct = DecisionTreeClassifier().fit(X_train, y_train)
print(f'dct accuracy: {dct.score(X_test, y_test)}')
from sklearn.model_selection import GridSearchCV

params = {}
params['max_leaf_nodes'] = list(range(2, 11))
params['max_depth'] = list(range(11))
gs = GridSearchCV(DecisionTreeClassifier(), params, cv=5, n_jobs=-1, verbose=2)
gs.fit(X_train, y_train)
print(gs.best_params_)
dtc2 = DecisionTreeClassifier(**gs.best_params_).fit(X_train, y_train)
print(f'dtc2 accuracy: {dtc2.score(X_test, y_test)}')<jupyter_output>dtc2 accuracy: 0.8161434977578476
{'fraction_non_alphanumeric': 0.12128612128612129, 'fraction_numerical': 0.024827024827024827, 'mean_word_length': 7.304054054054054, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '471353', 'license': 'CC0-1.0', 'n_tokens_mistral': 907, 'n_tokens_neox': 848, 'n_words': 218, 'path': 'handson_2/handson_2c.ipynb', 'repo_name': 'wooihaw/pai_nov2021', 'size': '2457'}