I have code for naive Bayes classifier that implement the concept of the naive Bayes, but the accuracy that this algorithm gives me is about 48% and it much lower than MATLAB build-in function for Naive Bayes (84%). Can anybody help me where is the problem?
here is my code:
function [conf, confMat] = NaiveBayesClassifier(train, test)
Att_cnt = size(train, 2) - 1;
% training set
x = train(:, 1:Att_cnt);
y = train(:, Att_cnt+1);
% test set
u = test(:, 1:Att_cnt);
v = test(:, Att_cnt+1);
yu = unique(y);
nc = length(yu); % number of classes
ni = size(x,2); % independent variables
ns = length(v); % test set
% compute class probability
for i = 1 : nc
fy(i) = sum(double(y==yu(i)))/length(y);
end
% normal distribution
% parameters from training set
[mu, sigma] = MLE(train);
% probability for test set
for j = 1 : ns
fu = normcdf(ones(nc,1)*u(j,:), mu, sigma);
P(j,:)= fy.*prod(fu,2)';
end
% get predicted output for test set
[pv0, id] = max(P,[],2);
for i = 1 : length(id)
pv(i,1) = yu(id(i));
end
% compare predicted output with actual output from test data
confMat = confusionmat(v,pv);
conf = sum(pv==v)/length(pv);
end
I just solve it. instead of this line
fu = normcdf(ones(nc,1)*u(j,:), mu, sigma);
i have to write
fu = normpdf(ones(nc,1)*u(j,:), mu, sigma);
so the accuracy is the same as matlab build in function.
Related
I have written a simple code to calculate the phase and magnitude of a signal, based on the sinusoidal input given in my problem. I have already determined the magnitude of the signal corresponding to different values of w. More specifically, the phase I want is a vector calculated for different values of w. Notice that the signal I'm talking about is the output signal of a linear process. As matter of fact, I want the phase difference between the input u and the output y, defined for all values of w for all time steps. I have created the time and w vector in my code. Here is the main code I have written in MATAB2021a:
clc;clear;close all;
%{
Problem 2 Simulation, By M.Sajjadi
%}
%% Predifined Parameters
tMin = 0;
tMax = 50;
Ts = 0.01; % Sample Time
n = tMax/Ts; % #Number of iterations
t = linspace(tMin,tMax,n);
% Hint: Frequency Domain
wMin = 10^-pi;
wMax = 10^pi;
Tw = 10;
w = wMin:Tw:wMax; % Vector of Frequency
Nw = length(w);
a1 = 1.8;
a2 = -0.95;
a3 = 0.13;
b1 = 1.3;
b2 = -0.5;
%% Input Generation
M = numel(w);
N = length(t);
U = zeros(M,N);
Y = U; % Response to the sinusoidal Input, Which means the initial conditions are set to ZERO.
U(1,:) = sin(w(1)*t);
U(2,:) = sin(w(2)*t);
U(3,:) = sin(w(3)*t);
Order = 3; % The Order of the Differential Equation, Delay.
%% Main Loop for Amplitude and Phase
Amplitude = zeros(Nw,1); % Amplitude Values
for i=1:numel(w)
U(i,:) = sin(w(i)*t);
for j=Order+1:numel(t)
Y(i,j) = a1*Y(i,j-1) + a2*Y(i,j-2) + a3*Y(i,j-3) + ...
b1*U(i,j-1) + b2*U(i,j-2);
end
Amplitude(i) = max(abs(Y(i,:)));
end
I know I should use fft or findpeaks function in MATLAB, but I do not know how I should do it.
I've spent quite some time trying to simulate a simple SISO system using two approaches:
1) Using lsim() in MATLAB
2) By writing down the difference equations myself and iterate over them in a loop.
I was never able to get the same simulation results from both approaches, and I have no idea what I am doing wrong.
I stacked my code in a single m-file so it's easier to follow. Here is the code:
function main()
clear all
clc
simulateUsing_lsim()
simulateUsing_loop()
end
%%%%%% Simulating using lsim %%%%%%%
function simulateUsing_lsim()
% Define the continuous-time closed-loop system
P = getContPlant();
[Kp,Ki,Kd] = get_PIDgains();
C = pid(Kp,Ki,Kd);
clSys_cont = feedback(C*P,1);
% Define the discrete-time closed-loop system
hk = get_sampling_time();
clSys_disc = c2d(clSys_cont,hk);
% Generate the reference signal and the time vector
[r,t] = getReference(hk);
%% Simulate and plot using lsim
figure
lsim(clSys_disc,r,t)
%% Finding and plotting the error
y = lsim(clSys_disc,r);
e = r - y;
figure
p = plot(t,e,'b--');
set(p,'linewidth',2)
legend('error')
xlabel('Time (seconds)')
ylabel('error')
% xlim([-.1 10.1])
end
%%%%%% Simulating using loop iteration (difference equations) %%%%%%%
function simulateUsing_loop()
% Get the cont-time ol-sys
P = getContPlant();
% Get the sampling time
hk = get_sampling_time();
% Get the disc-time ol-sys in SS representation
P_disc = ss(c2d(P,hk));
Ad = P_disc.A;
Bd = P_disc.B;
Cd = P_disc.C;
% Get the PID gains
[Kp,Ki,Kd] = get_PIDgains();
% Generate the reference signal and the time vector
[r,t] = getReference(hk);
%% Perform the system simulation
x = [0 0]'; % Set initial states
e = 0; % Set initial errors
integral_sum = 0; % Set initial integral part value
for i=2:1:length(t)
% Calculate the output signal "y"
y(:,i) = Cd*x;
% Calculate the error "e"
e(:,i) = y(:,i) - r(i);
% Calculate the control signal vector "u"
integral_sum = integral_sum + Ki*hk*e(i);
u(:,i) = Kp*e(i) + integral_sum + (1/hk)*Kd*(e(:,i)-e(:,i-1));
% Saturation. Limit the value of u withing the range [-tol tol]
% tol = 100;
% if abs(u(:,i)) > tol
% u(:,i) = tol * abs(u(:,i))/u(:,i);
% else
% end
% Calculate the state vector "x"
x = Ad*x + Bd*u(:,i); % State transitions to time n
end
%% Subplots
figure
plot(t,y,'b',t,r,'g--')
%% Plotting the error
figure
p = plot(t,e,'r');
set(p,'linewidth',2)
legend('error')
xlabel('Time (seconds)')
ylabel('error')
end
function P = getContPlant()
s = tf('s');
P = 1/(s^2 + 10*s + 20);
end
function [Kp,Ki,Kd] = get_PIDgains()
Kp = 350;
Ki = 300;
Kd = 50;
end
function hk = get_sampling_time()
hk = 0.01;
end
function [r,t] = getReference(hk)
[r,t] = gensig('square',4,10,hk);
end
I got the plant model P and its PID controller from this page (see equation 10), where the system is simulated against a step reference and the result looks pretty much exactly like the lsim() result (just for a single step peak).
However, the result of simulating the system using lsim() is this:
whereas, using the for loop, I got this performance:
I would highly appreciate any help or clarification why I am getting different results.
I'm trying to implement a neural network with sigmoid function
But the following code doesnot work
This is the training part of neural network.
It doesnot update the weights properly
What is wrong in this code?
clc; clear all; close all;
% load train_data1
train_data1=[-0.498800000000000,-0.257500000000000;-0.492800000000000,-0.274300000000000;-0.470300000000000,-0.282600000000000;-0.427400000000000,-0.474000000000000;-0.420400000000000,-0.518000000000000;-0.326300000000000,-1.13230000000000;-0.317300000000000,-0.875300000000000;-0.295000000000000,-1.02770000000000;-0.267600000000000,-0.882800000000000;-0.260500000000000,-0.976500000000000;-0.216100000000000,-0.970400000000000;-0.207000000000000,-0.813800000000000;-0.164000000000000,-0.696600000000000;-0.159900000000000,-0.793300000000000;-0.122000000000000,-0.764400000000000;-0.0729000000000000,-0.435300000000000;-0.00640000000000000,-0.0546000000000000;0.132200000000000,0.710300000000000;0.137100000000000,0.587000000000000;0.160300000000000,0.819200000000000;0.230600000000000,0.989200000000000;0.286800000000000,0.737700000000000;0.334000000000000,0.943500000000000;0.375200000000000,0.688200000000000;0.429700000000000,0.567800000000000];
train_data1 = sortrows(train_data1);
% normalize data to [0,1]
data1=[train_data1];
max1=max(max(data1));
min1=min(min(data1));
train_data2 = (train_data1 - min1) / ( max1 - min1);
x = train_data2(:,1); % train input data
r = train_data2(:,2); % train output data
hidden_neurons = 2;
maxepochcount = 1000;
datacount1 = size(x,1);
% add a bias as an input
bias = ones(datacount1,1);
% x = [x bias];
% read how many inputs
inputcount = size(x,2);
% ---------- data loaded -----------
% ---------- set weights -----------
% set initial random weights
WI = (randn(inputcount,hidden_neurons) - 0.5)/10;
WO = (randn(1,hidden_neurons) - 0.5)/10;
%-----------------------------------
%--- Learning Starts Here! ---------
%-----------------------------------
eta1 = 0.5;
eta2 = eta1/5;
% do a number of epochs
for iter = 1:maxepochcount
% loop through the data
for j = 1:datacount1
% read the current sample
I = x(j,:);
D = r(j,1);
% calculate the error for this sample
H = (sigmoid(I * WI))';
O = H' * WO';
error = D-O;
% adjust weight between hidden & output
delta_i = O.*(1-O).*(D-O); % D actual, O calculated output
% Calculate error for each node in layer_(n-1)
delta_j = H.*(1-H).*(WO.'*delta_i); % H.' is the output of hidden layer
% Adjust weights in matrices sequentially
WO = WO + eta2.*delta_i*(H.') % H.' is the output of hidden layer
WI = WI + eta1.*(delta_j*(I))' % I.' is the inputs
% % adjust weight between hidden & output
% delta_HO = error.*eta2 .* hidden_val;
% WO = WO - delta_HO';
% % adjust the weights between input & hidden
% delta_IH = eta1 .* error .* WO' .* (1 - (H .^ 2)) * I;
% WI = WI - delta_IH';
end
O = sigmoid(WO*sigmoid(x * WI)');
% error(iter) = (sum(error .^ 2)) ^ 0.5;
if rem(iter,100)==0 % Every 100 epochs, show how training is doing
plot(x,O, 'color','red','linewidth',2); hold on;
drawnow;
iter
end
% return
end
only the output values are needed to be scaled to the activation function.
If we use tanh we must scale them to [-1,1], in case of sigmoid [0,1].
The code is working fine but sometimes it needs more epochs.
I want to plot multiple realizations of a stochastic process in matlab. For a single realization I have the following code:
N = 80;
T = dt*N;
dWt = zeros(1,N);
S= repmat(S0,1,N);
S(1) = S0;
dWt = sqrt(dt) * randn;
for t=2:N
dWt(t) = sqrt(dt)*randn;
dSt = k*(mu-S(t-1))*dt + sigma*S(t-1)*dWt(t);
S(t) = S(t-1)+dSt;
end
plot(handles.pMeasure, [0:dt:T],[S0,S]);
I want to replicate this loop n times and plot the results in one plot.
You could add an additional for loop, but it would be best to vectorize everything and calculate all n instances at once:
k = ...
mu = ...
sigma = ...
S0 = ... % Initial condition
dt = ... % Time step
n = ... % Number of instances
N = 80; % Number of time steps, not counting initial condition
T = dt*N; % Final time
rng(1); % Always seed random number generator
dWt = sigma*sqrt(dt)*randn(n,N); % Calculate Wiener increments
S = zeros(n,N+1); % Allocate
S(:,1) = S0; % Set initial conditions
for t = 2:N+1
S(:,t) = S(:,t-1) + k*(mu-S(:,t-1))*dt + S(:,t-1).*dWt(:,t-1);
end
plot(handles.pMeasure,0:dt:T,S)
There are further ways to optimize this if want or you can also try sde_euler in my SDETools Matlab toolbox:
k = ...
mu = ...
sigma = ...
dt = ... % Time step
n = ... % Number of instances
N = 80; % Number of time steps, not counting initial condition
T = dt*N; % Final time
f = #(t,y)k*(mu-y); % Diffusion function
g = #(t,y)sigma*y; % Drift function
t = 0:dt:T; % Time vector
S0 = zeros(n,1); % Initial conditions
opts = sdeset('RandSeed',1,...
'SDEType','Ito'); % Set random seed, specify Ito SDE
S = sde_euler(f,g,t,S0,opts); % Simulate
plot(t,S)
Lets say I have a simple logistic equation
dx/dt = 2ax(1 - x/N)
where N is the carrying capacity, a is some growth rate, and both a and N are parameters I'd like to vary.
So what I want to do is to plot a 3D graph of my fixed point and the two parameters.
I understand how to find a fixed point of a single parameter.
Here is my sample code
function xprime = MyLogisticFunction(t,X) %% The ODE
% Parameters
N = 10 % Carrying Capacity
a = 0.5 % Growth Rate
x1prime = 2*a*X(1)*(1 - X(1)/N );
xprime = [x1prime ]';
end
Next my solver
% Initial Number
x0 = 0.4;
%Time Window
tspan=[0 100];
[t,x]=ode45(#MyLogisticFunction,tspan,x0);
clf
x(end,1) % This gives me the fixed point for the parameters above.
So my real question is, how do I put a for loop across two functions, that allows me to vary a and N, so that I can plot out a 3D graph of a and N and my fixed point x*.
I've tried combining both functions into one .m file but it does not seem to work
You need to pass the parameters to your function:
function xprime = MyLogisticFunction(t,X,a,N) %% The ODE
% Parameters (passed as function arguments)
% N = 10 % Carrying Capacity
% a = 0.5 % Growth Rate
x1prime = 2*a*X(1)*(1 - X(1)/N );
xprime = [x1prime ]';
end
and then when you call the ode solver:
% Initial Number
x0 = 0.4;
%Time Window
tspan=[0 100];
a = 0.1:0.1:1; % or whatever
N = 1:10; % or whatever
x_end = zeros(length(a),length(N));
for ii = 1:length(a)
for jj = 1:length(N)
[t,x]=ode45(#(t,X)MyLogisticFunction(t,X,a(ii),N(jj)),tspan,x0);
x_end(ii,jj) = x(end,1);
end
end