フィルターのクリア

How to plot best fit line?

1,025 ビュー (過去 30 日間)
fzhmktr
fzhmktr 2018 年 1 月 15 日
コメント済み: Miles 2023 年 11 月 11 日
I have 1700 plot of data in graph. How do I plot the line of best fit? I stored the x and y data in table and the plot them. From the graph, I can see that the graph plotting is upwards. I have read other answers for this kind of question but still confused. Please help. Thank you.
x = score;
y = MOS;
scatter(x,y)
xlabel('score')
ylabel('MOS')

採用された回答

Image Analyst
Image Analyst 2018 年 1 月 15 日
編集済み: Image Analyst 2021 年 2 月 10 日
Use polyfit() and polyval():
% Get coefficients of a line fit through the data.
coefficients = polyfit(x, y, 1);
% Create a new x axis with exactly 1000 points (or whatever you want).
xFit = linspace(min(x), max(x), 1000);
% Get the estimated yFit value for each of those 1000 new x locations.
yFit = polyval(coefficients , xFit);
% Plot everything.
plot(x, y, 'b.', 'MarkerSize', 15); % Plot training data.
hold on; % Set hold on so the next plot does not blow away the one we just drew.
plot(xFit, yFit, 'r-', 'LineWidth', 2); % Plot fitted line.
grid on;
See attached for a full demo.
  3 件のコメント
Sahil Bharti
Sahil Bharti 2021 年 7 月 22 日
Thanks. it was usefull to me as well
Miles
Miles 2023 年 11 月 11 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2018 年 1 月 15 日
if it is just for plotting, the command lsline would do.
x = 1:10 ; y = 2* x + 3 ;
plot(x, y, 'bo') ;
lsline
  1 件のコメント
optoengineer2
optoengineer2 2021 年 2 月 9 日
lsline works only with "Statistics and Machine Learning Toolbox"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSupport Vector Machine Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by