<jupyter_start><jupyter_text>Decision Tree Model<jupyter_code>import jenkspy as jenkspy
import pandas as pd
import numpy as np
import seaborn as sns
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn import tree, metrics
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.tree import DecisionTreeClassifier
df=pd.read_csv('prepared_data.csv')
df


train, test = train_test_split(df,test_size=0.2)
train
train['stroke'].hist()<jupyter_output><empty_output><jupyter_text>stroke = 1  need to add<jupyter_code>class_count_1, class_count_2 = train['stroke'].value_counts()
#seprate 2 classes of Outcome
class_1 = train[train['stroke'] == 0]
class_2 = train[train['stroke'] == 1]# print the shape of the class
#print('class 1:', class_1.shape)
#print('class 2:', class_2.shape)
class_2_over = class_2.sample(class_count_1, replace=True)
test_over = pd.concat([class_2_over, class_1], axis=0)
#print("total Outcome of 1 and 2:",test_over['Outcome'].value_counts())# plot the count after under-sampeling
test_over['stroke'].value_counts().plot(kind='bar', title='count (Stroke)')
y_train=test_over['stroke']
x_train=test_over.drop(columns='stroke')
x_train
y_train.hist()
y_test=test['stroke']
x_test=test.drop(columns='stroke')
x_test
accuracy_list = []
for i in range(1,10):
    DecisionTree = DecisionTreeClassifier(max_depth = i, criterion='gini')
    DecisionTree.fit(x_train, y_train)
    y_pred = DecisionTree.predict(x_test)
    accuracy_list.append(accuracy_score(y_test,y_pred)*100)
plt.figure()
plt.xticks([0,1,2,3,4,5,6],[3,4,5,6,7,8,9])
plt.plot(accuracy_list, color="green", linewidth=2)
plt.xlabel("depth",c="b")
plt.ylabel("accuracy",c="b")
plt.yticks(c= 'green')
plt.xticks(c= 'green')
plt.title("Decision Tree",c="b")
plt.show()<jupyter_output><empty_output><jupyter_text>10 is the best for max depth<jupyter_code>DT = DecisionTreeClassifier(criterion = "gini", max_leaf_nodes=5,max_depth=10).fit(x_train,y_train)
fig, axs = plt.subplots(1, 1, figsize=(10,10))
tree.plot_tree(DT, ax=axs)

y_pred = DT.predict(x_test)
metrics.accuracy_score(y_test, y_pred)
print('acc for training data: {:.3f}'.format(DT.score(x_train, y_train)*100))
print('acc for test data: {:.3f}'.format(DT.score(x_test, y_test)*100))<jupyter_output>acc for training data: 76.045
acc for test data: 74.374<jupyter_text>classification report for gini<jupyter_code>print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.97      0.75      0.85       789
           1       0.15      0.68      0.24        50

    accuracy                           0.74       839
   macro avg       0.56      0.71      0.54       839
weighted avg       0.92      0.74      0.81       839<jupyter_text>ٍ Let's test Entropy<jupyter_code>accuracy_list = []
for i in range(1,10):
    DecisionTree = DecisionTreeClassifier(max_depth = i, criterion="entropy")
    DecisionTree.fit(x_train, y_train)
    y_pred = DecisionTree.predict(x_test)
    accuracy_list.append(accuracy_score(y_test,y_pred)*100)
plt.figure()
plt.xticks([0,1,2,3,4,5,6],[3,4,5,6,7,8,9])
plt.plot(accuracy_list, color="r", linewidth=2)
plt.xlabel("depth",c="b")
plt.ylabel("accuracy",c="b")
plt.yticks(c= 'r')
plt.xticks(c= 'r')
plt.title("Decision Tree",c="b")
plt.show()<jupyter_output><empty_output><jupyter_text>10 is the best for max depth<jupyter_code>DT = DecisionTreeClassifier(criterion = "entropy", max_leaf_nodes=5,max_depth=10).fit(x_train,y_train)
fig, axs = plt.subplots(1, 1, figsize=(10,10))
tree.plot_tree(DT, ax=axs)
y_pred = DT.predict(x_test)
metrics.accuracy_score(y_test, y_pred)
print('acc for training data: {:.3f}'.format(DT.score(x_train, y_train)*100))
print('acc for test data: {:.3f}'.format(DT.score(x_test, y_test)*100))<jupyter_output>acc for training data: 75.586
acc for test data: 72.348<jupyter_text>classification report for entropy<jupyter_code>print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.97      0.72      0.83       789
           1       0.14      0.70      0.23        50

    accuracy                           0.72       839
   macro avg       0.56      0.71      0.53       839
weighted avg       0.92      0.72      0.80       839
{'fraction_non_alphanumeric': 0.13765460375629868, 'fraction_numerical': 0.053595968850206135, 'mean_word_length': 4.527848101265823, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '365502', 'license': 'MIT', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1650, 'n_words': 312, 'path': 'P4_Tree.ipynb', 'repo_name': 'GhazaleZe/Stroke-Prediction', 'size': '4366'}