Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Decision Tree Algorithm





No account yet ?

Sign up to access all content




The decision tree is the most powerful and widely used tool for categorization and prediction.

A decision tree is a flowchart-like tree structure in which each internal node represents an attribute test.

Each branch reflects the test result and each leaf node (terminal node) stores a class label.

The use of a decision tree algorithm aims to create a learning model capable of predicting the class or value of the target variable by learning simple decision rules inferred from historical data.



Free image provided by pexel.com


Root Nodes: It is the node present at the beginning of a decision tree from this node the population starts dividing according to various features.

Decision Nodes : The nodes we get after splitting the root nodes are called Decision Node.

Terminal Nodes : The nodes where further splitting is not possible are called leaf nodes or terminal nodes.

Sub-tree : Just like a small portion of a graph is called a sub-graph similarly a sub-section of this decision tree is called a sub-tree.



Tested in Anaconda and Python 3.7

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
 
#Creating a decision tree
from sklearn.datasets import make_blobs
 
X, y = make_blobs(n_samples=300, centers=4,
                  random_state=0, cluster_std=1.0)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='rainbow');
plt.show()
 
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier().fit(X, y)
 
def visualize_classifier(model, X, y, ax=None, cmap='rainbow'):
    ax = ax or plt.gca()
 
    # Plot the training points
    ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cmap,
               clim=(y.min(), y.max()), zorder=3)
    ax.axis('tight')
    ax.axis('off')
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
 
    # fit the estimator
    model.fit(X, y)
    xx, yy = np.meshgrid(np.linspace(*xlim, num=200),
                         np.linspace(*ylim, num=200))
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
 
    # Create a color plot with the results
    n_classes = len(np.unique(y))
    contours = ax.contourf(xx, yy, Z, alpha=0.3,
                           levels=np.arange(n_classes + 1) - 0.5,
                           cmap=cmap, clim=(y.min(), y.max()),
                           zorder=1)
 
    ax.set(xlim=xlim, ylim=ylim)
 
visualize_classifier(DecisionTreeClassifier(), X, y)
 
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import BaggingClassifier
 
tree = DecisionTreeClassifier()
bag = BaggingClassifier(tree, n_estimators=100, max_samples=0.8,
                        random_state=1)
 
bag.fit(X, y)
visualize_classifier(bag, X, y)
 


Source : https://jakevdp.github.io/PythonDataScienceHandbook/05.08-random-forests.html



PythonDataScienceHandbook

LICENSE-CODE

License: MITLicenseMIT  Copyright (c) 2016 Jacob VanderPlas


GitHub



Free image provided by pexel.com
Free image provided by pexel.com




Tested in Anaconda and Python 3.7

import matplotlib.pyplot as plot
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
 
iris = datasets.load_iris()
X = iris.data[:, 2:]
Y = iris.target
 
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.5, random_state=1, stratify=Y)
 
classifier_tree = DecisionTreeClassifier(criterion='gini', max_depth=6, random_state=1)
classifier_tree.fit(X_train, Y_train)
 
figure, axis = plot.subplots(figsize=(12, 12))
tree.plot_tree(classifier_tree, fontsize=12)
plot.show()
 


Source : https://pythonguides.com/scikit-learn-decision-tree/



Free image provided by pexel.com


Tested in Anaconda and Python 3.7

import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plot
 
 
range = np.random.RandomState(1)
X = np.sort(5 * range.rand(80, 1), axis=0)
Y = np.sin(X).ravel()
Y[::5] += 3 * (0.5 - range.rand(16))
 
 
regression_1 = DecisionTreeRegressor(max_depth=2)
regression_2 = DecisionTreeRegressor(max_depth=5)
regression_1.fit(X, Y)
regression_2.fit(X, Y)
 
 
X_test = np.arange(0.0, 5.0, 0.01)[:, np.newaxis]
Y1 = regression_1.predict(X_test)
Y2 = regression_2.predict(X_test)
 
 
plot.figure()
plot.scatter(X, Y, s=20, edgecolor="black", c="pink", label="data")
plot.plot(X_test, Y1, color="blue", label="max_depth=4", linewidth=2)
plot.plot(X_test, Y2, color="green", label="max_depth=7", linewidth=2)
plot.xlabel("data")
plot.ylabel("target")
plot.title("Decision Tree Regression")
plot.legend()
plot.show()
 


Source : https://pythonguides.com/scikit-learn-decision-tree/



Free image provided by pexel.com




Deep learning

Machine learning










Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License