Aml - Lab (1-6)
Aml - Lab (1-6)
EXPERIMENT: 1
The probability that it is Friday and that a student is absent is 3% since there are 5
school days in a week. the probability that it is Friday is 20% what is probability that a
student is absent given that today is Friday? apply baye's rule in python to get the result.
PROGRAM:
# Function to calculate conditional probability using Bayes' Rule
def calculate_conditional_probability(P_joint, P_event):
"""
Calculate P(A | B) using Bayes' Rule:
P(A | B) = P(A and B) / P(B)
:param P_joint: Probability of A and B (P(A and B))
:param P_event: Probability of B (P(B))
:return: Conditional probability P(A | B)
"""
return P_joint / P_event
# Given probabilities
P_Friday_and_Absent = 0.03 # P(Friday and Absent)
P_Friday = 0.20 # P(Friday)
OUTPUT:
The probability that a student is absent given that today is Friday is: 0.15
EXPERIMENT: 2
Create a k-means clustering algorithms from scratch in python?
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
class KMeans:
def init (self, k=3, max_iters=100, tolerance=1e-4):
"""
K-Means clustering algorithm.
Args:
k (int): Number of clusters.
max_iters (int): Maximum number of iterations.
tolerance (float): Tolerance for convergence.
"""
self.k = k
self.max_iters = max_iters
self.tolerance = tolerance
self.centroids = None
self.labels = None
Args:
data (numpy.ndarray): Input data array of shape (n_samples, n_features).
"""
# Randomly initialize centroids
np.random.seed(42)
random_indices = np.random.permutation(data.shape[0])[:self.k]
self.centroids = data[random_indices]
for i in range(self.max_iters):
# Assign clusters
self.labels = self._assign_clusters(data)
Args:
data (numpy.ndarray): Input data array.
Returns:
numpy.ndarray: Cluster labels for each data point.
"""
distances = np.linalg.norm(data[:, np.newaxis] - self.centroids, axis=2)
return np.argmin(distances, axis=1)
Args:
data (numpy.ndarray): Input data array.
Returns:
numpy.ndarray: New centroids.
"""
return np.array([data[self.labels == i].mean(axis=0) for i in range(self.k)])
Args:
data (numpy.ndarray): Input data array.
Returns:
numpy.ndarray: Cluster
labels. """
return self._assign_clusters(data)
# Example usage
if name == " main ":
# Generate synthetic data
from sklearn.datasets import make_blobs
data, _ = make_blobs(n_samples=300, centers=3, cluster_std=0.6, random_state=42)
# Apply K-Means
kmeans = KMeans(k=3)
kmeans.fit(data)
OUTPUT:
EXPERIMENT: 3
Implementation k-nearest neighbours classification using python.
PROGRAM:
import numpy as np
from collections import Counter
class KNearestNeighbors:
def init (self, k=3):
"""
K-Nearest Neighbors classifier.
Args:
k (int): Number of neighbors to consider.
"""
self.k = k
self.data = None
self.labels = None
Args:
X (numpy.ndarray): Training data of shape (n_samples, n_features).
y (numpy.ndarray): Labels of shape (n_samples,).
"""
self.data = X
self.labels = y
Args:
X (numpy.ndarray): Input data of shape (n_samples, n_features).
Returns:
numpy.ndarray: Predicted class labels.
"""
predictions = [self._predict_single_point(x) for x in X]
return np.array(predictions)
Returns:
int/str: Predicted class label.
"""
# Compute distances to all training points
distances = np.linalg.norm(self.data - x, axis=1)
# Majority vote
most_common = Counter(k_labels).most_common(1)
return most_common[0][0]
# Example usage
if name == " main ":
# Generate synthetic dataset
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
OUTPUT:
Accuracy: 0.83
EXPERIMENT: 4
Given the following data. which specify classification for nine combinations of VAR1
and VAR2 predict a classification for a case where VAR1=0.906 VAR2=0.606. using the
result of k-means clustering with 3 means (i.e., 3 centroids) VAR1 VAR2 CLASS 1.713 1.586
0 0.180
1.786 1 0.353 1.240 1 0.940 1.566 0 1.486 0.759 1 1.266 1.106 0 1.540 0.419 1 0.459 1.799 1
0.773 0.186 1
PROGRAM:
import numpy as np
from sklearn.cluster import KMeans
from collections import Counter
# Given data
data = np.array([
[1.713, 1.586],
[0.180, 1.786],
[0.353, 1.240],
[0.940, 1.566],
[1.486, 0.759],
[1.266, 1.106],
[1.540, 0.419],
[0.459, 1.799],
[0.773, 0.186]
])
OUTPUT:
The predicted class for VAR1=0.906 and VAR2=0.606 is: 1
EXPERIMENT: 5
The following training examples map descriptions of individuals onto high, medium
and low credit-worthiness.
Input attributes are (from left to right) income, recreation, job, status, age group, home-
owner. Find the unconditional probability of ‘golf’ and the conditional probability of ‘single’
given ‘med Risk’ in the dataset?
PROGRAM:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt
df = pd.DataFrame(data)
class LinearRegression:
def init (self, learning_rate=0.01, n_iterations=1000):
self.learning_rate = learning_rate
self.n_iterations = n_iterations
self.weights = None
self.bias = None
Args:
X (numpy.ndarray): Feature matrix (n_samples, n_features).
y (numpy.ndarray): Target vector (n_samples,).
"""
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
# Gradient descent
for _ in range(self.n_iterations):
y_predicted = np.dot(X, self.weights) + self.bias
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
Args:
X (numpy.ndarray): Feature matrix (n_samples, n_features).
Returns:
numpy.ndarray: Predicted target values.
"""
return np.dot(X, self.weights) + self.bias
# Example usage
if name == " main ":
# Generate synthetic data
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X.squeeze() + np.random.randn(100) # Linear relation with noise