MATLAB (Please Help, Full Practice in the below) : Split and plot the training data set and test data set.

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

Réponses (3)

Hint: randsample() or randperm() and polyfit.
Demo attached.

4 commentaires

Thanks a lot but I am new to using MATLAB , i run this demo code That you attached but the result is not same as a picture Which I put in case of question. what am i doing for this result ?
That's because you didn't give the data for generating Test Error and Train Error. You just gave code to make "x" which gives a single plot that looks like this:
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
plot(x, 'b.-', 'LineWidth', 3, 'MarkerSize', 30);
title('x as a function of index', 'FontSize', 18);
xlabel('Index', 'FontSize', 18);
ylabel('x value', 'FontSize', 18);
grid on;
How do we get the two curves you showed????
The data are divided into two Set, Train and Test, and each is plot based on it.
x value in your example That you sent is MSE & Index is regression degree from 1 to 100 .
what function use for divided test and train set ?
I didn't really send anything. It's not my example, it's yours:
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
I don't understand what you mean when you said "example That you sent is MSE & Index is regression degree from 1 to 100". The x value, which you call temp, goes from 0 to 2*pi. And the y value, which you (for some reason) call x, goes from -1.5 to +1.5. So exactly what is going from 1 to 100 and where is it used in your example???
And what is a "regression" degree? Is it the order of a polynomial you want to fit to something? Is it the independent variable along the x axis? Exactly where does the 1 to 100 get plugged in to a function? You never made any variable with a range of 0-100, and did not have a loop over 1-100.
Again...
"How do we get the two curves you showed????"
Please give the code for that because I don't know how to make them and your explanations are not clear.

Connectez-vous pour commenter.

I mean, the MSE range should be placed on the Y axis, and instead of the X axis, the regression value for the polynomial degree should be from 1 to 100 degrees.
First, divide the data into two parts: training data (Train - green line in plot) and test data(red line in plot). 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.(Use efficient functions in MATLAB for this purpose)
Third, Perform the regression for the polynomial degree from degree 1 to degree 100 and display the plot results of these 100 experiments.
Is it the independent variable along the x axis? no,This is actually the X axis and Y axis is MSE.
I have no other code, I need help to solve this practice.

1 commentaire

To compute the MSE, you need two curves. What are your two curves?
  1. The clean sin wave
  2. The sine wave with noise added?
  3. The fitted curve with polyfit?
Which 2 do you want to use?
If you plot var1 along the x axis and var2 along the y axis like this:
plot(var1, var2, 'b.-);
then to swap them so that var2 is along the x axis instead of the y axis, plot it like this:
plot(var2, var1, 'b.-');

Connectez-vous pour commenter.

Do you mean something like this:
% Demo by Image Analyst.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
markerSize = 20;
rng(914163506);
t = 0:.15:2*pi;
trueSignal = sin(t);
y = trueSignal +.2*randn(size(t));
plot(t, y, 'b.-', 'LineWidth', 3, 'MarkerSize', 30);
hold on;
for polynomialOrder = 1 : 100
fprintf('polynomialOrder = %d.', polynomialOrder);
coefficients = polyfit(t, y, polynomialOrder);
fittedy = polyval(coefficients, t);
% Bail out if the plot gets too rediculous
if max(fittedy) > 2
break;
end
plot(t, fittedy, '-', 'LineWidth', 1);
drawnow;
mse = immse(y, fittedy);
fprintf('MSE = %f.\n', mse);
end
title('y as a function of index', 'FontSize', 18);
xlabel('Index', 'FontSize', 18);
ylabel('x value', 'FontSize', 18);
grid on;

11 commentaires

i need two curves for the training data set and test data set,
  1. The fitted curve with polyfit
just like this picture
OK. I'm done making assumptions. Give me the data and I'll make a plot from it.
In the Practice, we have to generate the data code using these three lines code, so this is our data. I do not know exactly what other data you mean.
this code :
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
i use this code for split training and test data set but when i run the program , show me this error :
"" Error using cvpartition (line 160)
The number of observations must be a positive integer greater than one.
Error in Code (line 8)
c = cvpartition(size(x,1),'HoldOut',0.3); ""
what's the problem ??
this is my code :
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
c = cvpartition(size(x,1),'HoldOut',0.3);
idx= c.test;
Train= x(~idx,:);
Test= x(idx,:);
x is a row vector, not a column vector so you need size(x, 2) to count the rows.
c does not have a field called test.
Is this the right way to split with this warning ->> This segmentation should be completely random without duplicate data ( without overlap ). 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.
If the code is incorrect, please send the correct form of code.
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
Data=randn(size(x));
[i,j]=size(Data);
idx=randperm(j);
Train=Data(:,1:round(j*0.70));
Test=Data(:,1:round(j*0.70)+1:end);
According to the above code, How to plot TRAIN and TEST data set so that the x-axis is regression from degree 1 to 100 and the y-axis is proportional to the MSE value?
Please Help me and tell me the code for this section, thank you very much.
That is not correct. You can't take some 1-D signal and then take the first 70% of it and say that is training and the last 30% is Test. You'd need complete signals both for training and testing.
This is all the code I wrote, but I do not know exactly where the problem is? - I really thank you for telling me what the correct code is here and how MSE and Polyfit are calculated here, I'm so confused please help me for that. How fit the data with the training set and to compute the MSE on both the training and test sets using the same coefficients returned by the training-fit ?
clc;
close all;
clear;
workspace;
rng(914163506);
temp=0:.15:2*pi;
x = sin(temp)+.2*randn(size(temp));
idx=randperm(numel(x));
n = numel(x);
idxTrain = idx(1:floor(n*.7));
xTrain = temp(idxTrain);
yTrain = x(idxTrain);
idxTest = idx(1:floor(n*.3));
xTest = temp(idxTest);
yTest = x(idxTest);
Degrees = 1:100;
MSE = nan(numel(Degrees),2);
for i=1:numel(Degrees)
p=polyfit(xTrain,yTrain,i);
pval=polyval(p,xTrain);
MSE(i,1) = immse(pval,xTrain);
%MSE(i,2) = mean((idxTest - pval).^2);
end
plot(Degrees,MSE(i,1), 'b.-', 'LineWidth', 3);
hold on
%plot(Degrees,MSE(i,2), 'r.-', 'LineWidth', 3);
%title('x as a function of index', 'FontSize', 18);
xlabel('Regression', 'FontSize', 15);
ylabel('MSE', 'FontSize', 15);
grid on;
Why do you even have a test set? You can't really fit a polynomial to a sine wave and expect anything good. Next, so you train it with the training data, but then what would you use the test data for? To see how well it fits the polynomial trained with the training set? Why? And like I showed, it makes little to no sense to do a hundred degree polynomial. That's not the same as doing a Fourier series where you have a fit with sine waves of up to a hundred harmonic frequencies, if that's what you were thinking.
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;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Centre d'aide et File Exchange

Produits

Version

R2015b

Commenté :

le 13 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by