Write A Program To Demonstrate Decision Tree Algorithm For A Classification Problem and Perform Parameter Tuning For Better Results
Write A Program To Demonstrate Decision Tree Algorithm For A Classification Problem and Perform Parameter Tuning For Better Results
Description:
Decision Trees in Machine Learning
Decision Trees are a popular supervised learning algorithm used for both
classification and regression tasks. They mimic the human decision-making
process by creating a tree-like model of decisions and their possible
consequences.
Parameter Tuning
Algorithm:
Step 1: Start the Program.
Step 2: Import all necessary libraries for the problem.
Step 3: Loads the Iris dataset and splits it into training and testing sets.
Step 4: Create a Decision Tree Classifier and Train the initial decision tree model on the
training data using clf.fit()
Step 5: Use the trained model to make predictions on the test data using clf.predict().
Step 6: Calculate the accuracy of the initial model using accuracy_score()
Step 7: Create a dictionary param_grid to define the hyperparameters to tune and their possible
values
Step 8: Perform Grid Search with Cross-Validation and Print best parameters ,score.
Step 9:Create a new DecisionTreeClassifier instance with the best parameters found by grid
search and Train this new model on the training data.
Step 10. Make predictions on the test set using the model trained with the best parameters.
Step 11.Calculate and print the accuracy of the model with the best parameters.
Step 12. Visualize the decision tree
# Make predictions
y_pred = clf.predict(X_test)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Initial Accuracy:", accuracy)
# Make predictions
y_pred = regressor.predict(X_test)
VIVA QUESTIONS:
1. What is DecisionTreeClassifier and DecisionTreeRegressor in python.
2.What is the purpose of matplotlib library?
3. Describe Grid Search with Cross-Validation?
4.How to Visualize the decision tree?
5.Define Root Mean Squared Error.