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

20 Ansichten (letzte 30 Tage)
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));

Antworten (3)

Image Analyst
Image Analyst am 6 Mai 2021
Hint: randsample() or randperm() and polyfit.
Demo attached.
  4 Kommentare
arash Moha
arash Moha am 9 Mai 2021
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 ?
Image Analyst
Image Analyst am 9 Mai 2021
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.

Melden Sie sich an, um zu kommentieren.


arash Moha
arash Moha am 9 Mai 2021
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 Kommentar
Image Analyst
Image Analyst am 9 Mai 2021
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.-');

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 9 Mai 2021
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 Kommentare
Image Analyst
Image Analyst am 13 Mai 2021
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.
arash Moha
arash Moha am 13 Mai 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;

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by