周志华机器学习,3.3编程实现对率回归,并给出西瓜数据集3.0α上的结果
3.3编程实现对率回归,并给出西瓜数据集3.0α上的结果数据集:1 0.697 0.460 12 0.774 0.376 13 0.634 0.264 14 0.608 0.318 15 0.556 0.215 16 0.403 0.237 17 0.481 0.149 18 0.437 0.211 19 0.666 0.091 010 0.243 0.267
·
3.3编程实现对率回归,并给出西瓜数据集3.0α上的结果
数据集:
- 1 0.697 0.460 1
2 0.774 0.376 1
3 0.634 0.264 1
4 0.608 0.318 1
5 0.556 0.215 1
6 0.403 0.237 1
7 0.481 0.149 1
8 0.437 0.211 1
9 0.666 0.091 0
10 0.243 0.267 0
11 0.245 0.057 0
12 0.343 0.099 0
13 0.639 0.161 0
14 0.657 0.198 0
15 0.360 0.370 0
16 0.593 0.042 0
17 0.719 0.103 0
MATLAB 代码
clear
clc
data = load('gua.txt');
X = data(:, [2, 3]); y = data(:, 4);
figure;
hold on;
pos=find(y==1);
neg=find(y==0);
data(pos,4)
plot(X(pos,1),X(pos,2),'g.','LineWidth',2,'MarkerSize',7);
plot(X(neg,1),X(neg,2),'k.','MarkerFaceColor','y','MarkerSize',7);
% Labels and Legend
xlabel('密度')
ylabel('含糖率')
hold off;
[m, n] = size(X) ;
% Add intercept term to x and X_test
X = [X ones(m, 1) ];
% Initialize fitting parameters
initial_theta = zeros(n + 1, 1) ;
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
hold on;
plot_x = X(:,1) ;
y = X*theta ;
plot_y=1./(1.+exp(-1.*y))
plot(plot_x, plot_y,'r+','LineWidth',2,'MarkerSize',7)
hold off;
function [J, grad] = costFunction(theta, X, y)
m = length(y); % number of training examples
J = 0;
grad = zeros(size(theta));
h=1.0./(1.0+exp(-1*X*theta));
m=size(y,1);
J=((-1*y)'*log(h)-(1-y)'*log(1-h))/m;
for i=1:size(theta,1),
grad(i)=((h-y)'*X(:,i))/m;
end
end
结果:
黑点负例
绿点正例
红点 X轴密度,y轴y值
结果:
- 0.971079254646882 1
0.937598463307093 1
0.705751586241412 1
0.812393063785119 1
0.504856248448885 1
0.453265086556073 0
0.261482588297326 0
0.400249657018173 0
0.235208808199154 0
0.421463325239397 0
0.0508913086555173 0
0.109609784911483 0
0.403185071514151 0
0.531250393637467 1
0.791459430045055 1
0.117240356541242 0
0.296693337407861 0
以0.5做阈值:数据太少,不太好
更多推荐
所有评论(0)