Artificial Intelligence Lab 7
Artificial Intelligence Lab 7
Lab No: 07
Task No: 02
Modify Task 1 and perform it for all odd values of k from 1 to 10.
• Find accuracy for each value of k and display.
• Find the best k which gives highest value of accuracy and worst
k which gives worstaccuracy
• Also compute confusion matrix for both best and worst value of
k.
Code:
from sklearn.metrics import confusion_matrix, accuracy_score
data = [[25, 40000], [35, 60000], [45, 80000], [20, 20000], [35,
120000],
[52, 18000], [23, 95000], [40, 62000], [60, 100000], [48,
220000]]
labels = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
test_data = [[33, 150000], [48, 142000]]
test_labels = [1, 1]
best_k = 1
worst_k = 1
high_acc = 0
low_acc = 1
Task No: 04
Develop a python program to implement Bayesian classification
model for the following dataset and classify the given test vector:
Code:
from sklearn.naive_bayes import GaussianNB
data = [[25, 40000], [35, 60000], [45, 80000], [20, 20000], [35,
120000],
[52, 18000], [23, 95000], [40, 62000], [60, 100000], [48,
220000], [33, 150000]]
labels = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
model = GaussianNB()
model.fit(data, labels)
result = model.predict([[48, 142000]])
print(result)
Output:
Task No: 05
Use the given cancer dataset and classify it using Bayesian
classification model:
• First create a python script and load ‘breast_cancer’ file.
• Identify features and classes from the loaded dataset.
• Perform 2-fold cross validation on the dataset by splitting it into
testing and training parts.
• Implement a Bayesian classifier using the above algorithm and
use training dataset to classify each of the sample within testing
dataset.
• Compute the accuracy from the predicted test samples.
Code:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import KFold
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix
data = load_breast_cancer()
x = data.data
y = data.target
kf = KFold(n_splits=2)
fold = 1
for train_index, test_index in kf.split(x):
print(f"\n--- Fold {fold} ---")
x_train = x[train_index]
x_test = x[test_index]
y_train = y[train_index]
y_test = y[test_index]
model = GaussianNB()
model.fit(x_train, y_train)
pred = model.predict(x_test)
print("Accuracy:", accuracy_score(y_test, pred))
print("Confusion Matrix:")
print(confusion_matrix(y_test, pred))
fold += 1
Output:
Conclusion:
In conclusion, the implementation of K-Nearest Neighbors and Naïve
Bayes classification models provided valuable insights into two
distinct approaches to solving classification problems. KNN
demonstrated its effectiveness through simplicity and flexibility,
especially when dealing with well-structured data, while Naïve Bayes
showed strong performance in cases where the assumption of feature
independence holds true. Through this lab, we observed the
importance of selecting appropriate algorithms based on dataset
characteristics and problem requirements. Overall, the practical
application of these models helped reinforce theoretical concepts and
improved our understanding of how different machine learning
techniques can be applied to real-world data classification tasks.