How does a decision tree in machine learning work? explain and show some code example please
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
A decision tree is a flowchart-like tree structure where an internal node represents feature(or attribute), the branch represents a decision rule, and each leaf node represents the outcome. The topmost node in a decision tree is known as the root node. It learns to partition on the basis of the attribute value. It partitions the tree in recursively manner call recursive partitioning. This flowchart-like structure helps you in decision making. It's visualization like a flowchart diagram which easily mimics the human level thinking. That is why decision trees are easy to understand and interpret.
Decision Tree is a white box type of ML algorithm. It shares internal decision-making logic, which is not available in the black box type of algorithms such as Neural Network. Its training time is faster compared to the neural network algorithm. The time complexity of decision trees is a function of the number of records and number of attributes in the given data. The decision tree is a distribution-free or non-parametric method, which does not depend upon probability distribution assumptions. Decision trees can handle high dimensional data with good accuracy.
The basic idea behind any decision tree algorithm is as follows:
Below is the implementation in python code
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
# Function importing Dataset
def importdata():
balance_data = pd.read_csv(
'https://archive.ics.uci.edu/ml/machine-learning-'+
'databases/balance-scale/balance-scale.data',
sep= ',', header = None)
# Printing the dataswet shape
print ("Dataset Length: ", len(balance_data))
print ("Dataset Shape: ", balance_data.shape)
# Printing the dataset obseravtions
print ("Dataset: ",balance_data.head())
return balance_data
# Function to split the dataset
def splitdataset(balance_data):
# Separating the target variable
X = balance_data.values[:, 1:5]
Y = balance_data.values[:, 0]
# Splitting the dataset into train and test
X_train, X_test, y_train, y_test = train_test_split(
X, Y, test_size = 0.3, random_state = 100)
return X, Y, X_train, X_test, y_train, y_test
# Function to perform training with giniIndex.
def train_using_gini(X_train, X_test, y_train):
# Creating the classifier object
clf_gini = DecisionTreeClassifier(criterion = "gini",
random_state = 100,max_depth=3, min_samples_leaf=5)
# Performing training
clf_gini.fit(X_train, y_train)
return clf_gini
# Function to perform training with entropy.
def tarin_using_entropy(X_train, X_test, y_train):
# Decision tree with entropy
clf_entropy = DecisionTreeClassifier(
criterion = "entropy", random_state = 100,
max_depth = 3, min_samples_leaf = 5)
# Performing training
clf_entropy.fit(X_train, y_train)
return clf_entropy
# Function to make predictions
def prediction(X_test, clf_object):
# Predicton on test with giniIndex
y_pred = clf_object.predict(X_test)
print("Predicted values:")
print(y_pred)
return y_pred
# Function to calculate accuracy
def cal_accuracy(y_test, y_pred):
print("Confusion Matrix: ",
confusion_matrix(y_test, y_pred))
print ("Accuracy : ",
accuracy_score(y_test,y_pred)*100)
print("Report : ",
classification_report(y_test, y_pred))
# Driver code
def main():
# Building Phase
data = importdata()
X, Y, X_train, X_test, y_train, y_test = splitdataset(data)
clf_gini = train_using_gini(X_train, X_test, y_train)
clf_entropy = tarin_using_entropy(X_train, X_test, y_train)
# Operational Phase
print("Results Using Gini Index:")
# Prediction using gini
y_pred_gini = prediction(X_test, clf_gini)
cal_accuracy(y_test, y_pred_gini)
print("Results Using Entropy:")
# Prediction using entropy
y_pred_entropy = prediction(X_test, clf_entropy)
cal_accuracy(y_test, y_pred_entropy)
# Calling main function
if __name__=="__main__":
main()

Kindly revert for any queries
Thanks.
How does a decision tree in machine learning work? explain and show some code example please
Outline the steps of a decision tree induction algorithm and give an example of a decision tree which could be output. Describe how to interpret a decision tree model.
Please show the work. Please show and explain how you come to
the numbers.
Your boss askes you to analyze a buy lease decision r equipment for your The new machinery will cost $2,200,000. You can get a loan for 1009 of the value of the loan and would pay interest only at a rate of 5.75% with a balloon payment at the end of the three Purchasing the equipment will require annual maintenance costs $250,000 a year (paid at...
What are some behaviors that show infants are capable of learning? How does imitation differ in newborns and older infants? What is habituation?
please show work and explain Suppose that an AVL tree is constructed by adding the following strings, in the order given: "Fred", "Terri", "Bob", "Wilma", "Zelda", "Pam", "Ron" Which of the following statements will be true? Fred will be in the root. The tree will be full. Terri will have two children. The tree will have height 3.
How is decision tree analysis used in business analytics? Give an example of how it is used to solve a business problem. Mention the references.link from where you got the information
How does a Project Manager, in some cases, work like a politician? ▪Please explain this by applying it in a project that you have previously been (or currently) involved with, within your organization
Artificial intelligence and machine learning are transforming some industries. How can A.I.benefit patient care? Will machine learning eliminate jobs in healthcare?
(Data Analysis Question) Explain supervised machine learning using some examples of data and at least two, two-dimensional supervised machine learning methods. Describe in writing any figures that you normally would draw.
8. Decision Tree Learning Output Alt Yes Yes No Yes Fri No No No $ No Bar No No Yes No No No NO Yes Yes Yes Yes No Yes No $$$ No No Yes Yes Yes HunPatPrice Rain ResType Yes Some $$$ French Yes Full No Thai No Some No Burger Yes Full No No Thai Full Yes French Yes Some Yes Yes Italian No None Yes No Burger Yes Some Yes Yes Thai Full No Burger Yes Full...
Describe how is machine learning and how does it relate to AI in improving your organisational value chain performance