<jupyter_start><jupyter_text><jupyter_code>from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

cancer = load_breast_cancer()



X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)

gini_tree = DecisionTreeClassifier(random_state=0,max_depth=4)
gini_tree.fit(X_train, y_train)

print("Accuracy on training set: {:.3f}".format(gini_tree.score(X_train, y_train)))
print("Accuracy on test set: {:.3f}".format(gini_tree.score(X_test, y_test)))

#############CONFUSOIN MATRIX######################################
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm
################ TREE PROPERTIES ###################

import numpy as np

n_nodes = gini_tree.tree_.node_count
children_left = gini_tree.tree_.children_left
children_right = gini_tree.tree_.children_right
feature = gini_tree.tree_.feature
threshold = gini_tree.tree_.threshold

node_depth = np.zeros(shape=n_nodes, dtype=np.int64)
is_leaves = np.zeros(shape=n_nodes, dtype=bool)
stack = [(0, 0)]  # start with the root node id (0) and its depth (0)
while len(stack) > 0:
    # `pop` ensures each node is only visited once
    node_id, depth = stack.pop()
    node_depth[node_id] = depth

    # If the left and right child of a node is not the same we have a split
    # node
    is_split_node = children_left[node_id] != children_right[node_id]
    # If a split node, append left and right children and depth to `stack`
    # so we can loop through them
    if is_split_node:
        stack.append((children_left[node_id], depth + 1))
        stack.append((children_right[node_id], depth + 1))
    else:
        is_leaves[node_id] = True

print("The binary tree structure has {n} nodes and has "
      "the following tree structure:\n".format(n=n_nodes))
for i in range(n_nodes):
    if is_leaves[i]:
        print("{space}node={node} is a leaf node.".format(
            space=node_depth[i] * "\t", node=i))
    else:
        print("{space}node={node} is a split node: "
              "go to node {left} if X[:, {feature}] <= {threshold} "
              "else to node {right}.".format(
                  space=node_depth[i] * "\t",
                  node=i,
                  left=children_left[i],
                  feature=feature[i],
                  threshold=threshold[i],
                  right=children_right[i]))


###### DECIOSION PATH ################
node_indicator = gini_tree.decision_path(X_test)
leaf_id = gini_tree.apply(X_test)

sample_id = 0
# obtain ids of the nodes `sample_id` goes through, i.e., row `sample_id`
node_index = node_indicator.indices[node_indicator.indptr[sample_id]:
                                    node_indicator.indptr[sample_id + 1]]

print('Rules used to predict sample {id}:\n'.format(id=sample_id))
for node_id in node_index:
    # continue to the next node if it is a leaf node
    if leaf_id[sample_id] == node_id:
        continue

    # check if value of the split feature for sample 0 is below threshold
    if (X_test[sample_id, feature[node_id]] <= threshold[node_id]):
        threshold_sign = "<="
    else:
        threshold_sign = ">"

    print("decision node {node} : (X_test[{sample}, {feature}] = {value}) "
          "{inequality} {threshold})".format(
              node=node_id,
              sample=sample_id,
              feature=feature[node_id],
              value=X_test[sample_id, feature[node_id]],
              inequality=threshold_sign,
              threshold=threshold[node_id]))
    
##########################################33

sample_ids = [0, 2]
# boolean array indicating the nodes both samples go through
common_nodes = (node_indicator.toarray()[sample_ids].sum(axis=0) ==
                len(sample_ids))
# obtain node ids using position in array
common_node_id = np.arange(n_nodes)[common_nodes]

print("\nThe following samples {samples} share the node(s) {nodes} in the "
      "tree.".format(samples=sample_ids, nodes=common_node_id))
print("This is {prop}% of all nodes.".format(
    prop=100 * len(common_node_id) / n_nodes))
import matplotlib.pyplot as plt
import seaborn as sns 
from sklearn import tree


"""**Pictorial representation of Decision Tree**"""

plt.figure(figsize=(20,20))
tree.plot_tree(gini_tree)
plt.show()
# Commented out IPython magic to ensure Python compatibility.
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns 

from sklearn import tree

# %matplotlib inline

"""**Read Iris Dataset**"""

data = pd.read_csv('Iris.csv')
data

data.shape

"""**Define Colunms**"""

col_names = ['id', 'sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']


data.columns = col_names

col_names

"""**Drop Id Column**"""

data = data.drop(['id'], axis=1)

data.head()

data.info()

"""**Checking the  target categorical counts**"""

data['species'].value_counts()

"""**Check missing values in variables**"""

data.isnull().sum()

target_col = ['species']

X = data.drop(['species'], axis=1)

y = data['species']

"""**Split dataset into train and test**"""

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 42)

"""**Check datatypes**"""

X_train.dtypes

"""**Decision Tree Classification based on Gini index criterion**"""

from sklearn.tree import DecisionTreeClassifier

clf_gini = DecisionTreeClassifier(criterion='gini', max_depth=3, random_state=0)
clf_gini.fit(X_train, y_train)

y_pred_gini = clf_gini.predict(X_test)
y_pred_gini

"""**Check accurcy of model**"""

from sklearn.metrics import accuracy_score

print('Model accuracy score with criterion gini index: {0:0.4f}'. format(accuracy_score(y_test, y_pred_gini)))# y_pred_gini are the predicted class labels in the test-set.

#Compare the train-set and test-set accuracy
y_pred_train_gini = clf_gini.predict(X_train)

y_pred_train_gini

print('Training-set accuracy score: {0:0.4f}'. format(accuracy_score(y_train, y_pred_train_gini)))

#Check for overfitting and underfitting

print('Training set score: {:.4f}'.format(clf_gini.score(X_train, y_train)))

print('Test set score: {:.4f}'.format(clf_gini.score(X_test, y_test)))

"""**Pictorial representation of Decision Tree**"""

plt.figure(figsize=(12,8))
tree.plot_tree(clf_gini.fit(X_train, y_train))
#############DECISION TREE FOR REGRESSION ##########################

import pandas as pd
from sklearn import datasets

boston = datasets.load_boston()            # Load Boston Dataset
df = pd.DataFrame(boston.data[:, 12])      # Create DataFrame using only the LSAT feature
df.columns = ['LSTAT']
df['MEDV'] = boston.target                 # Create new column with the target MEDV
df.head()

from sklearn.tree import DecisionTreeRegressor    # Import decision tree regression model

X = df[['LSTAT']].values                          # Assign matrix X
y = df['MEDV'].values                             # Assign vector y

sort_idx = X.flatten().argsort()                  # Sort X and y by ascending values of X
X = X[sort_idx]
y = y[sort_idx]

tree = DecisionTreeRegressor(criterion='mse',     # Initialize and fit regressor
                             max_depth=3)         
tree.fit(X, y)

plt.figure(figsize=(16, 8))
plt.scatter(X, y, c='steelblue',                  # Plot actual target against features
            edgecolor='white', s=70)
plt.plot(X, tree.predict(X),                      # Plot predicted target against features
         color='black', lw=2)
plt.xlabel('% lower status of the population [LSTAT]')
plt.ylabel('Price in $1000s [MEDV]')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1385058912008022, 'fraction_numerical': 0.007896715968914515, 'mean_word_length': 3.336413043478261, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '106461', 'license': 'MIT', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2554, 'n_words': 652, 'path': 'decision_tree_learning/basic_decision_tree.ipynb', 'repo_name': 'iceman011/tutorials', 'size': '7978'}