practical 15 python
practical 15 python
Aim:-Write a program a Build a Decision Tree Classifier using Gini Criteria in a Dataset.
A Decision Tree Classifier is a type of supervised learning algorithm that uses a tree-like
model to classify data into different categories. The algorithm works by recursively
partitioning the data into smaller subsets based on the values of the input features. Each
internal node in the tree represents a feature or attribute, and each leaf node represents a
class label. The classification process involves traversing the tree from the root node to a leaf
node, with each node providing a decision based on the input features.
Input:-
iris = load_iris()
X = iris.data
y = iris.target
clf = DecisionTreeClassifier(random_state=1)
# train the classifier
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
# claculate accuracy
print(f'Accuracy: {accuracy}')
Output:-
Accuracy: 0.9555555555555556
Input:-
param_grid = {
tree = DecisionTreeClassifier(random_state=1)
# GridSearchCV
cv=5, verbose=True)
grid_search.fit(X_train, y_train)
# Best score and estimator
print(grid_search.best_estimator_)
Output:
Input:-
# best estimator
tree_clf = grid_search.best_estimator_
# plot
plt.figure(figsize=(18, 15))
class_names=iris.target_names)
plt.show()
Output:-
Input:-
import pandas as pd
# load dataset
dataset_link = 'https://media.geeksforgeeks.org/wp-
content/uploads/20240620175612/spam_email.csv'
df = pd.read_csv(dataset_link)
# plot the category count
df['Category'].value_counts().plot.bar(color = ["g","r"])
plt.show()
Input:-
# confusion matrix
Output:-
Conclusion:-
In this article, we have explored the world of Decision Tree Classifiers using Scikit-Learn. We
have covered the theoretical foundations, implementation, and practical applications of
Decision Tree Classifiers, providing a comprehensive guide for both beginners and
experienced practitioners. By understanding the strengths and limitations of Decision Tree
Classifiers, we can harness their power to build accurate and interpretable machine learning
models.