Using a 2 dimensional feature

1 view (last 30 days)
G. Nardi
G. Nardi on 18 Jan 2019
Commented: G. Nardi on 21 Jan 2019
I am current student and new to Matlab. For an assignment I was told that I was not using the 2 dimensional feature for a .m file that I loaded. How do I do this and how do I figure it out? Where are my mistakes?
partTwoData is a 10000x2 double.
My variables made are coming up as x1
2019-01-18_21-35-40.png
load ('partTwoData.mat');
numberOfClassOneSamples = size(classOne, 1);
numberOfClassTwoSamples = size(classTwo, 1);
%Generate a random partition of the data,
randomizedIndices = randsample(numberOfClassOneSamples, numberOfClassOneSamples);
%splitting each of the classes into 60% training and 40%
classOneTrainingIndices = randomizedIndices(1:6000);
classOneTestIndices= randomizedIndices(6001:10000);
%for classTwo
classTwoTrainingIndices = randomizedIndices(1:6000);
classTwoTestIndices= randomizedIndices(6001:10000);
classOneTrainingData = classOne(classOneTrainingIndices);
classOneTestData = classOne(classOneTestIndices);
classTwoTrainingData = classTwo(classTwoTrainingIndices);
classTwoTestData = classTwo(classTwoTestIndices);
numberOfTestSamples = 4000;
numberOfTrainingSamples = 6000;
%class one test samples
for i=1:numberOfTestSamples
for j=1:numberOfTrainingSamples
euclideanDistanceClassOne(j) = sqrt ( (classOneTestData(i) - classOneTrainingData(j))^2);
end
for j=1 : numberOfTrainingSamples
euclideanDistanceClassTwo(j) = sqrt( (classOneTestData(i) - classTwoTrainingData(j))^2);
end
if(min(euclideanDistanceClassOne) < min(euclideanDistanceClassTwo))
%classify as class one
predict(i) = 1;
else
predict(i) = 2;
end
end
correctClassOne = sum(predict==1);
incorrectClassOne = sum(predict==2);
%Class Two Test Samples, still j, because we are stepping through the training sample.
for i=1:numberOfTestSamples
for j=1:numberOfTrainingSamples
euclideanDistanceClassOne(j) = sqrt((classTwoTestData(i) - classOneTrainingData(j))^2);
end
for j=1:numberOfTrainingSamples
euclideanDistanceClassTwo(j) = sqrt((classTwoTestData(i) - classTwoTrainingData(j))^2);
end
if (min(euclideanDistanceClassOne) < min(euclideanDistanceClassTwo))
%classify as class one
predict(i)=1;
else
%classify as class two
predict(i)=2;
end
end
correctClassTwo = sum(predict == 2);
incorrectClassTwo = sum(predict == 1);
fprintf('Part Two:');
fprintf('Class One Correct Predictions = %d\n', correctClassOne);
fprintf('Class Two Correct Predictions = %d\n', correctClassTwo);
fprintf('Class One Incorrect Predictions = %d\n', incorrectClassOne);
fprintf('Class Two Incorrect Predictions = %d\n', incorrectClassTwo);
fprintf('Total Class One Incorrect Predictions = %d\n', correctClassOne + correctClassTwo);
fprintf('Total Class Two Incorrect Predictions = %d\n', incorrectClassOne + incorrectClassTwo);
fprintf('Total Prediction Accuracy = %f\n', (correctClassOne + correctClassTwo) / (2 * numberOfTestSamples));

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jan 2019
Every place that you index classTwoTestData(some_index) you need to change it to classTwoTestData(some_index,:) and likewise for similar variables.
You also need to adjust your euclidean distance calculations. A(index,:) - B(index,:) will be a vector, so you cannot use ^2 for it: you need to use .^2 . You also need to take the sum of the squared components.
  5 Comments
Walter Roberson
Walter Roberson on 20 Jan 2019
If you have a vector of values, such as
(classOneTestData(i,:) - classOneTrainingData(j,:)).^2
is a vector of values, then how would you find the total value?
G. Nardi
G. Nardi on 21 Jan 2019
sum! got it thanks.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!