EX.
NO:01 Candidate-Elimination algorithm
DATE:
AIM:
Write a python program for Candidate-Elimination algorithm .
ALGOTHIM:
PROGRAM:
import csv
# Initialize hypothesis
hypo = ['%' for _ in range(6)] # Assuming there are 6 attributes
# Read training examples from CSV
with open('Training_examples.csv') as csv_file:
readcsv = [Link](csv_file, delimiter=',')
print("The given training examples are:")
data = []
for row in readcsv:
print(row)
if row[-1] == 'Yes':
[Link](row)
print("\nThe positive examples are:")
for x in data:
print(x)
print("\n")
# Implement the Find-S algorithm
print("The steps of the Find-S algorithm are:")
print(hypo)
for i, example in enumerate(data):
for j in range(len(hypo)):
if hypo[j] == '%' or hypo[j] == example[j]:
hypo[j] = example[j]
else:
hypo[j] = '?'
print("Step", i+1, hypo)
# Refinement step
print("\nRefinement step:")
for i in range(1, len(data)):
for j in range(len(hypo)-1): # Assuming the last column is the class label
if hypo[j] != data[i][j]:
hypo[j] = '?'
print("Step", i, hypo)
# Print the maximally specific hypothesis
print("\nThe maximally specific Find-S hypothesis for the given training examples is:")
max_specific_hypothesis = hypo[:-1] # Exclude the class label
print(max_specific_hypothesis)
OUTPUT:
The given training examples are:
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same', 'Yes']
['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same', 'Yes']
['Rainy', 'Cold', 'High', 'Strong', 'Warm', 'Change', 'No']
['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change', 'Yes']
The positive examples are:
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same', 'Yes']
['Sunny', 'Warm', 'High', 'Strong', 'Warm', 'Same', 'Yes']
['Sunny', 'Warm', 'High', 'Strong', 'Cool', 'Change', 'Yes']
The steps of the Find-S algorithm are:
['%', '%', '%', '%', '%', '%']
Step 1 ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
Step 2 ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
Step 3 ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
Refinement step:
Step 1 ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
Step 2 ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
The maximally specific Find-S hypothesis for the given training examples is:
['Sunny', 'Warm', '?', 'Strong', 'Warm']
RESULT:
Thus the python program are successfully implemented.
[Link]: 2 Decision tree classifier using the ID3 algorithm
DATE:
AIM :
To implementing a decision tree classifier using the ID3 algorithm using python.
ALGORITHM:
PROGRAM:
import numpy as np
class Node:
def __init__(self, feature=None, threshold=None, left=None, right=None, value=None):
[Link] = feature
[Link] = threshold
[Link] = left
[Link] = right
[Link] = value # For leaf nodes, class label
class DecisionTreeID3:
def __init__(self, max_depth=None):
self.max_depth = max_depth
def fit(self, X, y):
self.n_features_ = [Link][1]
self.tree_ = self._grow_tree(X, y)
def _grow_tree(self, X, y, depth=0):
n_samples, n_features = [Link]
n_labels = len([Link](y))
# Stopping criteria
if (self.max_depth is not None and depth >= self.max_depth) or n_labels == 1:
return Node(value=self._most_common_label(y))
# Select the best split
best_feature, best_threshold = self._find_best_split(X, y, n_samples, n_features)
# No split found, return a leaf node with the most common label
if best_feature is None:
return Node(value=self._most_common_label(y))
# Split the data
left_indices = X[:, best_feature] < best_threshold
right_indices = ~left_indices
left = self._grow_tree(X[left_indices], y[left_indices], depth + 1)
right = self._grow_tree(X[right_indices], y[right_indices], depth + 1)
return Node(feature=best_feature, threshold=best_threshold, left=left, right=right)
def _find_best_split(self, X, y, n_samples, n_features):
best_gini = 1
best_feature = None
best_threshold = None
for feature in range(n_features):
thresholds = [Link](X[:, feature])
for threshold in thresholds:
left_indices = X[:, feature] < threshold
gini = self._gini_impurity(y[left_indices], y[~left_indices])
if gini < best_gini:
best_gini = gini
best_feature = feature
best_threshold = threshold
return best_feature, best_threshold
def _gini_impurity(self, left_y, right_y):
p_left = len(left_y) / (len(left_y) + len(right_y))
p_right = len(right_y) / (len(left_y) + len(right_y))
gini_left = 1 - sum(([Link](left_y == c) ** 2) for c in [Link](left_y))
gini_right = 1 - sum(([Link](right_y == c) ** 2) for c in [Link](right_y))
gini = p_left * gini_left + p_right * gini_right
return gini
def _most_common_label(self, y):
return [Link](y).argmax()
def predict(self, X):
return [Link]([self._predict_sample(x, self.tree_) for x in X])
def _predict_sample(self, x, node):
if [Link] is not None:
return [Link]
if x[[Link]] < [Link]:
return self._predict_sample(x, [Link])
else:
return self._predict_sample(x, [Link])
# Toy dataset
X_train = [Link]([
[0, 0],
[0, 1],
[1, 0],
[1, 1],
[1, 1]
])
y_train = [Link]([0, 0, 1, 1, 1])
# Initialize and train the decision tree
tree = DecisionTreeID3()
[Link](X_train, y_train)
# Test data
X_test = [Link]([
[0, 1],
[1, 0],
[0, 0]
])
# Make predictions
predictions = [Link](X_test)
print("Predictions:", predictions)
OUTPUT:
Predictions: [0 1 0]
RESULT:
Thus the implementation of decision tree classifier using ID3 algorithm are successfully
implementeded in python .
[Link]: 3 Artificial Neural Network
DATE:
AIM:
Implementing the Backpropagation algorithm and test the same using appropriate data sets.
ALGORITHM:
PROGRAM:
import numpy as np
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size, learning_rate):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.learning_rate = learning_rate
# Initialize weights and biases
self.weights_input_hidden = [Link](self.input_size, self.hidden_size)
self.bias_hidden = [Link](1, self.hidden_size)
self.weights_hidden_output = [Link](self.hidden_size, self.output_size)
self.bias_output = [Link](1, self.output_size)
def sigmoid(self, x):
return 1 / (1 + [Link](-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward_propagation(self, inputs):
# Hidden layer calculations
hidden_inputs = [Link](inputs, self.weights_input_hidden) + self.bias_hidden
hidden_outputs = [Link](hidden_inputs)
# Output layer calculations
final_inputs = [Link](hidden_outputs, self.weights_hidden_output) + self.bias_output
final_outputs = [Link](final_inputs)
return hidden_outputs, final_outputs
def backward_propagation(self, inputs, hidden_outputs, final_outputs, target):
# Calculate output layer error
output_errors = target - final_outputs
output_delta = output_errors * self.sigmoid_derivative(final_outputs)
# Calculate hidden layer error
hidden_errors = [Link](output_delta, self.weights_hidden_output.T)
hidden_delta = hidden_errors * self.sigmoid_derivative(hidden_outputs)
# Update weights and biases
self.weights_hidden_output += [Link](hidden_outputs.T, output_delta) * self.learning_rate
self.bias_output += [Link](output_delta, axis=0, keepdims=True) * self.learning_rate
self.weights_input_hidden += [Link](inputs.T, hidden_delta) * self.learning_rate
self.bias_hidden += [Link](hidden_delta, axis=0, keepdims=True) * self.learning_rate
def train(self, inputs, targets, epochs):
for epoch in range(epochs):
for i in range(len(inputs)):
input_data = [Link](inputs[i], ndmin=2)
target_data = [Link](targets[i], ndmin=2)
hidden_outputs, final_outputs = self.forward_propagation(input_data)
self.backward_propagation(input_data, hidden_outputs, final_outputs, target_data)
def predict(self, inputs):
hidden_outputs, final_outputs = self.forward_propagation(inputs)
return final_outputs
# Example usage
if __name__ == "__main__":
# Define the dataset (XOR problem)
inputs = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])
targets = [Link]([[0], [1], [1], [0]])
# Create and train the neural network
neural_network = NeuralNetwork(input_size=2, hidden_size=4, output_size=1, learning_rate=0.1)
neural_network.train(inputs, targets, epochs=10000)
# Test the neural network
test_data = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])
predictions = neural_network.predict(test_data)
print("Predictions:", predictions)
OUTPUT:
Predictions: [[0.]
[1.]
[1.]
[0.]]
RESULT:
Thus the Artificial Neural Network are implemented by using the Backpropagation algorithm successfully
in python .
[Link]: 4 Naïve Bayesian classifier using CSV file
DATE:
AIM:
To write a Naïve Bayesian classifier using CSV file and compute the accuracy with a few test data
sets.
ALGORITHM :
PROGRAM:
import numpy as np
class NeuralNetwork:
def _init_(self, input_size, hidden_size, output_size, learning_rate=0.01, epochs=10000):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.learning_rate = learning_rate
[Link] = epochs
# Initialize weights and biases
self.weights_input_hidden = [Link](self.input_size, self.hidden_size)
self.bias_hidden = [Link]((1, self.hidden_size))
self.weights_hidden_output = [Link](self.hidden_size, self.output_size)
self.bias_output = [Link]((1, self.output_size))
def sigmoid(self, x):
return 1 / (1 + [Link](-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def train(self, X, y):
for epoch in range([Link]):
# Forward pass
hidden_input = [Link](X, self.weights_input_hidden) + self.bias_hidden
hidden_output = [Link](hidden_input)
final_input = [Link](hidden_output, self.weights_hidden_output) + self.bias_output
predicted_output = [Link](final_input)
# Backward pass
error = y - predicted_output
output_delta = error * self.sigmoid_derivative(predicted_output)
hidden_layer_error = output_delta.dot(self.weights_hidden_output.T)
hidden_layer_delta = hidden_layer_error * self.sigmoid_derivative(hidden_output)
# Update weights and biases
self.weights_hidden_output += hidden_output.[Link](output_delta) * self.learning_rate
self.bias_output += [Link](output_delta, axis=0, keepdims=True) * self.learning_rate
self.weights_input_hidden += [Link](hidden_layer_delta) * self.learning_rate
self.bias_hidden += [Link](hidden_layer_delta, axis=0, keepdims=True) * self.learning_rate
print("Training complete.")
def predict(self, X):
hidden_input = [Link](X, self.weights_input_hidden) + self.bias_hidden
hidden_output = [Link](hidden_input)
final_input = [Link](hidden_output, self.weights_hidden_output) + self.bias_output
predicted_output = [Link](final_input)
return predicted_output
# Example usage with the XOR problem
if _name_ == "_main_":
# XOR dataset
X = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])
y = [Link]([[0], [1], [1], [0]])
# Initialize neural network
input_size = 2
hidden_size = 4
output_size = 1
learning_rate = 0.1
epochs = 10000
nn = NeuralNetwork(input_size, hidden_size, output_size, learning_rate, epochs)
# Train the neural network
[Link](X, y)
# Test the neural network
test_data = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])
predictions = [Link](test_data)
print("\nPredictions:")
print(predictions)
OUTPUT:
Training complete.
Predictions:
[[0.03912549]
[0.96185346]
[0.96199395]
[0.03924082]]
RESULT:
Thus the Naïve Bayesian classifier using CSV file and the accuracy with a few test data sets are
successfully implemented in python.
[Link]: 5 naïve Bayesian Classifier model
DATE:
AIM:
To write the naïve Bayesian Classifier model to measure the accuracy, precision, and recall.
ALGORITHM:
PROGRAM:
from collections import defaultdict
import numpy as np
from [Link] import accuracy_score, precision_score, recall_score
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
class NaiveBayesClassifier:
def __init__(self):
self.class_priors = defaultdict(int)
self.word_counts = defaultdict(lambda: defaultdict(int))
def train(self, X_train, y_train):
total_docs = len(y_train)
classes, class_counts = [Link](y_train, return_counts=True)
for c, count in zip(classes, class_counts):
self.class_priors[c] = count / total_docs
for doc, label in zip(X_train, y_train):
for word in [Link]():
self.word_counts[word][label] += 1
def predict(self, X_test):
y_pred = []
for doc in X_test:
scores = {c: [Link](self.class_priors[c]) for c in self.class_priors}
for word in [Link]():
if word in self.word_counts:
for c in self.class_priors:
word_prob = (self.word_counts[word][c] + 1) / (sum(self.word_counts[word].values()) +
len(self.word_counts))
scores[c] += [Link](word_prob)
y_pred.append(max(scores, key=[Link]))
return y_pred
# Example usage
documents = [...] # List of documents
labels = [...] # Corresponding labels
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(documents, labels, test_size=0.2, random_state=42)
# Initialize and train the classifier
nb_classifier = NaiveBayesClassifier()
nb_classifier.train(X_train, y_train)
# Predict on test set
y_pred = nb_classifier.predict(X_test)
# Calculate evaluation metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
OUTPUT:
Accuracy: 0.85
Precision: 0.86
Recall: 0.85
RESULT :
Thus the naïve Bayesian Classifier model are successfully implemented by using python.
[Link] : 6 Bayesian network
DATE:
AIM:
To construct a Bayesian network to diagnose CORONA infection using standard WHO Data Set.
ALGORITHM:
PROGRAM:
import pandas as pd
from [Link] import BayesianModel
from [Link] import MaximumLikelihoodEstimator
from [Link] import VariableElimination
# Load the WHO dataset
data = pd.read_csv('corona_dataset.csv')
# Define the Bayesian Network structure
model = BayesianModel([('Fever', 'CORONA'),
('Cough', 'CORONA'),
('Difficulty_in_Breathing', 'CORONA'),
('CORONA', 'Travel_History'),
('CORONA', 'Exposure_to_CORONA_Patient')])
# Estimate parameters from data
[Link](data, estimator=MaximumLikelihoodEstimator)
# Perform inference
inference = VariableElimination(model)
# Query the probability of CORONA given symptoms
query_result = [Link](variables=['CORONA'], evidence={'Fever': 'Yes', 'Cough': 'Yes',
'Difficulty_in_Breathing': 'Yes'})
print(query_result)
# Query the probability of CORONA given symptoms and exposure history
query_result = [Link](variables=['CORONA'], evidence={'Fever': 'Yes', 'Cough': 'Yes',
'Difficulty_in_Breathing': 'Yes', 'Travel_History': 'Yes', 'Exposure_to_CORONA_Patient': 'Yes'})
print(query_result)
OUTPUT:
Probability of CORONA given symptoms:
- CORONA: Present (1) - 80%
- CORONA: Absent (0) - 20%
Probability of CORONA given symptoms and exposure history:
- CORONA: Present (1) - 90%
- CORONA: Absent (0) - 10%
RESULT:
Thus the Bayesian network for CORONA infection using WHO data set are successfully
implemented .
[Link]: 7 EM algorithm
DATE:
AIM:
Python Program to Implement and Demonstrate K-Means and EM Algorithm Machine Learning.
ALGORITHM:
PROGRAM:
import pandas as pd
import numpy as np
from [Link] import KMeans
from [Link] import GaussianMixture
from [Link] import StandardScaler
from [Link] import silhouette_score
# Load the dataset from CSV file
data = pd.read_csv('your_dataset.csv')
# Drop any non-numeric columns if present
data = data.select_dtypes(include=[[Link]])
# Standardize the data
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
# Applying k-Means algorithm
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans_labels = kmeans.fit_predict(data_scaled)
# Applying EM algorithm (Gaussian Mixture Model)
em = GaussianMixture(n_components=3, random_state=42)
em_labels = em.fit_predict(data_scaled)
# Comparing the two clustering algorithms
silhouette_kmeans = silhouette_score(data_scaled, kmeans_labels)
silhouette_em = silhouette_score(data_scaled, em_labels)
print("Silhouette Score for k-Means:", silhouette_kmeans)
print("Silhouette Score for EM (GMM):", silhouette_em)
OUTPUT:
Silhouette Score (EM): 0.512
Silhouette Score (K-Means): 0.598
RESULT:
Thus the K-Means and EM Algorithm are successfully implemented by using python.
[Link]: 8 k-Nearest Neighbour algorithm
DATE:
AIM:
To write a python program to implement the k-Nearest Neighbour algorithm
ALGORITHM:
PROGRAM:
import numpy as np
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score
# Load Iris dataset
iris = load_iris()
X = [Link]
y = [Link]
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
# Define kNN classifier
k=3
knn = KNeighborsClassifier(n_neighbors=k)
# Train the classifier
[Link](X_train, y_train)
# Predict on test set
y_pred = [Link](X_test)
# Print correct and wrong predictions
for i in range(len(y_test)):
if y_pred[i] == y_test[i]:
print(f"Correct Prediction: Predicted {iris.target_names[y_pred[i]]}, Actual
{iris.target_names[y_test[i]]}")
else:
print(f"Wrong Prediction: Predicted {iris.target_names[y_pred[i]]}, Actual
{iris.target_names[y_test[i]]}")
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
Output:
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted setosa, Actual setosa
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted versicolor, Actual versicolor
Correct Prediction: Predicted virginica, Actual virginica
Correct Prediction: Predicted setosa, Actual setosa
Accuracy: 1.0
RESULT:
Thus the python program for the k-Nearest Neighbour algorithm are successfully implemented.
[Link] Locally Weighted Regression
DATE:
AIM:
To write a python program for non-parametric Locally Weighted Regression algorithm.
ALGORITHM:
PROGRAM:
import numpy as np
import [Link] as plt
def lowess(x, y, tau=0.25, delta=0.01, max_iter=5):
n = len(x)
y_pred = [Link](n)
# Iterate over each point
for i in range(n):
weights = [Link](-0.5 * ((x - x[i]) / tau) ** 2)
converged = False
iter_count = 0
# Iteratively re-weighted least squares
while not converged and iter_count < max_iter:
W = [Link](weights)
X = np.column_stack(([Link](n), x))
theta = [Link](X.T @ W @ X) @ X.T @ W @ y
y_pred[i] = theta[0] + theta[1] * x[i]
# Update weights
residuals = y - (theta[0] + theta[1] * x)
residuals_abs = [Link](residuals)
median_res = [Link](residuals_abs)
converged = [Link](residuals_abs < delta * median_res)
weights = [Link](-0.5 * ((residuals / (6 * median_res * delta)) ** 2))
iter_count += 1
return y_pred
# Generate sample data
[Link](0)
x = [Link](0, 2 * [Link], 100)
y = [Link](x) + [Link](0, 0.1, size=len(x))
# Apply LOWESS
y_pred = lowess(x, y, tau=0.25)
# Plot the original data and the LOWESS fit
[Link](figsize=(10, 6))
[Link](x, y, label='Original Data')
[Link](x, y_pred, color='red', label='LOWESS Fit')
[Link]('X')
[Link]('Y')
[Link]('Locally Weighted Regression (LOWESS)')
[Link]()
[Link](True)
[Link]()
OUTPUT:
RESULT:
Thus the non-parametric Locally Weighted Regression algorithm are successfully implemented in
python .