Using a 2 dimensional feature

1 回表示 (過去 30 日間)
G. Nardi
G. Nardi 2019 年 1 月 18 日
コメント済み: G. Nardi 2019 年 1 月 21 日
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));

採用された回答

Walter Roberson
Walter Roberson 2019 年 1 月 19 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 20 日
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 2019 年 1 月 21 日
sum! got it thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeClassification Learner App についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by