<jupyter_start><jupyter_text>Fitting the decision tree with default hyperparameters, apart from max_depth which is 3 so that we can plot and read the tree.<jupyter_code>from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(max_depth=3)
dt.fit(X_train, y_train)
# print(sklearn.tree.export_graphviz(dt_default, 
#                 filled=True, rounded=True,
#                 special_characters=True, feature_names = X.columns,
#                 class_names=['Disease', "No Disease"]))
# Importing required packages for visualization
from IPython.display import Image  
from sklearn.externals.six import StringIO  
from sklearn.tree import export_graphviz
import pydotplus, graphviz
# # If you're on windows:
# # Specifing path for dot file.
# import os
# os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/graphviz-2.38/bin/'
# plotting tree with max_depth=3
dot_data = StringIO()  

export_graphviz(dt, out_file=dot_data, filled=True, rounded=True,
                feature_names=X.columns, 
                class_names=['No Disease', "Disease"])

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
#Image(graph.create_png(),width=800,height=900)
#graph.write_pdf("dt_heartdisease.pdf")
# print(export_graphviz(dt, filled=True, rounded=True, special_characters=True,
#                 feature_names=X.columns, 
#                 class_names=['No Disease', "Disease"]))
# print(dot_data.getvalue())<jupyter_output><empty_output><jupyter_text>Evaluating model performance<jupyter_code>y_train_pred = dt.predict(X_train)
y_test_pred = dt.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
print(accuracy_score(y_train, y_train_pred))
confusion_matrix(y_train, y_train_pred)
print(accuracy_score(y_test, y_test_pred))
confusion_matrix(y_test, y_test_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10855614973262032, 'fraction_numerical': 0.0074866310160427805, 'mean_word_length': 5.278523489932886, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '359183', 'license': 'MIT', 'n_tokens_mistral': 644, 'n_tokens_neox': 616, 'n_words': 120, 'path': 'Heart Data - Decision Tree/.ipynb_checkpoints/Heart+Disease+Prediction_1-checkpoint.ipynb', 'repo_name': 'nirbaaytandon/Data-Science', 'size': '1870'}