Atul MLT Exp 4-11
Atul MLT Exp 4-11
Object: Build an Artificial Neural Network by implementing the Backpropagation algorithm and test
the same using appropriate data sets.
import numpy as np
def sigmoid_derivative(x):
return x * (1 - x)
= np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
outputs = np.array([
[0],
[1],
[1],
[0]
])
hidden_layer_neurons = 2 output_neurons
=1
hidden_layer_neurons) - 1 weights_hidden_output = 2 *
np.random.rand(hidden_layer_neurons, output_neurons) - 1
= np.random.rand(1, output_neurons)
hidden_output = sigmoid(hidden_input)
predicted_output = sigmoid(final_input)
predicted_output
sigmoid_derivative(predicted_output)
error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T)
if epoch % 1000 == 0:
loss = np.mean(np.square(error))
after Training:")
print(predicted_output)
OUTPUT
Object: Write a program to implement the naïve Bayesian classifier for a sample training data set
stored as a CSV file. Compute the accuracy of the classifier, considering few test data sets.
import csv
def load_csv(filename):
reader = csv.reader(f)
data = list(reader)
header, data[1:]
separated = defaultdict(list)
label = row[class_index]
separated[label].append(row) return
separated
= {}
total = len(rows)
feature_summary = {}
counts = defaultdict(int)
summaries[class_value] = {
'features': feature_summary
return summaries
max(probabilities, key=probabilities.get)
if __name__ == "__main__":
summarize_by_class(train_data, header)
# Predictions
print("\nPredictions:") for
row in test_data:
OUTPUT :
Object: Assuming a set of documents that need to be classified, use the naïve Bayesian Classifier
model to perform this task. Built-in Java classes/API can be used to write the program. Calculate the
accuracy, precision, and recall for your data set. from sklearn.feature_extraction.text import
CountVectorizer from sklearn.naive_bayes import MultinomialNB
=[
= CountVectorizer()
= MultinomialNB()
model.fit(X_train_counts, y_train)
model.predict(X_test_counts)
print(f"Accuracy : {accuracy:.2f}")
: {recall:.2f}")
OUTPUT
Object: Write a program to construct a Bayesian network considering medical data. Use this model to
demonstrate the diagnosis of heart patients using standard Heart Disease Data Set. You can use
Java/Python ML library classes/API. import pandas as pd
databases/heart-disease/heart-disease.data"
column_names = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang', 'oldpeak', 'slope',
'ca', 'thal', 'num']
= LabelEncoder()
data['sex'] = encoder.fit_transform(data['sex'])
= encoder.fit_transform(data['fbs']) data['restecg'] =
encoder.fit_transform(data['restecg']) data['exang'] =
encoder.fit_transform(data['exang']) data['slope'] =
encoder.fit_transform(data['slope']) data['ca'] =
encoder.fit_transform(data['ca']) data['thal'] =
encoder.fit_transform(data['thal']) data['num'] =
encoder.fit_transform(data['num'])
= BayesianNetwork([
('age', 'num'),
('sex', 'num'),
('cp', 'num'),
('trestbps', 'num'),
('chol', 'num'),
('fbs', 'num'),
('restecg', 'num'),
('thalach', 'num'),
('exang', 'num'),
('oldpeak', 'num'),
('slope', 'num'),
('ca', 'num'),
('thal', 'num')
])
# Example CPDs (this should ideally be learned or based on domain knowledge) age_cpt
= CPT(
['age', 'num'],
[2, 2],
[[0.9, 0.1], [0.2, 0.8]] # Random probabilities for age to be healthy (0) or diseased (1)
['sex', 'num'],
[2, 2],
[[0.7, 0.3], [0.3, 0.7]] # Random probabilities for sex to be healthy (0) or diseased (1)
# You would need to create CPTs for all other variables similarly. Here, we'll just add the age and sex
CPDs for simplicity.
model.add_cpds(age_cpt, sex_cpt)
= VariableElimination(model)
# Step 7: Make predictions on new patient data (for example) patient_data = {'age': 60, 'sex': 1, 'cp':
# Convert patient data to format that matches the variables in the model query
= inference.query(variables=['num'], evidence=patient_data)
OUTPUT
Object: 8. Apply EM algorithm to cluster a set of data stored in a CSV file. Use the same data set for
clustering using k-Means algorithm. Compare the results of these two algorithms and comment on the
quality of clustering. You can add Python ML library classes/API in the program. import pandas as pd
import numpy as np
matplotlib.pyplot as plt
# Step 1: Load the dataset (Replace with the path to your CSV file)
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" # Example URL for Iris
dataset
# Step 2: Prepare the data (features only, remove the target column 'species')
X = data.drop('species', axis=1)
gmm_labels = gmm.fit_predict(X)
KMeans(n_clusters=3, random_state=42)
kmeans_labels = kmeans.fit_predict(X)
silhouette_score(X, kmeans_labels)
axes = plt.su
OUTPUT
Object: Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set.
Print both correct and wrong predictions. Python ML library classes can be used for this problem.
import pandas as pd
# Features y = iris.target #
Labels
y_train)
= knn.predict(X_test)
{accuracy * 100:.2f}%")
correct_predictions = [] incorrect_predictions
= []
if true_label == predicted_label:
else:
OUTPUT
Object: Implement the non-parametric Locally Weighted Regression algorithm in order to fit data
points. Select appropriate data set for your experiment and draw graphs.
matplotlib.pyplot as plt
xi) / (2 * tau**2))
Main routine
# Plotting plt.figure(figsize=(10,
6))
Output:
• A smooth curve that locally fits the data based on tau.