0% found this document useful (0 votes)
12 views

ML File - Merged

Nsnncnf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

ML File - Merged

Nsnncnf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

INDEX

S. No. TOPIC DATE TEACHER’S


SIGNATURE

1. Write a Program to Implement Linear Regression 21/02/2024

2. Write a Program to Implement Logistic Regression Classification 28/02/2024

3. Write a Program to Test any Classification Models using Accuracy, Precision, Recall, 03/04/2024
F1-Score, Confusion Matrix and Regression Models using Mean Absolute Error, Mean
Squared Error and Root Mean Squared Error

4. Write a Program to Implement Random Forests for Classification 10/04/2024

5. Write a Program to Implement Gradient Boosting for Classification 17/04/2024

6. Write a Program to Reduce the Number of Dimensions using Principal Component 24/04/2024
Analysis (PCA) and t-Distributed Stochastic Neighbor Embedding (t-SNE)

7. Write a Program to Implement K-Means and Hierarchical Clustering 01/05/2024

8. Write a Program to Implement Autoencoders for Data Compression 08/05/2024

9. Write a Program to Implement Markov Decision Processes 15/05/2024

10. Write a Program to Implement Q-Learning 22/05/2024


Experiment 1
Aim : Write a program to perform linear regression
Software used :- MATLAB
Theory:-
Linear regression is a type of supervised machine learning algorithm that computes the
linear relationship between the dependent variable and one or more independent features by
fitting a linear equation to observed data.
When there is only one independent feature, it is known as Simple Linear Regression, and
when there are more than one feature, it is known as Multiple Linear Regression.
Mathematically,
y=β0+β1X+β2X+………βnX (equation)
Y is the dependent variable
X1, X2, …, Xp are the independent variables
β0 is the intercept
β1, β2, …, βn are the slopes
Cost Function
( (∑(yi^−yi)^2)/n

Code : -
% Sample data

X = [1 2 3 4 5]'; % Input features

y = [2 4 5 4 5]'; % Output values

% Number of training examples

m = length(y);

% Add a column of ones to X (for the intercept term)

X = [ones(m, 1), X];

% Calculate the parameters theta using normal equation

theta = pinv(X' * X) * X' * y;

% Display the parameters

fprintf('Theta computed from normal equation: \n');

fprintf(' %f \n', theta);

% Plot the data


plot(X(:,2), y, 'rx', 'MarkerSize', 10); % Plot the data

hold on;

xlabel('Feature');

ylabel('Output');

title('Linear Regression');

% Plot the linear regression line

plot(X(:,2), X*theta, '-');

legend('Training data', 'Linear regression');

hold off;

Output

Theta computed from normal equation:


75.149882
-0.948892
Experiment 2
Aim:- To write a program to perform Polynomial Regression.
Software used :- MATLAB
Theory :-
Polynomial Regression is a form of linear regression in which the relationship between the
independent variable x and dependent variable y is modelled as an nth-degree polynomial.
Polynomial regression fits a nonlinear relationship between the value of x and the corresponding
conditional mean of y, denoted E(y | x).
Mathematically,
y= b0+b1x1+ b2x12+ b2x13+...... bnx1n

Code:-
% Sample data
X = [1 2 3 4 5]'; % Input features
y = [2 4 5 4 5]'; % Output values
m = length(y);
degree = 2;
% Create polynomial features
X_poly = zeros(m, degree);
for i = 1:degree
X_poly(:, i) = X.^i;
end
X_poly = [ones(m, 1), X_poly];
theta = pinv(X_poly' * X_poly) * X_poly' * y;
fprintf('Theta computed from normal equation: \n');
fprintf(' %f \n', theta);
plot(X, y, 'rx', 'MarkerSize', 10); % Plot the data
hold on;
xlabel('Feature');
ylabel('Output');
title('Polynomial Regression');
x_values = min(X):0.1:max(X);
x_values_poly = zeros(length(x_values), degree);
for i = 1:degree
x_values_poly(:, i) = x_values'.^i;
end
x_values_poly = [ones(length(x_values), 1), x_values_poly];
y_values = x_values_poly * theta;
plot(x_values, y_values, '-');
legend('Training data', 'Polynomial regression');
hold off;

Output:-
Theta computed from normal equation:
30.261131
-0.050338
0.007006
Experiment 3
Aim:- Write a program and perform Logistic Regression
Software used:- MATLAB
Theory:- Logistic regression is used for binary classification where we use sigmoid
function, that takes input as independent variables and produces a probability value
between 0 and 1.
For example, we have two classes Class 0 and Class 1 if the value of the logistic function
for an input is greater than 0.5 (threshold value) then it belongs to Class 1 otherwise it
belongs to Class 0. It’s referred to as regression because it is the extension of linear
regression but is mainly used for classification problems.

Code
% Sample data
X = [1 2 3 4 5]'; % Input features
y = [0 1 0 1 1]'; % Binary output values (0 or 1)
% Add a column of ones to X (for the intercept term)
X = [ones(length(y), 1), X];
% Perform logistic regression using glmfit
[beta, ~, ~] = glmfit(X, y, 'binomial', 'link', 'logit');
% Display the parameters
fprintf('Intercept: %f\n', beta(1));
fprintf('Coefficient for feature: %f\n', beta(2));
% Plot the data and logistic regression curve
plot(X(:,2), y, 'rx', 'MarkerSize', 10);
hold on;
xlabel('Feature');
ylabel('Output');
title('Logistic Regression');
% Generate x values for plotting the curve
x_values = linspace(min(X(:,2)), max(X(:,2)), 100);
x_values_extended = [ones(100, 1), x_values'];
% Compute y values using glmval
y_values = glmval(beta, x_values_extended, 'logit');
% Plot the logistic regression curve
plot(x_values, y_values, '-');
legend('Training data', 'Logistic regression');
hold off;

Output
Intercept: 0.000000
Coefficient for feature: -2.648587
Experiment-4

Aim: Write a program to implement random forests for classification.


Software used:- MATLAB

Theory:
Random Forest is an ensemble learning technique used for classification tasks. It operates by
constructing multiple decision trees during training. Each tree is built on a bootstrap sample of the
training data, and at each node of the tree, a random subset of features is considered for splitting. This
randomness helps to decorrelate the trees and reduce overfitting. During prediction, each tree in the
forest independently classifies the input, and the mode (most frequent class) of all the trees'
predictions is taken as the final prediction. Random Forests are robust against noise, outliers, and
missing values and are less prone to overfitting compared to individual decision trees. They are
widely used due to their simplicity, scalability, and ability to handle high-dimensional data, making
them suitable for a variety of classification tasks in various domains.

Code:
% Step 1: Load the dataset
load fisheriris
X = meas; % features
y = species; % labels
% Step 2: Prepare the data
% Convert the species to categorical data
y = categorical(y);
% Step 3: Split the data into training and testing sets
cv = cvpartition(y, 'HoldOut', 0.3);
X_train = X(training(cv), :);
y_train = y(training(cv), :);
X_test = X(test(cv), :);
y_test = y(test(cv), :);
% Step 4: Train the Random Forest classifier
numTrees = 100;
rng(42); % For reproducibility
mdl = TreeBagger(numTrees, X_train, y_train, 'Method', 'classification');
% Step 5: Make predictions on the test set
y_pred = predict(mdl, X_test);
y_pred = categorical(y_pred);
% Step 6: Evaluate the classifier
accuracy = sum(y_pred == y_test) / numel(y_test);
fprintf('Accuracy: %.2f%%\n', accuracy * 100);
% Confusion Matrix
confMat = confusionmat(y_test, y_pred);
% Step 7: Visualize the results
figure;
confusionchart(confMat, categories(y), 'Title', 'Confusion Matrix', 'RowSummary', 'row-
normalized', 'ColumnSummary', 'column-normalized');

OUTPUT :
Experiment-5

Aim: Write a program to implement gradient boosting for classification.


Software used:- MATLAB

Theory:
Gradient Boosting is a powerful machine learning technique used for both regression and
classification tasks. It works by sequentially fitting simple models, typically decision trees, to the
residuals of the previous models in an ensemble. Each new model is trained to correct the errors made
by the previous ones, gradually improving the overall prediction accuracy. Gradient Boosting
iteratively builds an ensemble of decision trees, where each tree is trained to predict the gradient of
the loss function with respect to the output scores of the previous trees. By combining the predictions
of multiple weak learners, typically shallow decision trees, gradient boosting can create a strong
classifier capable of capturing complex relationships in the data.

Code:
% Step 1: Load the dataset
load fisheriris
X = meas; % features
y = species; % labels
% Step 2: Prepare the data
% Convert the species to categorical data
y = categorical(y);
% Step 3: Split the data into training and testing sets
cv = cvpartition(y, 'HoldOut', 0.3);
X_train = X(training(cv), :);
y_train = y(training(cv), :);
X_test = X(test(cv), :);
y_test = y(test(cv), :);
% Step 4: Train the Gradient Boosting classifier
t = templateTree('MaxNumSplits', 5);
Mdl = fitcensemble(X_train, y_train, 'Method', 'AdaBoostM2', 'NumLearningCycles', 100,
'Learners', t);
% Step 5: Make predictions on the test set
y_pred = predict(Mdl, X_test);
% Step 6: Evaluate the classifier
accuracy = sum(y_pred == y_test) / numel(y_test);
fprintf('Accuracy: %.2f%%\n', accuracy * 100);
% Confusion Matrix
confMat = confusionmat(y_test, y_pred);
% Step 7: Visualize the results
figure;
confusionchart(confMat, categories(y), 'Title', 'Confusion Matrix', 'RowSummary', 'row-
normalized', 'ColumnSummary', 'column-normalized');

OUTPUT :
Experiment-6

Aim: Write a program to reduce the number of dimensions using principal


component analysis (PCA) and t-Distributed Stochastic Neighbor Embedding
(t-SNE).
Software used:- MATLAB

Theory:
Principal Component Analysis (PCA) and t-Distributed Stochastic Neighbor Embedding
(tSNE) are dimensionality reduction techniques used to visualize and explore high
dimensional data in lower-dimensional space. 1. Principal Component Analysis (PCA): PCA
transforms high dimensional data into a lower-dimensional space by identifying the principal
components that capture the maximum variance in the data. It achieves this by finding
orthogonal axes (principal components) along which the data varies the most. By retaining a
subset of these components, PCA effectively reduces the dimensionality while preserving as
much variance as possible. This facilitates visualization and analysis of data while
minimizing information loss. 2. t-Distributed Stochastic Neighbor Embedding (t-SNE): t-
SNE is a nonlinear dimensionality reduction technique that focuses on preserving local
structure and similarities between data points in the high-dimensional space when mapping
them to a lower-dimensional space. It constructs a probability distribution over pairs of high-
dimensional data points and seeks to minimize the KullbackLeibler divergence between this
distribution and a similar distribution in the low dimensional space. t-SNE is particularly
effective for visualizing clusters and capturing intricate structures in the data, making it
suitable for exploratory data analysis and visualization tasks.

Code:
function X_pca = pca(X, n_components)
X_mean = mean(X, 1);
X_centered = X - X_mean;
covariance_matrix = cov(X_centered);
[eigenvectors, eigenvalues] = eig(covariance_matrix, 'vector');
[eigenvalues, idx] = sort(eigenvalues, 'descend');
eigenvectors = eigenvectors(:, idx);
principal_components = eigenvectors(:, 1:n_components);
X_pca = X_centered * principal_components;
end
function distances = compute_pairwise_distances(X)
n = size(X, 1);
distances = zeros(n, n);
for i = 1:n
for j = 1:n
distances(i, j) = norm(X(i, :) - X(j, :));
end
end
end
function probabilities = compute_probabilities(distances, perplexity)
n = size(distances, 1);
probabilities = zeros(n, n);
for i = 1:n
distances(i, i) = 0;
numerator = exp(-distances(i, :) / (2 * perplexity));
denominator = sum(numerator) - 1;
probabilities(i, :) = numerator / (denominator + eps);
end
end
function Y = tSNE(X, n_components, perplexity, learning_rate, n_iters, verbose,
clip_gradients, lr_schedule,
early_stopping)
rng(0);
Y = randn(size(X, 1), n_components);
distances = compute_pairwise_distances(X);
gains = ones(size(Y));
prev_cost = inf;
for i = 1:n_iters
probabilities = compute_probabilities(distances, perplexity);
Q = (1 ./ (1 + sum((Y - permute(Y, [1, 3, 2])).^2, 3))) / 2;
Q(logical(eye(size(Q)))) = 0;
PQ_diff = probabilities - Q;
grad = zeros(size(Y));
for j = 1:size(X, 1)
grad(j, :) = sum(bsxfun(@times, PQ_diff(j, :), (Y(j, :) - Y)), 1);
end
if clip_gradients
grad_norm = norm(grad);
if grad_norm > 1.0
grad = grad / grad_norm;
end
end
if lr_schedule
learning_rate = learning_rate * 0.9;
end
gains = (gains + 0.2) .* ((grad > 0) ~= (gains > 0)) + (gains * 0.8) .* ((grad > 0)
== (gains > 0));
Y = Y - learning_rate * gains .* grad;
cost = sum(probabilities(:) .* log((probabilities + eps) ./ (Q + eps)));
if verbose && mod(i, 100) == 0
fprintf('Iteration %d: Cost = %.4f\n', i, cost);
end
if early_stopping && abs(cost - prev_cost) < 1e-4
fprintf('Early stopping at iteration %d\n', i);
break;
end
prev_cost = cost;
end
end
if false
rng(0);
X = randn(100, 10);
X_pca = pca(X, 2);
fprintf('PCA results:\n');
disp(X_pca);
X_tsne = tSNE(X, 2, 30, 100, 1000, true, true, false, false);
fprintf('\nt-SNE results:\n');
disp(X_tsne);
end

OUTPUT :
PCA results:
[[-0.58286733 2.45981363]
[-1.09081114 1.00786326]
[-1.60664763 1.39106741]
[ 1.37231517 0.33252053]
[ 0.75817845 -1.20168206]
[ 0.43140885 -0.54126264]
[-0.62152646 -0.49271683]
[-0.57558823 0.92950044]
…………
[ 2.68918652 0.18873081]
[-0.95119061 0.38800946]
[-2.42065914 0.1343737 ]
[-0.12466609 -0.27417858]
[-1.55521862 1.72021859]
[-0.62236114 -0.76473871]
[ 0.60525191 0.50687456]]
Iteration 0: Cost = -235.97167187305257
Iteration 100: Cost = -127.31506547439005
Iteration 200: Cost = -135.46951506431597
Iteration 300: Cost = -137.7242278776401
Iteration 400: Cost = -118.06925953193259
Iteration 500: Cost = -132.26012763770737
Iteration 600: Cost = -125.37618961272483
Iteration 700: Cost = -76.05242452020524
Iteration 800: Cost = -137.7851707835792
Iteration 900: Cost = -128.81619111124616
t-SNE results:
[[630.79410232 640.18387175]
[634.93761013 638.09201384]
[630.56516922 638.93196213]
………
[633.28620841 638.27984386]
[630.59277833 634.6848945 ]
[630.08355411 634.64788425]
[631.27528559 634.66920896]
[630.77520612 634.55405962]
[632.56021374 639.41442274]]
Experiment 7
Aim : Write a program to implement K-means and hierarchical clustering.
Software used :- MATLAB
Theory:-
K-means clustering is a popular par oning method in machine learning that divides a
dataset into dis nct, non-overlapping subsets called clusters. Each data point in the dataset
is assigned to the cluster with the nearest mean, which serves as the prototype of the
cluster.
Hierarchical clustering is an agglomera ve (bo om-up) method that builds a hierarchy of clusters by
itera vely merging or spli ng clusters based on their similarity. This method is par cularly useful for
understanding the hierarchical structure of data

Program : -
rng(1); % For reproducibility
numSamples = 100;
numFeatures = 2; % For visualization purposes, we use 2D data
data = rand(numSamples, numFeatures);
numClusters = 3;
[idxKMeans, centroids] = kmeans(data, numClusters);
Z = linkage(data, 'ward'); % Use 'ward' linkage for hierarchical clustering
T = cluster(Z, 'maxclust', numClusters); % Cut the tree to form the specified
number of clusters
figure;
subplot(1, 2, 1);
gscatter(data(:,1), data(:,2), idxKMeans);
hold on;
plot(centroids(:,1), centroids(:,2), 'kx', 'MarkerSize', 15, 'LineWidth', 3);
title('K-Means Clustering');
xlabel('Feature 1');
ylabel('Feature 2');
legend('Cluster 1', 'Cluster 2', 'Cluster 3', 'Centroids');
grid on;
hold off;
subplot(1, 2, 2);
gscatter(data(:,1), data(:,2), T);
title('Hierarchical Clustering');
xlabel('Feature 1');
ylabel('Feature 2');
legend('Cluster 1', 'Cluster 2', 'Cluster 3');
grid on;
figure;
dendrogram(Z);
title('Hierarchical Clustering Dendrogram');
xlabel('Sample Index');
ylabel('Distance');
Output
Experiment 8
Aim : Write a program to implement autoencoders for data compression.
Software used :- MATLAB
Theory:-
Autoencoders are a type of artificial neural network used to learn efficient codings of
unlabeled data (unsupervised learning). They are primarily used for dimensionality reduction
(data compression) and feature learning. The goal of an autoencoder is to learn a
representation (encoding) for a set of data, typically for the purpose of dimensionality
reduction.

Program : -
rng(1); % For reproducibility

numSamples = 1000;

numFeatures = 20; % Number of features in the dataset

data = rand(numSamples, numFeatures);

hiddenSize = 10; % Size of the hidden layer (compression size)

autoenc = trainAutoencoder(data, hiddenSize, ...

'L2WeightRegularization', 0.004, ...

'SparsityRegularization', 4, ...

'SparsityProportion', 0.05, ...

'MaxEpochs', 100, ...

'ShowProgressWindow', false);

encoder = autoenc.EncoderWeights;

decoder = autoenc.DecoderWeights;

% Encode the data (compression)

encodedData = encode(autoenc, data);

% Decode the data (reconstruction)

decodedData = predict(autoenc, data);

reconstructionError = mse(data, decodedData);

% Display the results


disp('Original Data (first 5 samples):');

disp(data(1:5, :));

disp('Encoded Data (first 5 samples):');

disp(encodedData(1:5, :));

disp('Reconstructed Data (first 5 samples):');

disp(decodedData(1:5, :));

disp(['Mean Squared Reconstruction Error: ', num2str(reconstructionError)])

if numFeatures == 2

figure;

subplot(1, 2, 1);

scatter(data(:, 1), data(:, 2));

title('Original Data');

xlabel('Feature 1');

ylabel('Feature 2');

subplot(1, 2, 2);

scatter(decodedData(:, 1), decodedData(:, 2));

title('Reconstructed Data');

xlabel('Feature 1');

ylabel('Feature 2');

end

Output

0.4598 0.4599
0.3499 0.3500
0.4418 0.4417
0.5086 0.5086

Mean Squared Reconstruction Error: 0.062406


Experiment-9

Aim: Write a program to implement Markov decision processes.


Software used:- MATLAB

Theory:
Markov Decision Processes (MDPs) are like a map that helps agents make decisions while
exploring uncertain environments. Imagine the agent as a traveler and the environment as a
maze. At every point in the maze, the traveler (agent) needs to decide which direction to take.
These decisions are the actions. The traveler's goal is to find the best way through the maze to
maximize their happiness or rewards. The map (MDP) tells the traveler about the layout of
the maze, the chances of reaching different points from where they are, and the rewards they
might get for reaching each point. With this information, the traveler learns which actions to
take in each situation to get the most rewards over time. They might use different strategies,
like always turning left or following a particular path. By learning from their experiences,
travelers (agents) get better at navigating mazes (environments) and achieving their goals.

Code:
transition_matrix = [0.7, 0.2, 0.1; 0.3, 0.5, 0.2; 0.4, 0.1, 0.5];
initial_state = 0;
num_steps = 10;
markov_process = markeds(transition_matrix, initial_state);
sequence = zeros(1, num_steps+1);
sequence(1) = initial_state;
for i = 2:num_steps+1
sequence(i) = markov_process();
end
disp('Generated sequence:')
disp(sequence)

OUTPUT :

Generated sequence:
0 0 0 0 0 0 0 0 0 0
Experiment-10
Aim: Write a program to implement Q learning.
Software used: MATLAB
Theory:
Q-learning is a value-based reinforcement learning algorithm that learns the optimal action-
value function for a given Markov Decision Process (MDP). The algorithm uses a Q-table to
store the estimated Q-values for each state-action pair. At each time step, the agent observes
the current state, selects an action based on the Q-values, receives a reward, and updates the
Q-value for the state-action pair using the Q-learning update rule. The exploration-
exploitation trade-off is handled using an ε-greedy policy. Q-learning converges to the
optimal Q-value function in the limit of infinite time steps and sufficient exploration.
The Q-learning update rule is:
Q(s,a) = Q(s,a) + α * (r + γ * max_a' Q(s',a') - Q(s,a))
where Q(s,a) is the current Q-value for the state-action pair, α is the learning rate, r is the
received reward, γ is the discount factor, s' is the next state, and max_a' Q(s',a') is the
maximum Q-value for the next state.
Q-learning has been successfully applied to a wide range of problems, including robot
navigation, game playing, and resource management.

Code:
% Define environment parameters
numStates = 10;
numActions = 4;
numEpisodes = 1000;

% Initialize Q-table
Q = zeros(numStates, numActions);

% Set learning parameters


gamma = 0.9;
alpha = 0.8;
epsilon = 0.1;

% Run Q-learning algorithm


for episode = 1:numEpisodes
% Reset environment at the start of each episode
currentState = 1; % Initial state

while true % Infinite loop until terminal state is reached


% Choose action using epsilon-greedy policy
if rand < epsilon
action = randi(numActions); % Explore: random action
else
[~, action] = max(Q(currentState, :)); % Exploit: best action based on Q-table
end

% Take action and observe new state and reward


[nextState, reward] = transition(currentState, action, numStates);

% Update Q-value
bestNextAction = max(Q(nextState, :));
Q(currentState, action) = Q(currentState, action) + alpha * (reward + gamma *
bestNextAction - Q(currentState, action));

% Check if terminal state is reached


if nextState == numStates || currentState == nextState
break;
end

% Update current state


currentState = nextState;
end
end

% Display the final Q-table


disp('Final Q-table:');
disp(Q);

% Helper function for state transition


function [nextState, reward] = transition(currentState, action, numStates)
% Implement your state transition logic here
% For example, a simple transition based on action
switch action
case 1
nextState = max(currentState - 1, 1); % Move left
case 2
nextState = min(currentState + 1, numStates); % Move right
case 3
nextState = currentState; % Stay in the same place
case 4
nextState = currentState; % Stay in the same place
otherwise
nextState = currentState; % Invalid action, stay in the same place
end

% Reward function
if nextState == numStates
reward = 1; % Reward for reaching the terminal state
else
reward = -0.1; % Small penalty for each step
end
end
Output:

You might also like