标签:svm logistic 核函数 特征空间映射 01分类
数据离散点如图:
最后结果:
theta =
5.1555
3.2317
-11.9929
4.1505
-11.7849
-7.4954
>> cost
cost =
0.3481
图形如下:
%% part0: 准备
data = load(‘ex2data2.txt‘);
x = data(:,[1,2]);
y = data(:,3);
pos = find(y==1);
neg = find(y==0);
x1 = x(:,1);
x2 = x(:,2);
plot(x(pos,1),x(pos,2),‘r*‘,x(neg,1),x(neg,2),‘co‘);
m = size(y,1);
X = zeros(m,6);
X(:,1) = ones(m,1);
X(:,2) = x(:,1);
X(:,3) = x(:,1).*x(:,1);
X(:,4) = x(:,2);
X(:,5) = x(:,2).*x(:,2);
X(:,6) = x(:,1).*x(:,2);
pause;
%% part1: GradientDecent and compute cost of J
[m,n] = size(X);
theta = zeros(6,1);
options = optimset(‘GradObj‘, ‘on‘, ‘MaxIter‘, 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(computeCost(X,y,t)), theta, options);
display(theta);
ezplot(‘5.15 + 3.2317*XX - 11.99*XX*XX + 4.15*YY - 11.78*YY*YY - 7.50*XX*YY‘,[-1.0,1.5,-0.8,1.2]);
hold on
plot(x(pos,1),x(pos,2),‘r*‘,x(neg,1),x(neg,2),‘co‘);
pause;
function [J,grad] = computeCost(x, y, theta)
%% compute cost: J
m = size(x,1);
grad = zeros(size(theta));
hx = sigmoid(x * theta);
J = (1.0/m) * sum(-y .* log(hx) - (1.0 - y) .* log(1.0 - hx));
grad = (1.0/m) .* x‘ * (hx - y);
end
function g = sigmoid(z)
%% SIGMOID Compute sigmoid functoon
g = zeros(size(z));
g = 1.0 ./ (1.0 + exp(-z));
end
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:svm logistic 核函数 特征空间映射 01分类
原文地址:http://blog.csdn.net/neu_chenguangq/article/details/47313933