0% found this document useful (0 votes)
0 views

back propagation algorithm

The document contains Prolog facts and rules defining a family tree, including parent-child relationships, gender, and rules for determining family relations such as father, mother, sibling, grandparent, and grandchild. It also includes queries for testing these relationships. Additionally, it presents a Python implementation of a Decision Tree Classifier using the Iris dataset, including model training, evaluation, and visualization of the decision tree structure.

Uploaded by

sabiha.mahveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

back propagation algorithm

The document contains Prolog facts and rules defining a family tree, including parent-child relationships, gender, and rules for determining family relations such as father, mother, sibling, grandparent, and grandchild. It also includes queries for testing these relationships. Additionally, it presents a Python implementation of a Decision Tree Classifier using the Iris dataset, including model training, evaluation, and visualization of the decision tree structure.

Uploaded by

sabiha.mahveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

% 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 sklearn.datasets import load_iris

from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

import matplotlib.pyplot as plt

# Load the Iris dataset

iris = load_iris()

X = iris.data # Features

y = iris.target # 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

clf.fit(X_train, y_train)

# Make predictions on the test set

y_pred = clf.predict(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

plt.figure(figsize=(12, 8))

plot_tree(clf, feature_names=iris.feature_names, class_names=iris.target_names, filled=True)

plt.title("Decision Tree for Iris Dataset")

plt.show()

You might also like