Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.760379e-19.

2 views (last 30 days)
% Logstic Regress with Newton's Method
clear all;
close all;
clc;
% data
x = [0.230000 0.394000;
0.11000 0.690000;
0.13000 0.440000;
0.24000 0.510000;
0.33000 0.440000;
0.36000 0.550000;
0.35000 0.660000;
0.41000 0.470000;
0.52000 0.695000;
0.53000 0.590000;
0.39500 0.250000;
0.41000 0.075000;
0.49000 0.220000;
0.52000 0.130000;
0.57000 0.070000;
0.35000 0.140000;
0.62000 0.170000;
0.63000 0.280000;
0.62500 0.350000;
0.780000 0.282000];
y = [0;
0;
0;
0;
0;
0;
0;
0;
0;
0;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1];
[m, n] = size(x);
x = [ones(m, 1), x];
% plot the datas
figure
pos = find(y); neg = find(y == 0);%find是找到的一个向量,其结果是find函数括号值为真时的值的编号
plot(x(pos, 2), x(pos,3), '+')
hold on
plot(x(neg, 2), x(neg, 3), 'o')
hold on
xlabel('axis X')
ylabel('axis Y')
% Initialize fitting parameters
x0 = 0;
i = 1;
theta_new = zeros(n+1, 1);
theta_old = ones(n+1, 1);
err = abs((theta_new - theta_old)'*(theta_new - theta_old));
e = 0.0001;
N = 100;
T = [];
% Define the sigmoid function
g = inline('1.0 ./ (1.0 + exp(-z))');
% Newton's method
while (i<=N && err>e)
% Calculate the hypothesis function
z = x * theta_new;
h = g(z);
% Calculate gradient and hessian.
grad = x' * (h-y);
H = x' * diag(h) * diag(1-h) * x;
theta_new = theta_new - H\grad;
T = [T;i theta_new'];
i = i +1;
theta_old = theta_new;
end
Result = array2table(T); %converts m-by-n array T
Result.Properties.VariableNames = {'Iter', 'theta_new', 'Itera', 'thet_new'}; %Variable names, specified as a cell array of character vectors or a string array whose elements are nonempty and distinct. The number of names must equal the number of variables.
plot_x = [x(:,2)];
plot_y = (-1./theta_new(3)).*(theta_new(2).*plot_x +theta_new(1));
plot(plot_x, plot_y)
legend('Label 1', 'Label 2', 'Decision Boundary')
hold off
This is where i get the warning: theta_new = theta_new - H\grad. How should i solve it? I hope someone can help me.
  1 Comment
Stephen23
Stephen23 on 4 Jan 2022
Edited: Stephen23 on 4 Jan 2022
You should take a look at the INLINE documentation:
and consider using designed for the purpose anonymous functions, just as the documentation recommends.

Sign in to comment.

Answers (1)

DGM
DGM on 4 Jan 2022
Your table has 4 columns, but you only have two labels in the vector called "Variables". Depending on your intent, you can add more labels to "Variables", or you can restrict the range of labels being assigned on the LHS so that it matches the length of the RHS. For instance, if you wanted to rename the first two columns:
Result.Properties.VariableNames(1:numel(Variables)) = Variables;
or some arbitrary selection of two columns:
Result.Properties.VariableNames([1 3]) = Variables;
  4 Comments
DGM
DGM on 7 Jan 2022
I'm not sure what it's supposed to be printing. You can pick the variable of interest and display it using disp() somewhere inside the loop.
For example, you could print the most recent row of T by placing this at the end of the loop.
disp(T(end,:))

Sign in to comment.

Categories

Find more on Stress and Strain in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!