Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Task 1

% Load train1 data


train1_data = load('train1.data'); % Assuming the data is space-separated

% Separate data points and class labels


X_train1 = train1_data(:, 1:2); % Features (first two columns)
y_train1 = train1_data(:, 3); % Class labels (third column)

% Plot the data points


figure;
scatter(X_train1(y_train1 == 0, 1), X_train1(y_train1 == 0, 2), 'r', 'filled',
'DisplayName', 'Class 0');
hold on;
scatter(X_train1(y_train1 == 1, 1), X_train1(y_train1 == 1, 2), 'b', 'filled',
'DisplayName', 'Class 1');
legend('show');
xlabel('Feature 1');
ylabel('Feature 2');
title('Train1 Data');

Task 2
function plotData(train_file, test_file)
% Import training data
train_data = load("train2.data");
X_train = train_data(:, 1:2); % Features (first two columns)
y_train = train_data(:, 3); % Class labels (third column)

% Plot training data


figure;
scatter(X_train(y_train == 0, 1), X_train(y_train == 0, 2), 'r', 'filled',
'DisplayName', 'Class 0');
hold on;
scatter(X_train(y_train == 1, 1), X_train(y_train == 1, 2), 'b', 'filled',
'DisplayName', 'Class 1');

% Import test data


if nargin > 1
test_data = load("test.data");
X_test = test_data(:, 1:2); % Features (first two columns)

% Plot test data over training data


scatter(X_test(:, 1), X_test(:, 2), 'g', 'filled', 'DisplayName', 'Test
Data');
end

% Plot settings
legend('show');
xlabel('Feature 1');
ylabel('Feature 2');
title('Data Plot');
end

% Plot training data from train2.txt and test data from test.txt

plotData('train2.txt', 'test.txt');
Task3

function [error_rate, confusion_mat] = classifyAndPlotSVM(train_file, test_file)


% Load training data
train_data = load("train1.data");
X_train = train_data(:, 1:2); % Features (first two columns)
y_train = train_data(:, 3); % Class labels (third column)

% Train SVM classifier


SVMModel = fitcsvm(X_train, y_train, 'KernelFunction', 'linear');

% Load test data


test_data = load("test.data");
X_test = test_data(:, 1:2); % Features (first two columns)
y_test = test_data(:, 3); % True class labels (third column)

% Predict labels for test data


y_pred = predict(SVMModel, X_test);

% Compute classification error


error_rate = sum(y_pred ~= y_test) / length(y_test);

% Compute confusion matrix


confusion_mat = confusionmat(y_test, y_pred);

% Plot SVM decision boundary


figure;
gscatter(X_train(:,1), X_train(:,2), y_train, 'rb', 'o+', 8);
hold on;

% Extract support vectors


SV = SVMModel.SupportVectors;
% Plot support vectors
plot(SV(:,1),SV(:,2),'ko','MarkerSize',10);

% Set the range for the plot


xlim([min(X_train(:,1))-1 max(X_train(:,1))+1]);
ylim([min(X_train(:,2))-1 max(X_train(:,2))+1]);

% Create grid to evaluate model


d = 0.02; % Step size of the grid
[x1Grid,x2Grid] = meshgrid(min(X_train(:,1)):d:max(X_train(:,1)),...
min(X_train(:,2)):d:max(X_train(:,2)));
xGrid = [x1Grid(:),x2Grid(:)]; % Concatenate to get a 2-D grid of points

% Predict scores over the grid


[~,scores] = predict(SVMModel,xGrid);

% Plot decision boundary and margins


contour(x1Grid,x2Grid,reshape(scores(:,2),size(x1Grid)),[0
0],'k','LineWidth',2);

% Plot settings
xlabel('Feature 1');
ylabel('Feature 2');
title('SVM Decision Boundary');
legend('Class 0', 'Class 1', 'Support Vectors', 'Decision Boundary',
'Location', 'best');
hold off;
end

Classification Error: 0.07


Confusion Matrix:
42 3
4 51
Task4
function reduced_data = combineAndReduce(train_file, test_file)

% Load training data


train_data = load("train1.data");
X_train = train_data(:, 1:2); % Features (first two columns)
y_train = train_data(:, 3); % Class labels (third column)

% Load test data


test_data = load("test.data");
X_test = test_data(:, 1:2); % Features (first two columns)
y_test = test_data(:, 3); % Class labels (third column)

% Combine training and test data


combined_X = [X_train; X_test];
combined_y = [y_train; y_test];

% Apply PCA to reduce dimensionality from 2 to 1


coeff = pca(combined_X);
reduced_X = combined_X * coeff(:,1); % Project data onto the first principal
component

% Combine reduced data with class labels


reduced_data = [reduced_X, combined_y];
end

reduced_data = combineAndReduce('train1.txt', 'test.txt');

disp(reduced_data)

You might also like