<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import re
import numpy as np
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn import preprocessing

%matplotlib inline
sns.set()
test_df = pd.read_csv("/content/drive/My Drive/Colab Notebooks/db/titanic-test_b6547a1c-55fe-4409-8e04-67b6678214a4.csv")
train_df = pd.read_csv("/content/drive/My Drive/Colab Notebooks/db/titanic-train_4adcc656-193c-46d5-a978-f254fe4f0c22_e73b185d-1cb9-4af4-a668-da1ed47b97f2.csv")
train_df.head()
test_df.head()
train_df.info()
train_df.Sex.value_counts().plot(kind = "bar", color = ["b", "r"])
plt.title("Passenger Distribution")
plt.show()
label_encoder = preprocessing.LabelEncoder()

encoder_sex = label_encoder.fit_transform(train_df["Sex"])
train_df.head()
train_df["Age"] = train_df["Age"].fillna(train_df["Age"].median())
train_df["Embarked"] = train_df["Embarked"].fillna("S")
train_predictors = train_df.drop(["PassengerId", "Survived", "Name", "Ticket", "Cabin"], axis = 1)

categorical_cols = [cname for cname in train_predictors.columns if 
                      train_predictors[cname].nunique() < 10 and
                      train_predictors[cname].dtype == "object"]
numerical_cols = [cname for cname in train_predictors.columns if
                  train_predictors[cname].dtype in ["int64", "float"]]
my_cols = categorical_cols + numerical_cols

train_predictors = train_predictors[my_cols]
dummy_encoded_train_predictor = pd.get_dummies(train_predictors)
train_df["Pclass"].value_counts()
y_target = train_df["Survived"].values
x_features_one = dummy_encoded_train_predictor.values
X_train, X_validation, y_train, y_validation = train_test_split(x_features_one, y_target, test_size = 0.25, random_state = 1)
tree_one = tree.DecisionTreeClassifier()
tree_one = tree_one.fit(x_features_one, y_target)
tree_one_accuracy = round(tree_one.score(x_features_one, y_target), 4)
print("Accuracy: %0.4f" % (tree_one_accuracy))<jupyter_output>Accuracy: 0.9798<jupyter_text>Visualization<jupyter_code>from io import StringIO
from IPython.display import Image, display
import pydotplus

out = StringIO()
tree.export_graphviz(tree_one, out_file= out)

graph = pydotplus.graph_from_dot_data(out.getvalue())
graph.write_png("Titanic.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12150719729043184, 'fraction_numerical': 0.03344623200677392, 'mean_word_length': 6.573717948717949, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '650528', 'license': 'MIT', 'n_tokens_mistral': 923, 'n_tokens_neox': 865, 'n_words': 145, 'path': 'Decission_Trees.ipynb', 'repo_name': 'XavierCarrera/practical-machine-learning', 'size': '2362'}