% Facts
parent(john, mary). % John is a parent of Mary
parent(john, michael). % John is a parent of Michael
parent(susan, mary). % Susan is a parent of Mary
parent(susan, michael). % Susan is a parent of Michael
parent(mary, sophia). % Mary is a parent of Sophia
parent(mary, james). % Mary is a parent of James
male(john). % John is male
male(michael). % Michael is male
male(james). % James is male
female(susan). % Susan is female
female(mary). % Mary is female
female(sophia). % Sophia is female
% Rules
father(F, C) :- parent(F, C), male(F). % F is the father of C if F is a parent of C and F is male
mother(M, C) :- parent(M, C), female(M). % M is the mother of C if M is a parent of C and M is
female
sibling(A, B) :- parent(P, A), parent(P, B), A \= B. % A and B are siblings if they share a parent and are
not the same person
grandparent(G, C) :- parent(G, P), parent(P, C). % G is a grandparent of C if G is a parent of P and P is
a parent of C
grandchild(C, G) :- grandparent(G, C). % C is a grandchild of G if G is a grandparent of C
% Queries and Testing
% To test, you can use queries like:
% - father(john, mary). (Should return true)
% - mother(susan, michael). (Should return true)
% - sibling(mary, michael). (Should return true)
% - grandparent(john, sophia). (Should return true)
% - grandchild(james, susan). (Should return true) % Facts
parent(john, mary). % John is a parent of Mary
parent(john, michael). % John is a parent of Michael
parent(susan, mary). % Susan is a parent of Mary
parent(susan, michael). % Susan is a parent of Michael
parent(mary, sophia). % Mary is a parent of Sophia
parent(mary, james). % Mary is a parent of James
male(john). % John is male
male(michael). % Michael is male
male(james). % James is male
female(susan). % Susan is female
female(mary). % Mary is female
female(sophia). % Sophia is female
% Rules
father(F, C) :- parent(F, C), male(F). % F is the father of C if F is a parent of C and F is male
mother(M, C) :- parent(M, C), female(M). % M is the mother of C if M is a parent of C and M is
female
sibling(A, B) :- parent(P, A), parent(P, B), A \= B. % A and B are siblings if they share a parent and are
not the same person
grandparent(G, C) :- parent(G, P), parent(P, C). % G is a grandparent of C if G is a parent of P and P is
a parent of C
grandchild(C, G) :- grandparent(G, C). % C is a grandchild of G if G is a grandparent of C
% Queries and Testing
% To test, you can use queries like:
% - father(john, mary). (Should return true)
% - mother(susan, michael). (Should return true)
% - sibling(mary, michael). (Should return true)
% - grandparent(john, sophia). (Should return true)
% - grandchild(james, susan). (Should return true) % Facts
parent(john, mary). % John is a parent of Mary
parent(john, michael). % John is a parent of Michael
parent(susan, mary). % Susan is a parent of Mary
parent(susan, michael). % Susan is a parent of Michael
parent(mary, sophia). % Mary is a parent of Sophia
parent(mary, james). % Mary is a parent of James
male(john). % John is male
male(michael). % Michael is male
male(james). % James is male
female(susan). % Susan is female
female(mary). % Mary is female
female(sophia). % Sophia is female
% Rules
father(F, C) :- parent(F, C), male(F). % F is the father of C if F is a parent of C and F is male
mother(M, C) :- parent(M, C), female(M). % M is the mother of C if M is a parent of C and M is
female
sibling(A, B) :- parent(P, A), parent(P, B), A \= B. % A and B are siblings if they share a parent and are
not the same person
grandparent(G, C) :- parent(G, P), parent(P, C). % G is a grandparent of C if G is a parent of P and P is
a parent of C
grandchild(C, G) :- grandparent(G, C). % C is a grandchild of G if G is a grandparent of C
% Queries and Testing
% To test, you can use queries like:
% - father(john, mary). (Should return true)
% - mother(susan, michael). (Should return true)
% - sibling(mary, michael). (Should return true)
% - grandparent(john, sophia). (Should return true)
% - grandchild(james, susan). (Should return true) % Facts
parent(john, mary). % John is a parent of Mary
parent(john, michael). % John is a parent of Michael
parent(susan, mary). % Susan is a parent of Mary
parent(susan, michael). % Susan is a parent of Michael
parent(mary, sophia). % Mary is a parent of Sophia
parent(mary, james). % Mary is a parent of James
male(john). % John is male
male(michael). % Michael is male
male(james). % James is male
female(susan). % Susan is female
female(mary). % Mary is female
female(sophia). % Sophia is female
% Rules
father(F, C) :- parent(F, C), male(F). % F is the father of C if F is a parent of C and F is male
mother(M, C) :- parent(M, C), female(M). % M is the mother of C if M is a parent of C and M is
female
sibling(A, B) :- parent(P, A), parent(P, B), A \= B. % A and B are siblings if they share a parent and are
not the same person
grandparent(G, C) :- parent(G, P), parent(P, C). % G is a grandparent of C if G is a parent of P and P is
a parent of C
grandchild(C, G) :- grandparent(G, C). % C is a grandchild of G if G is a grandparent of C
% Queries and Testing
% To test, you can use queries like:
% - father(john, mary). (Should return true)
% - mother(susan, michael). (Should return true)
% - sibling(mary, michael). (Should return true)
% - grandparent(john, sophia). (Should return true)
% - grandchild(james, susan). (Should return true) % Facts
parent(john, mary). % John is a parent of Mary
parent(john, michael). % John is a parent of Michael
parent(susan, mary). % Susan is a parent of Mary
parent(susan, michael). % Susan is a parent of Michael
parent(mary, sophia). % Mary is a parent of Sophia
parent(mary, james). % Mary is a parent of James
male(john). % John is male
male(michael). % Michael is male
male(james). % James is male
female(susan). % Susan is female
female(mary). % Mary is female
female(sophia). % Sophia is female
% Rules
father(F, C) :- parent(F, C), male(F). % F is the father of C if F is a parent of C and F is male
mother(M, C) :- parent(M, C), female(M). % M is the mother of C if M is a parent of C and M is
female
sibling(A, B) :- parent(P, A), parent(P, B), A \= B. % A and B are siblings if they share a parent and are
not the same person
grandparent(G, C) :- parent(G, P), parent(P, C). % G is a grandparent of C if G is a parent of P and P is
a parent of C
grandchild(C, G) :- grandparent(G, C). % C is a grandchild of G if G is a grandparent of C
% Queries and Testing
% To test, you can use queries like:
% - father(john, mary). (Should return true)
% - mother(susan, michael). (Should return true)
% - sibling(mary, michael). (Should return true)
% - grandparent(john, sophia). (Should return true)
% - grandchild(james, susan). (Should return true)
DECISION TREE
# Import necessary libraries
import numpy as np
import pandas as pd
from [Link] import load_iris
from [Link] import DecisionTreeClassifier, export_text, plot_tree
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
import [Link] as plt
# Load the Iris dataset
iris = load_iris()
X = [Link] # Features
y = [Link] # Labels
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Initialize the Decision Tree Classifier
clf = DecisionTreeClassifier(criterion='gini', max_depth=3, random_state=42)
# Train the model
[Link](X_train, y_train)
# Make predictions on the test set
y_pred = [Link](X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of the Decision Tree Classifier: {accuracy:.2f}")
# Display the tree structure as text
print("\nDecision Tree Structure:")
print(export_text(clf, feature_names=iris.feature_names))
# Plot the decision tree
[Link](figsize=(12, 8))
plot_tree(clf, feature_names=iris.feature_names, class_names=iris.target_names, filled=True)
[Link]("Decision Tree for Iris Dataset")
[Link]()