Matlab: Split and Plot the training data set and test data set.

22 views (last 30 days)
Hi Everyone
Thanks for any Help
i need some solution for this practice.
First, divide the data into two parts: training data (Train) and test data. Consider 30% of the data for the test set and 70% as the training set.
Second, This segmentation should be completely random without duplicate data. In other words, none of the randomly selected test set data should be present in the training set and also the data in the test set should not be duplicate. (Use efficient functions in MATLAB for this purpose)
--> Perform the regression for the polynomial degree from degree 1 to degree 100 and display the results of these 100 experiments in the plot below.
--> This example plot shows the MSE error for each degree of polynomial for both the training set and test set.
i have generated the data in one dimension using the following code :
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
  4 Comments
Adam Danz
Adam Danz on 7 May 2021
Do you have any data to work with? Surely your assignment wasn't to just make up data that looks like those curves.
arash Moha
arash Moha on 7 May 2021
this picture just an example shape of the output I want.
i have generated the data in one dimension using the following code :
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
To generate data, only this code should be used for the given practice.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 7 May 2021
I'll point you in the right direction toward the tools you need to complete your assignment.
>First, divide the data into two parts: training data (Train) and test data. Consider 30% of the data for the test set and 70% as the training set. Second, This segmentation should be completely random without duplicate data. In other words, none of the randomly selected test set data should be present in the training set and also the data in the test set should not be duplicate. (Use efficient functions in MATLAB for this purpose)
I suggest using cvpartition to break up the data into training and testing set. It's not the most straightforward function but hopefully the documentation page and examples on that page will help to learn it.
Alternatively you use use randperm to create a vector of indicies that can be used to break up the data.
> Perform the regression for the polynomial degree from degree 1 to degree 100 and display the results of these 100 experiments in the plot below.
Sounds like your instructor wants you to use polyfit within a loop.
  22 Comments
arash Moha
arash Moha on 13 May 2021
Is this code correct? - Please, if there is a problem somewhere in the code, please tell me the correct code, thank you very much.
clc;
close all;
clear;
workspace;
rng(914163506);
temp=0:.15:2*pi;
Data = sin(temp)+.2*randn(size(temp));
[i,j]=size(Data);
p=0.30;
idx=randperm(j);
Data_Trainset=Data(:,idx(1:round(p*j)));
Data_Testset=Data(:,idx(round(p*j)+1:end));
Data_Trainset_y=randn(size(Data_Trainset));
Data_Testset_y=randn(size(Data_Testset));
l1=[];
l2=[];
for i1=1:100
p=polyfit(Data_Trainset,Data_Trainset_y,i1);
pval= polyval(p,Data_Trainset_y);
pvall=polyval(p,Data_Testset_y);
MSE_Trainset=immse(pval,Data_Trainset);
MSE_Testset=immse(pvall,Data_Testset);
g=inv(MSE_Trainset);
g1=inv(MSE_Testset);
l1=[l1,g];
l2=[l2,g1];
plot(i1,g,'b.-', 'LineWidth', 3);
legend('Train error','Test error');
hold on
plot(i1,g1,'r.-', 'LineWidth', 3);
legend('Train error','Test error');
hold on
end
xlabel('Regression', 'FontSize', 15);
ylabel('MSE', 'FontSize', 15);
grid on;
Adam Danz
Adam Danz on 13 May 2021
No, the plotting should stay out of the loop.
To plot a column z of matrix m, plot(m(:,z))

Sign in to comment.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!