MATLAB (Please Help, Full Practice in the below) : Split and plot the training data set and test data set.
13 次查看(过去 30 天)
显示 更早的评论
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));
0 个评论
回答(3 个)
Image Analyst
2021-5-6
Hint: randsample() or randperm() and polyfit.
Demo attached.
4 个评论
Image Analyst
2021-5-9
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.
arash Moha
2021-5-9
1 个评论
Image Analyst
2021-5-9
To compute the MSE, you need two curves. What are your two curves?
- The clean sin wave
- The sine wave with noise added?
- 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.-');
Image Analyst
2021-5-9
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 个评论
Image Analyst
2021-5-13
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!