MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

G. Nardi
0

Using a 2 dimensional feature

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));

  0 件のコメント

サインイン to comment.

製品


リリース

R2018b

1 件の回答

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 件のコメント

G. Nardi
2019 年 1 月 20 日
I am still a student and trying to learn. Nothing is really obvious.
I tried
%class one test samples
for i=1:numberOfTestSamples
for j=1:numberOfTrainingSamples
euclideanDistanceClassOne(j) = sqrt ( (classOneTestData(i,:) - classOneTrainingData(j,:)).^2);
end
But I get this error
Unable to perform assignment because the left and right sides have a different number of
elements.
Error in
euclideanDistanceClassOne(j) = sqrt ( (classOneTestData(i,:) -
classOneTrainingData(j,:)).^2);
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
2019 年 1 月 21 日
sum! got it thanks.

サインイン to comment.



Translated by