<jupyter_start><jupyter_text>Decision Trees Algorithm Comparison between CART and ID3<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree<jupyter_output><empty_output><jupyter_text>Download and Make Dataset<jupyter_code>url = 'https://archive.ics.uci.edu/ml/machine-learning-databases' \
    '/balance-scale/balance-scale.data'
names = [
    'Class Name',
    'Left-Weight',
    'Left-Distance',
    'Right-Weight',
    'Right-Distance',
]
df = pd.read_csv(url, sep=',', names=names)
df.head(2)
df['Class Name'].value_counts().plot.bar()<jupyter_output><empty_output><jupyter_text>Data Prep<jupyter_code>X = df.iloc[:, 1:5]
y = df.iloc[:, 0]
feature_names = names
class_names = df['Class Name'].unique().tolist()
random_state = 123
X_train, X_test, y_train, y_test = train_test_split( 
    X, y, test_size = 0.3, random_state=random_state)<jupyter_output><empty_output><jupyter_text>CART<jupyter_code>clf_gini = DecisionTreeClassifier(criterion='gini', max_depth=6, min_samples_leaf=5, random_state=random_state)
clf_gini.fit(X_train, y_train)

clf_gini.min_impurity_decrease, clf_gini.feature_importances_
y_pred = clf_gini.predict(X_train)

clf_gini.score(X_train, y_train)
metrics.confusion_matrix(y_pred, y_train)<jupyter_output><empty_output><jupyter_text>ID3<jupyter_code>clf_entropy = DecisionTreeClassifier(criterion='entropy', max_depth=6, min_samples_leaf=5, random_state=random_state)
clf_entropy.fit(X_train, y_train)

y_pred = clf_entropy.predict(X_train)
clf_entropy.score(X_train, y_train)
metrics.confusion_matrix(y_pred, y_train)<jupyter_output><empty_output><jupyter_text>With Test Data<jupyter_code>clf = DecisionTreeClassifier(criterion='entropy', max_depth=6, min_samples_leaf=5, random_state=random_state)
clf.fit(X_test, y_test)

y_pred = clf.predict(X_test)
clf.score(X_test, y_test)
print(metrics.classification_report(y_pred, y_test))
plt.figure(figsize=(20,10))
plot_tree(clf, feature_names=feature_names, class_names=class_names, filled=True)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12225274725274725, 'fraction_numerical': 0.009615384615384616, 'mean_word_length': 8.886877828054299, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '569991', 'license': 'MIT', 'n_tokens_mistral': 866, 'n_tokens_neox': 844, 'n_words': 120, 'path': 'contents/decision-trees/Gini vs Entropy.ipynb', 'repo_name': 'metinsenturk/class-materials', 'size': '2184'}