0% found this document useful (0 votes)
8 views11 pages

AI Lab 2nd Intenal.

The document outlines a Python program for implementing a Decision Tree Classifier and a Multi-Layer Perceptron (MLP) neural network. It includes functions for data import, dataset splitting, training models using Gini and entropy criteria, and calculating accuracy. Additionally, it provides code for visualizing data and training an MLP classifier with specified parameters, showcasing the weights and biases of the model.

Uploaded by

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

AI Lab 2nd Intenal.

The document outlines a Python program for implementing a Decision Tree Classifier and a Multi-Layer Perceptron (MLP) neural network. It includes functions for data import, dataset splitting, training models using Gini and entropy criteria, and calculating accuracy. Additionally, it provides code for visualizing data and training an MLP classifier with specified parameters, showcasing the weights and biases of the model.

Uploaded by

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

AI lab 2nd intenal….

Set1: Decision Tree Classifier


1. Decision Tree:
Python program:

import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
def importdata():
balance_data = pd.read_csv(
'balance-scale.data',
sep= ',', header = None)

print ("Dataset Length: ", len(balance_data))


print ("Dataset Shape: ", balance_data.shape)
print ("Dataset: ",balance_data.head())
return balance_data
def splitdataset(balance_data):
X = balance_data.values[:, 1:5]
Y = balance_data.values[:, 0]
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

AI lab 2nd intenal…. 1


def train_using_gini(X_train, X_test, y_train)
clf_gini = DecisionTreeClassifier(criterion = "gini",
random_state = 100,max_depth=3, min_samples_leaf=5
clf_gini.fit(X_train, y_train)
return clf_gini
def tarin_using_entropy(X_train, X_test, y_train)
clf_entropy = DecisionTreeClassifier(
criterion = "entropy", random_state = 100,
max_depth = 3, min_samples_leaf = 5
clf_entropy.fit(X_train, y_train)
return clf_entropy
def prediction(X_test, clf_object):
y_pred = clf_object.predict(X_test)
print("Predicted values:")
print(y_pred)
return y_pred
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))

def main():
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)
print("Results Using Gini Index:")

y_pred_gini = prediction(X_test, clf_gini)


cal_accuracy(y_test, y_pred_gini)
print("Results Using Entropy:")

y_pred_entropy = prediction(X_test, clf_entropy)


cal_accuracy(y_test, y_pred_entropy)

AI lab 2nd intenal…. 2


if name=="main":
main()
Output:
Dataset Length: 625
Dataset Shape: (625, 5)
Dataset: 0 1 2 3 4
0B1111
15
1R1112
2R1113
3R1114
4R1115
Results Using Gini Index:
Predicted values:
['R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'R' 'L'
'L' 'R' 'L' 'R' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'L'
'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'R' 'L' 'R'
'R' 'L' 'R' 'R' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'L' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'R' 'L'
'R' 'R' 'L' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'R' 'L' 'R' 'R' 'R' 'R' 'R' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L'
'L' 'L' 'L' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R'
'L' 'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'R' 'R'
'L' 'L' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'R' 'R'
'L' 'R' 'R' 'L' 'L' 'R' 'R' 'R']
Confusion Matrix: [[ 0 6 7]
[ 0 67 18]
[ 0 19 71]]
Accuracy : 73.40425531914893
Report : precision recall f1
-score support
B 0.00 0.00 0.00 13
L 0.73 0.79 0.76 85
R 0.74 0.79 0.76 90
accuracy 0.73 188

AI lab 2nd intenal…. 3


macro avg 0.49 0.53 0.51 188
weighted avg 0.68 0.73 0.71 188
Results Using Entropy:
Predicted values:
['R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'L'
'L' 'R' 'L' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'L' 'L'
'L' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'R' 'L' 'L' 'R' 'L' 'L' 'R' 'L' 'L'
'R' 'L' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'L' 'R' 'L' 'L' 'L' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'R' 'R' 'L' 'R' 'L'
'R' 'R' 'L' 'L' 'L' 'R' 'R' 'L' 'L' 'L' 'R' 'L' 'L' 'R' 'R' 'R' 'R' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L'
'L' 'L' 'L' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'L' 'R'
'L' 'R' 'R' 'L' 'L' 'R' 'L' 'R' 'R' 'R' 'R' 'R' 'L' 'R' 'R' 'R' 'R' 'R'
'R' 'L' 'R' 'L' 'R' 'R' 'L' 'R' 'L' 'R' 'L' 'R' 'L' 'L' 'L' 'L' 'L' 'R'
'R' 'R' 'L' 'L' 'L' 'R' 'R' 'R']
Confusion Matrix: [[ 0 6 7]
[ 0 63 22]
[ 0 20 70]]
Accuracy : 70.74468085106383
Report : precision recall f1
-score support
B 0.00 0.00 0.00 13
16
L 0.71 0.74 0.72 85
R 0.71 0.78 0.74 90
accuracy 0.71 188
macro avg 0.47 0.51 0.49 188
weighted avg 0.66 0.71 0.68 188

Set2: multi feed forward Nn:


Python program:
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

AI lab 2nd intenal…. 4


n_samples = 200
blob_centers = ([1, 1], [3, 4], [1, 3.3], [3.5, 1.8])
data, labels = make_blobs(n_samples=n_samples,
centers=blob_centers,
cluster_std=0.5,
random_state=0)
colours = ('green', 'orange', "blue", "magenta")
fig, ax = plt.subplots()
for n_class in range(len(blob_centers)):
ax.scatter(data[labels==n_class][:, 0],
data[labels==n_class][:, 1],
c=colours[n_class],
s=30,
label=str(n_class))
from sklearn.model_selection import train_test_split
datasets = train_test_split(data,labels,test_size=0.2)
train_data, test_data, train_labels, test_labels = datasets
from sklearn.neural_network import MLPClassifier
clf = MLPClassifier(solver='lbfgs',alpha=1e-5,hidden_layer_sizes=
(6,),random_state=1)
clf.fit(train_data, train_labels)
clf.score(train_data, train_labels)
from sklearn.metrics import accuracy_score
predictions_train = clf.predict(train_data)
predictions_test = clf.predict(test_data)
train_score = accuracy_score(predictions_train, train_labels)
print("score on train data: ", train_score)
test_score = accuracy_score(predictions_test, test_labels)
print("score on test data: ", test_score)
predictions_train[:20]
from sklearn.neural_network import MLPClassifier
X = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]
y = [0, 0, 0, 1]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2),
random_state=1)
print(clf.fit(X, y))

AI lab 2nd intenal…. 5


print("weights between input and first hidden layer:")
print(clf.coefs_[0])
print("\nweights between first hidden and second hidden layer:")
print(clf.coefs_[1])
print("w0 = ", clf.coefs_[0][0][0])
print("w1 = ", clf.coefs_[0][1][0])
clf.coefs_[0][:,0]
for i in range(len(clf.coefs_)):
number_neurons_in_layer = clf.coefs_[i].shape[1]
for j in range(number_neurons_in_layer):
weights = clf.coefs_[i][:,j]
print(i, j, weights, end=", ")
print()
print()
print("Bias values for first hidden layer:")
print(clf.intercepts_[0])
print("\nBias values for second hidden layer:")
print(clf.intercepts_[1])
result = clf.predict([[0, 0], [0, 1],[1, 0], [0, 1],[1, 1], [2., 2.],[1.3, 1.3], [2, 4.8]])
prob_results = clf.predict_proba([[0, 0], [0, 1],[1, 0], [0, 1],[1, 1], [2., 2.],[1.3, 1.3], [2,
4.8]])
print(prob_results)
Output:

runfile('/home/akbar/working directory/MLPClassifier classifier2.py',


wdir='/home/akbar/working directory')
Reloaded modules:
mp_main
score on train data: 1.0
score on test data: 0.95
MLPClassifier(alpha=1e-05, hidden_layer_sizes=(5, 2), random_state=1,
solver='lbfgs')
weights between input and first hidden layer:
[[-0.14203691 -1.18304359 -0.85567518 -4.53250719 -0.60466275]
[-0.69781111 -3.5850093 -0.26436018 -4.39161248 0.06644423]]
19

AI lab 2nd intenal…. 6


weights between first hidden and second hidden layer:
[[ 0.29179638 -0.14155284]
[ 4.02666592 -0.61556475]
[-0.51677234 0.51479708]
[ 7.37215202 -0.31936965]
[ 0.32920668 0.64428109]]
w0 = -0.1420369126782716
w1 = -0.6978111149778686
0 0 [-0.14203691 -0.69781111],
0 1 [-1.18304359 -3.5850093 ],
0 2 [-0.85567518 -0.26436018],
0 3 [-4.53250719 -4.39161248],
0 4 [-0.60466275 0.06644423],
Bias values for first hidden layer:
1 0 [ 0.29179638 4.02666592 -0.51677234 7.37215202 0.32920668],
1 1 [-0.14155284 -0.61556475 0.51479708 -0.31936965 0.64428109],
Bias values for first hidden layer:
2 0 [-4.96774269 -0.86330397],
Bias values for first hidden layer:
[-0.14962269 -0.59232707 -0.5472481 7.02667699 -0.87510813]
Bias values for second hidden layer:
[-3.61417672 -0.76834882]
[[1.00000000e+000 5.25723951e-101]
[1.00000000e+000 3.71534882e-031]
[1.00000000e+000 6.47069178e-029]
[1.00000000e+000 3.71534882e-031]
[2.07145538e-004 9.99792854e-001]
[2.07145538e-004 9.99792854e-001]
[2.07145538e-004 9.99792854e-001]
[2.07145538e-004 9.99792854e-001]

Set3: Any two NLP Programs


Remove stop words:

AI lab 2nd intenal…. 7


Python program:
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
example_sent = """This is a sample sentence,
showing off the stop words filtration."""
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)
filtered_sentence = [w for w in word_tokens if not w.lower() instop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)
Output:
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing',
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']

Implement stemming:
Python program:

from nltk.stem import PorterStemmer


from nltk.tokenize import word_tokenize
ps = PorterStemmer()

words = ["program", "programs", "programmer", "programming","programmers"]


for w in words:
print(w, " : ", ps.stem(w))
Output:
program : program
programs : program
programmer : program
programming : program
programmers : program

AI lab 2nd intenal…. 8


Set4: Game bot & chat bot programs.

Game bot:
def generate_number():
return random.randint(1, 100)
def play_game():
number_to_guess = generate_number()attempts = 0
print("Welcome to the Guessing Game!")
print("I have chosen a number between 1 and 100. Try to guessit.")
while True:
guess = input("Enter your guess (or 'exit' to quit): ")
if guess.lower() == "exit":
print("The number was:", number_to_guess)
print("Thanks for playing! Goodbye!")
break
try:
guess = int(guess)
attempts += 1
if guess < number_to_guess:
print("Too low! Try again.")
elif guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts}
attempts.")
break
except ValueError:
print("Invalid input. Please enter a number.")

play_game()
Output:
Welcome to the Guessing Game!
I have chosen a number between 1 and 100. Try to guess it.
Enter your guess (or 'exit' to quit): 35

AI lab 2nd intenal…. 9


Too low! Try again.
Enter your guess (or 'exit' to quit): 56
Too high! Try again.
Enter your guess (or 'exit' to quit): 33
Too low! Try again.
Enter your guess (or 'exit' to quit): 45
Too high! Try again.
Enter your guess (or 'exit' to quit): 40
Congratulations! You guessed the number in 5 attempts

Chat bot
!pip install nltk
import nltk
from nltk.chat.util import Chat, reflections
pairs = [
[
r"my name is (.
)",
["Hello %1, how can I help you today?",]
],
[
r"what is your name?",
["My name is ChatBot and I'm here to assist you.",]
],
[
r"how are you ?",
["I'm doing well, thank you!", "I am always here to help.",]
],
[
r"sorry (.
)",
["No problem, please tell me how can I assist you?",]
],

AI lab 2nd intenal…. 10


[
r"quit",
["Bye! Take care. :)", "Goodbye, have a great day!"]
],
]

chatbot = Chat(pairs, reflections)


print("Hi! I'm ChatBot. How can I assist you today?")
chatbot.converse()

Output:
Requirement already satisfied: nltk in /usr/local/lib/python3.10/distpackages (3.8.1)
Requirement already satisfied: click in
/usr/local/lib/python3.10/dist-packages (from nltk) (8.1.7) Requirement
already satisfied: joblib in /usr/local/lib/python3.10/dist-packages
(from nltk) (1.3.2) Requirement already satisfied: regex>=2021.8.3 in
/usr/local/lib/python3.10/dist-packages (from nltk) (2023.12.25)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/distpackages
(from nltk) (4.66.1)
Hi! I'm ChatBot. How can I assist you today?

what is your name?


My name is ChatBot and I'm here to assist you.
how are you? I'm doing well, thank you!
sorry
None
sorry dear
No problem, please tell me how can I assist you?
bye
None
quit
Goodbye, have a great day

AI lab 2nd intenal…. 11

You might also like