matlab(2) Logistic Regression: 画出样本数据点plotData

本文详细介绍了使用Matlab进行数据绘图的方法,包括初始化环境、加载数据、绘制不同类型的点等步骤,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

画出data数据

data数据

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0
61.10666453684766,96.51142588489624,1
75.02474556738889,46.55401354116538,1
76.09878670226257,87.42056971926803,1
84.43281996120035,43.53339331072109,1

 

ex2.m文件

%% Initialization(灰色代表注释)
clear ; close all; clc       (clear: Clear variables and functions from memory; close: close figure;  clc: Clear command window.)

%% Load Data
% The first two columns contains the exam scores and the third column
% contains the label.

data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3); (取data的第一列至第二列给X,取data的第三列给y)

%% ==================== Part 1: Plotting ====================
% We start the exercise by first plotting the data to understand the
% the problem we are working with.

fprintf(['Plotting data with + indicating (y = 1) examples and o ' ... (...表示与下一行相连)
'indicating (y = 0) examples.\n']);

plotData(X, y); (调用函数plotData(X,y),参见下面的plotData.m)

% Put some labels
hold on; (保持住现有的plot和所有的坐标属性,包括颜色和线条的style)
% Labels and Legend
xlabel('Exam 1 score') (给x轴加上label)
ylabel('Exam 2 score') (给y轴加上label)

% Specified in plot order
legend('Admitted', 'Not admitted') (给两种不同的点的标记加说明)
hold off; (hold 关闭)

fprintf('\nProgram paused. Press enter to continue.\n');
pause;  (暂停运行,等待用户响应pause causes a procedure to stop and wait for the user to strike any key before continue)

 

plotData.m文件

 

function plotData(X, y)  (在文件的开头应写上新定义的function,文件的名称(plotData.m)中的plotData应与function的名称一至)

%PLOTDATA Plots the data points X and y into a new figure
% PLOTDATA(x,y) plots the data points with + for the positive examples
% and o for the negative examples. X is assumed to be a Mx2 matrix.

 

% Create New Figure
figure; hold on; (figure:创建一个figure 窗口)

 

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
% 2D plot, using the option 'k+' for the positive
% examples and 'ko' for the negative examples.
%
% Find indices of positive and negative example
pos = find(y==1); neg = find(y==0); (返回所有y==1的点的线性序列(linear indices (如上述data则返回(4,5,7,8,9,10)))

 

%plot example
plot(X(pos,1), X(pos,2), 'k+', 'LineWidth', 2, 'MarkerSize', 7); (将相应序列对应的X矩阵的元素画出(如第4行的第一列的值做为x轴的值,第4行的第二列的值做为y轴的值);  k+表示线的颜色为黑色(black),形状为+; MarkerSize 表示+形状的大小 )
plot(X(neg,1), X(neg,2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7); (MarkerFaceColor: 表示填充在o里面的颜色为黄色)

 

% ========================================================================= 

 

hold off;

 

end  (表示plotData(X, y)函数的结束)

 

转载于:https://www.cnblogs.com/yan2015/p/4820409.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值