0% found this document useful (0 votes)
2 views16 pages

Pattern Recognition Lab

The document outlines a series of experiments using MATLAB/Python for various computational tasks including operations on arrays, Gaussian distribution, gradient descent, linear regression, Newton-Raphson method, and classification using SVM, ELM, and CNN. Each experiment includes problem statements, code implementations, and expected outputs. The experiments demonstrate practical applications of mathematical concepts and machine learning techniques.

Uploaded by

071Koustav Das
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)
2 views16 pages

Pattern Recognition Lab

The document outlines a series of experiments using MATLAB/Python for various computational tasks including operations on arrays, Gaussian distribution, gradient descent, linear regression, Newton-Raphson method, and classification using SVM, ELM, and CNN. Each experiment includes problem statements, code implementations, and expected outputs. The experiments demonstrate practical applications of mathematical concepts and machine learning techniques.

Uploaded by

071Koustav Das
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

EXPERIMENT 1

Problem Statement: Study the MATLAB/ Python tool boxes, and Operations on Arrays, Functions and
Files.

Program

a. Program to perform addition and subtraction operation on two Vectors.

import numpy as np

A = [Link]([7, 11, 15, 23, 9])

B = [Link]([2, 5, 13, 16, 20])

C=A+B

D=A-B

print("Element-wise sum (A + B):", C)

print("Element-wise difference (A - B):", D)

Output

b. Program to perform addition and subtraction operation on two Matrix.

import numpy as np

A = [Link]([[7, 11, 15], [23, 9, 5]])

B = [Link]([[2, 5, 13], [16, 20, 3]])

C=A+B

D=A-B

print("Element-wise sum (A + B):\n", C)

print("Element-wise difference (A - B):\n", D)

Output
c. Length, Dimension and Number of elements in Array.

length_A = len(A)

dimension_A = [Link]

num_elements_A = [Link]

print(f"Array A:\n{A}")

print(f"Length (number of rows/elements in first dimension) of A: {length_A}")

print(f"Dimension of A: {dimension_A}")

print(f"Number of elements in A: {num_elements_A}")

Output

EXPERIMENT 2

Problem Statement: Write a MATLAB/Python function that computes the value of the Gaussian
distribution N (m,S), at a given vector x. Hence, plot the effect of varying mean and variance to the
normal distributions.

Program

import numpy as np

def gaussian_pdf(x, mean, variance):

exponent_term = (-0.5 * ((x - mean)**2)) / variance

coefficient_term = 1 / [Link](2 * [Link] * variance)

return coefficient_term * [Link](exponent_term)

import numpy as np

x_test = [Link]([-2, -1, 0, 1, 2])

mean_test = 0

variance_test = 1

pdf_values = gaussian_pdf(x_test, mean_test, variance_test)

print(f"Test x values: {x_test}")


print(f"Test mean: {mean_test}")

print(f"Test variance: {variance_test}")

print(f"Computed PDF values: {pdf_values}")

import [Link] as plt

x_values = [Link](-10, 10, 1000)

constant_variance = 1.0

mean_values = [-3, 0, 3]

[Link](figsize=(10, 6))

for mean_val in mean_values:

pdf_y_values = gaussian_pdf(x_values, mean_val, constant_variance)

[Link](x_values, pdf_y_values, label=f'Mean = {mean_val}')

[Link]('Gaussian PDF with Varying Mean (Variance = 1.0)')

[Link]('x')

[Link]('Probability Density')

[Link]()

[Link](True)

[Link]()

Output
EXPERIMENT 3

Problem Statement: Write a MATLAB/Python function that computes and plot the Gradient Descent.

Program

import [Link] as plt

import numpy as np

initial_x = 4.0

learning_rate = 0.1

num_iterations = 20

history = gradient_descent(objective_function, derivative_function, initial_x, learning_rate,


num_iterations)

x_plot = [Link](-5, 5, 400)

y_plot = objective_function(x_plot)

[Link](figsize=(10, 6))

[Link](x_plot, y_plot, label='Objective Function $f(x) = x^2$')

history_y = [objective_function(x) for x in history]

[Link](history, history_y, 'ro-', markersize=8, label='Gradient Descent Path')

[Link](history[0], history_y[0], 'go', markersize=10, label='Starting Point')

[Link](history[-1], history_y[-1], 'ko', markersize=10, label='Final Point')

[Link]('Gradient Descent Optimization Path')

[Link]('x')

[Link]('$f(x)$')

[Link]()

[Link](True)

[Link]()

print(f"Initial x: {initial_x}")

print(f"Final x after {num_iterations} iterations: {history[-1]}")

print(f"Final f(x) value: {objective_function(history[-1])}")

Output
EXPERIMENT 4

Problem Statement: Implementation of Linear Regression using Gradient Descent.

Program

import [Link] as plt

import numpy as np

def hypothesis(X, m, b):

return m * X + b

def cost_function(X, y, m, b):

n_samples = len(y)

predictions = hypothesis(X, m, b)

return [Link]((predictions - y)**2) / 2

def gradients(X, y, m, b):

n_samples = len(y)

predictions = hypothesis(X, m, b)

dm = [Link]((predictions - y) * X)
db = [Link](predictions - y)

return dm, db

def gradient_descent(X, y, initial_m, initial_b, learning_rate, num_iterations):

m = initial_m

b = initial_b

cost_history = []

for i in range(num_iterations):

dm, db = gradients(X, y, m, b)

m = m - learning_rate * dm

b = b - learning_rate * db

cost = cost_function(X, y, m, b)

cost_history.append(cost)

return m, b, cost_history

if 'X' not in globals() or 'y' not in globals():

n_samples = 100

true_m = 2

true_b = 5

noise_std = 2

X = [Link](n_samples) * 10

y = true_m * X + true_b + [Link](n_samples) * noise_std

initial_m = 0.0

initial_b = 0.0

learning_rate = 0.01

num_iterations = 1000

m_initial = initial_m

b_initial = initial_b

final_m, final_b, cost_history = gradient_descent(X, y, initial_m, initial_b, learning_rate,


num_iterations)

print(f"Initial m: {m_initial:.2f}, Initial b: {b_initial:.2f}")

print(f"Final m: {final_m:.2f}, Final b: {final_b:.2f}")

print(f"Final Cost: {cost_history[-1]:.2f}")


x_line = [Link](min(X), max(X), 100)

y_initial_pred = hypothesis(x_line, m_initial, b_initial)

y_final_pred = hypothesis(x_line, final_m, final_b)

[Link](figsize=(14, 6))

[Link](1, 2, 1)

[Link](X, y, label='Generated Data Points', alpha=0.7)

[Link](x_line, y_initial_pred, color='orange', linestyle='--', label='Initial Regression Line')

[Link](x_line, y_final_pred, color='red', label='Final Regression Line')

[Link]('X (Feature)')

[Link]('y (Target)')

[Link]('Linear Regression with Gradient Descent')

[Link]()

[Link](True)

[Link](1, 2, 2)

[Link](range(num_iterations), cost_history, color='blue')

[Link]('Number of Iterations')

[Link]('Cost (MSE)')

[Link]('Cost Function Over Iterations')

[Link](True)

plt.tight_layout()

[Link]()

Output
EXPERIMENT 5

Problem Statement: Implementation of Newton Raphson method.

import numpy as np

def f_newton(x):

return x**3 - x

def df_newton(x):

return 3 * x**2 - 1

import [Link] as plt

initial_guess = 2.0

tolerance = 1e-6

max_iterations = 100

approx_root, history = newton_raphson(f_newton, df_newton, initial_guess, tolerance,


max_iterations)

print(f"Initial Guess: {initial_guess}")

print(f"Approximate Root found: {approx_root:.6f}")

print(f"History of guesses: {history}")

x_vals = [Link](-2.5, 2.5, 400)

y_vals = f_newton(x_vals)

[Link](figsize=(10, 7))
[Link](x_vals, y_vals, label='$f(x) = x^3 - x$', color='blue')

[Link](0, color='gray', linestyle='--', linewidth=0.75, label='y=0 (Root Line)')

history_y = [f_newton(x) for x in history]

[Link](history, history_y, 'ro', markersize=6, linestyle='--', label='Newton-Raphson Path') # Fixed:


removed '-' from 'ro-'

[Link](history[0], history_y[0], 'go', markersize=8, label='Initial Guess')

[Link](approx_root, f_newton(approx_root), 'kx', markersize=10, mew=2, label='Approximate Root')

[Link]('Newton-Raphson Method Convergence for $f(x) = x^3 - x$')

[Link]('x')

[Link]('$f(x)$')

[Link]()

[Link](True)

[Link](-5, 5)

[Link]()

Output
EXPERIMENT 6

Problem Statement: Comparison of Classification Accuracy of SVM, ELM and CNN for a given
dataset.

Program

from [Link] import fetch_openml

from sklearn.model_selection import train_test_split

from [Link] import to_categorical

print("Loading MNIST dataset...")

mnist = fetch_openml('mnist_784', version=1, parser='auto')

print("MNIST dataset loaded successfully.")

X, y = [Link], [Link]

print("Splitting data into training and testing sets...")

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

print(f"Training features shape: {X_train.shape}")

print(f"Testing features shape: {X_test.shape}")

print(f"Training labels shape: {y_train.shape}")

print(f"Testing labels shape: {y_test.shape}")

print("Scaling feature data...")

X_train = X_train.astype('float32') / 255.0

X_test = X_test.astype('float32') / 255.0

print("Feature data scaled.")

print("Reshaping feature data for CNN compatibility...")

X_train = X_train.[Link](-1, 28, 28, 1)

X_test = X_test.[Link](-1, 28, 28, 1)

print(f"Reshaped Training features shape: {X_train.shape}")

print(f"Reshaped Testing features shape: {X_test.shape}")

print("One-hot encoding target labels...")

y_train_encoded = to_categorical(y_train, num_classes=10)

y_test_encoded = to_categorical(y_test, num_classes=10)

print(f"Encoded Training labels shape: {y_train_encoded.shape}")

print(f"Encoded Testing labels shape: {y_test_encoded.shape}")


from [Link] import SVC

from [Link] import accuracy_score

X_train_flat = X_train.reshape(-1, 28 * 28)

X_test_flat = X_test.reshape(-1, 28 * 28)

print(f"Flattened Training features shape: {X_train_flat.shape}")

print(f"Flattened Testing features shape: {X_test_flat.shape}")

svm_model = SVC(kernel='rbf', random_state=42)

print("Training SVM model...")

svm_model.fit(X_train_flat, y_train)

print("SVM model training complete.")

y_pred_svm = svm_model.predict(X_test_flat)

svm_accuracy = accuracy_score(y_test, y_pred_svm)

print(f"SVM Model Accuracy on the test set: {svm_accuracy:.4f}")

import numpy as np

from [Link] import accuracy_score

class CustomELMClassifier:

def __init__(self, n_neurons=1000, activation_func='relu', random_state=None):

self.n_neurons = n_neurons

self.activation_func = activation_func

self.random_state = random_state

self.input_weights = None

[Link] = None

self.output_weights = None

if self.random_state is not None:

[Link](self.random_state)

def _activate(self, X):

if self.activation_func == 'relu':

return [Link](0, X)

elif self.activation_func == 'sigmoid':

return 1 / (1 + [Link](-X))

else:
raise ValueError("Activation function not supported")

def fit(self, X, y):

n_input_features = [Link][1]

n_output_classes = [Link][1]

self.input_weights = [Link](-1, 1, (n_input_features, self.n_neurons))

[Link] = [Link](-1, 1, (1, self.n_neurons))

H_input = [Link](X, self.input_weights) + [Link]

H = self._activate(H_input)

self.output_weights = [Link]([Link](H), y)

def predict_proba(self, X):

H_input = [Link](X, self.input_weights) + [Link]

H = self._activate(H_input)

return [Link](H, self.output_weights)

def predict(self, X):

return probabilities

print("Initializing Custom ELM Classifier...")

elm_model = CustomELMClassifier(n_neurons=1000, activation_func='relu', random_state=42)

print("Training ELM model...")

elm_model.fit(X_train_flat, y_train_encoded)

print("ELM model training complete.")

y_pred_elm_raw = elm_model.predict(X_test_flat)

y_pred_elm = [Link](y_pred_elm_raw, axis=1)

y_test_numeric = y_test.astype(int).to_numpy()

elm_accuracy = accuracy_score(y_test_numeric, y_pred_elm)

print(f"ELM Model Accuracy on the test set: {elm_accuracy:.4f}")

from [Link] import Sequential

from [Link] import Conv2D, MaxPooling2D, Flatten, Dense

model = Sequential()

[Link](Conv2D(32, (3, 3), activation='relu', input_shape=X_train.shape[1:]))

[Link](MaxPooling2D((2, 2)))

[Link](Conv2D(64, (3, 3), activation='relu'))


[Link](MaxPooling2D((2, 2)))

[Link](Flatten())

[Link](Dense(128, activation='relu'))

[Link](Dense(10, activation='softmax'))

[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

print("CNN Model Summary:")

[Link]()

print("\nTraining CNN model...")

[Link](X_train, y_train_encoded, epochs=5, batch_size=32, verbose=1)

print("CNN model training complete.")

print("\nEvaluating CNN model on test data...")

loss, cnn_accuracy = [Link](X_test, y_test_encoded, verbose=0)

print(f"CNN Model Accuracy on the test set: {cnn_accuracy:.4f}")

Output
EXPERIMENT 7

Problem Statement: Image Classification Using Artificial Neural Networks.

Program

import pandas as pd

import numpy as np

from [Link] import Sequential

from [Link] import Dense

from [Link] import to_categorical


from sklearn.model_selection import train_test_split

from [Link] import MinMaxScaler

data = pd.read_csv("[Link]")

X = [Link]('label', axis=1)

y = data['label']

scaler = MinMaxScaler()

X = scaler.fit_transform(X)

y = to_categorical(y, num_classes=10)

X_train, X_val, y_train, y_val = train_test_split(

X, y, test_size=0.2, random_state=42

model = Sequential()

[Link](Dense(600, activation='relu', input_dim=784))

[Link](Dense(400, activation='relu'))

[Link](Dense(200, activation='relu'))

[Link](Dense(10, activation='softmax'))

[Link](

optimizer='sgd',

loss='categorical_crossentropy',

metrics=['accuracy']

[Link](

X_train, y_train,

batch_size=32,

epochs=10,

validation_data=(X_val, y_val)

test_data = pd.read_csv("[Link]")

test_data = [Link](test_data)

predictions = [Link](test_data)

final_output = [Link](predictions, axis=1)


print(final_output)

Output

You might also like